Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for linking to sections in current file #1289

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('Link generation in preview', () => {
);
});

it('generates a link to a section within the note', () => {
expect(md.render(`[[#sec]]`)).toEqual(
`<p><a class='foam-note-link' title='sec' href='#sec' data-href='#sec'>#sec</a></p>\n`
);
expect(md.render(`[[#Section Name]]`)).toEqual(
`<p><a class='foam-note-link' title='Section Name' href='#section-name' data-href='#section-name'>#Section Name</a></p>\n`
);
});

it('generates a link to a note with a specific section', () => {
expect(md.render(`[[note-b#sec2]]`)).toEqual(
`<p><a class='foam-note-link' title='My second note#sec2' href='/path2/to/note-b.md#sec2' data-href='/path2/to/note-b.md#sec2'>note-b#sec2</a></p>\n`
Expand Down
23 changes: 19 additions & 4 deletions packages/foam-vscode/src/features/preview/wikilink-navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { toVsCodeUri } from '../../utils/vsc-utils';
import { MarkdownLink } from '../../core/services/markdown-link';
import { Range } from '../../core/model/range';
import { isEmpty } from 'lodash';
import { toSlug } from '../../utils/slug';

export const markdownItWikilinkNavigation = (
md: markdownit,
Expand All @@ -26,18 +27,29 @@ export const markdownItWikilinkNavigation = (
isEmbed: false,
});
const formattedSection = section ? `#${section}` : '';
const linkSection = section ? `#${toSlug(section)}` : '';
const label = isEmpty(alias) ? `${target}${formattedSection}` : alias;

// [[#section]] links
if (target.length === 0) {
// we don't have a good way to check if the section exists within the
// open file, so we just create a regular link for it
return getResourceLink(section, linkSection, label);
}

const resource = workspace.find(target);
if (isNone(resource)) {
return getPlaceholderLink(label);
}

const link = `${vscode.workspace.asRelativePath(
const resourceLink = `/${vscode.workspace.asRelativePath(
toVsCodeUri(resource.uri)
)}${formattedSection}`;
const title = `${resource.title}${formattedSection}`;
return `<a class='foam-note-link' title='${title}' href='/${link}' data-href='/${link}'>${label}</a>`;
)}`;
return getResourceLink(
`${resource.title}${formattedSection}`,
`${resourceLink}${linkSection}`,
label
);
} catch (e) {
Logger.error(
`Error while creating link for [[${wikilink}]] in Preview panel`,
Expand All @@ -52,4 +64,7 @@ export const markdownItWikilinkNavigation = (
const getPlaceholderLink = (content: string) =>
`<a class='foam-placeholder-link' title="Link to non-existing resource" href="javascript:void(0);">${content}</a>`;

const getResourceLink = (title: string, link: string, label: string) =>
`<a class='foam-note-link' title='${title}' href='${link}' data-href='${link}'>${label}</a>`;

export default markdownItWikilinkNavigation;