Skip to content

Commit

Permalink
Added support for linking to sections in current file (foambubble#1289)
Browse files Browse the repository at this point in the history
And improved support for links to sections that need to be slugified
  • Loading branch information
riccardoferretti committed Oct 12, 2023
1 parent 3ef1b69 commit e7c8d5a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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;

0 comments on commit e7c8d5a

Please sign in to comment.