diff --git a/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts b/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts index 902cae5c9..708f4be4c 100644 --- a/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts +++ b/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts @@ -135,13 +135,13 @@ describe('generateLinkReferences', () => { expect(actual).toEqual(expected); }); - it('should encode spaces links', async () => { + it('should put links with spaces in angel brackets', async () => { const note = findBySlug('angel-reference'); const expected = { newText: textForNote( ` [//begin]: # "Autogenerated link references for markdown compatibility" -[Note being referred as angel]: Note%20being%20referred%20as%20angel "Note being referred as angel" +[Note being referred as angel]: "Note being referred as angel" [//end]: # "Autogenerated link references"` ), range: Range.create(3, 0, 3, 0), diff --git a/packages/foam-vscode/src/core/services/markdown-provider.ts b/packages/foam-vscode/src/core/services/markdown-provider.ts index 55769fb1d..fd9d946a0 100644 --- a/packages/foam-vscode/src/core/services/markdown-provider.ts +++ b/packages/foam-vscode/src/core/services/markdown-provider.ts @@ -137,6 +137,12 @@ export function createMarkdownReferences( relativeUri = relativeUri.changeExtension('*', ''); } + // Extract base path and link name separately. + const basePath = relativeUri.path.split('/').slice(0, -1).join('/'); + const linkName = relativeUri.path.split('/').pop(); + + const encodedURL = encodeURIComponent(linkName).replace(/%20/g, ' '); + // [wikilink-text]: path/to/file.md "Page title" return { // embedded looks like ![[note-a]] @@ -145,7 +151,7 @@ export function createMarkdownReferences( link.isEmbed ? 3 : 2, link.rawText.length - 2 ), - url: encodeURIComponent(relativeUri.path), + url: `${basePath ? basePath + '/' : ''}${encodedURL}`, title: target.title, }; })