Skip to content

Commit

Permalink
Fix embedded notes not generating proper reference links (foambubble#…
Browse files Browse the repository at this point in the history
  • Loading branch information
badsketch committed Sep 25, 2023
1 parent 5859b2a commit 3ef1b69
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
36 changes: 36 additions & 0 deletions packages/foam-vscode/src/core/services/markdown-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,40 @@ describe('Generation of markdown references', () => {
'../dir3/page-c.md',
]);
});

it('should generate links for embedded notes that are formatted properly', () => {
const workspace = createTestWorkspace();
const noteA = createNoteFromMarkdown(
'Link to ![[page-b]] and [[page-c]]',
'/dir1/page-a.md'
);
workspace
.set(noteA)
.set(createNoteFromMarkdown('Content of note B', '/dir2/page-b.md'))
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));

const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => [r.url, r.label])).toEqual([
['../dir2/page-b.md', 'page-b'],
['../dir3/page-c.md', 'page-c'],
]);
});

it('should not generate links for placeholders', () => {
const workspace = createTestWorkspace();
const noteA = createNoteFromMarkdown(
'Link to ![[page-b]] and [[page-c]] and [[does-not-exist]] and ![[does-not-exist-either]]',
'/dir1/page-a.md'
);
workspace
.set(noteA)
.set(createNoteFromMarkdown('Content of note B', '/dir2/page-b.md'))
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));

const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => r.url)).toEqual([
'../dir2/page-b.md',
'../dir3/page-c.md',
]);
});
});
10 changes: 6 additions & 4 deletions packages/foam-vscode/src/core/services/markdown-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ export function createMarkdownReferences(

// [wikilink-text]: path/to/file.md "Page title"
return {
label:
link.rawText.indexOf('[[') > -1
? link.rawText.substring(2, link.rawText.length - 2)
: link.rawText,
// embedded looks like ![[note-a]]
// regular note looks like [[note-a]]
label: link.rawText.substring(
link.isEmbed ? 3 : 2,
link.rawText.length - 2
),
url: relativeUri.path,
title: target.title,
};
Expand Down

0 comments on commit 3ef1b69

Please sign in to comment.