Skip to content

Commit

Permalink
Handle a case where the embedded note does not exist (foambubble#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
badsketch committed Sep 7, 2023
1 parent e327115 commit a308dfd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/foam-vscode/src/features/preview/wikilink-embed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ content-card![[note-e#Section 2]]`);
);
});

it('should render the bare text for an embedded note that is embedding a note that is not found', async () => {
const note = await createFile(
'This is the text of note A which includes ![[does-not-exist]]',
['note.md']
);

const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));

await withModifiedFoamConfiguration(
CONFIG_EMBED_NOTE_TYPE,
'full-inline',
() => {
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
expect(md.render(`This is the root node. ![[note]]`)).toMatch(
`<p>This is the root node. <p>This is the text of note A which includes ![[does-not-exist]]</p>
</p>`
);
}
);
});

it('should display a warning in case of cyclical inclusions', async () => {
const noteA = await createFile(
'This is the text of note A which includes ![[note-b]]',
Expand Down
8 changes: 7 additions & 1 deletion packages/foam-vscode/src/features/preview/wikilink-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { readFileSync } from 'fs';
import { workspace as vsWorkspace } from 'vscode';
import markdownItRegex from 'markdown-it-regex';
import { isSome } from '../../utils';
import { isSome, isNone } from '../../utils';
import { FoamWorkspace } from '../../core/model/workspace';
import { Logger } from '../../core/utils/log';
import { Resource, ResourceParser } from '../../core/model/note';
Expand Down Expand Up @@ -122,13 +122,19 @@ function withLinksRelativeToWorkspaceRoot(
.map(link => {
const info = MarkdownLink.analyzeLink(link);
const resource = workspace.find(info.target);
// embedded notes that aren't created are still collected
// return null so it can be filtered in the next step
if (isNone(resource)) {
return null;
}
const pathFromRoot = vsWorkspace.asRelativePath(
toVsCodeUri(resource.uri)
);
return MarkdownLink.createUpdateLinkEdit(link, {
target: pathFromRoot,
});
})
.filter(linkEdits => !isNone(linkEdits))
.sort((a, b) => Position.compareTo(b.range.start, a.range.start));
const text = edits.reduce(
(text, edit) => TextEdit.apply(text, edit),
Expand Down

0 comments on commit a308dfd

Please sign in to comment.