From f5789d80839617a7894c96bf441da13af4549c79 Mon Sep 17 00:00:00 2001 From: Eric Masseran Date: Fri, 16 Oct 2026 20:48:55 +0200 Subject: [PATCH] Fix wikilinks on folder to markdown link A wikilinks on `my-folder/README.md` would be `[[my-folder]]`. But once a conversion is done to generate a normal markdown link `[]()`, it broke as a folder can not be the target: ``` Failed to convert wikilink to markdown link: Resource "my-folder" not found ``` The conversion should find a link by considering README.md or index.md. --- .../features/editing/convert-links.spec.ts | 22 +++++++++++++++++++ .../vscode/features/editing/convert-links.ts | 5 ++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/foam-vscode/src/vscode/features/editing/convert-links.spec.ts b/packages/foam-vscode/src/vscode/features/editing/convert-links.spec.ts index 53624c68b..b30af70c8 100644 --- a/packages/foam-vscode/src/vscode/features/editing/convert-links.spec.ts +++ b/packages/foam-vscode/src/vscode/features/editing/convert-links.spec.ts @@ -87,6 +87,28 @@ describe('Link Conversion Commands', () => { await deleteFile(uri); }); + it('should convert a directory wikilink to its resolved index file', async () => { + const readme = await createFile('# My Folder Index', [ + 'my-folder', + 'README.md', + ]); + const { uri } = await createFile('Text before [[my-folder]] text after'); + const { editor } = await showInEditor(uri); + await waitForNoteInFoamWorkspace(readme.uri); + + editor.selection = new vscode.Selection(0, 15, 0, 15); + + await vscode.commands.executeCommand(CONVERT_WIKILINK_TO_MDLINK.command); + + const result = editor.document.getText(); + expect(result).toBe( + 'Text before [My Folder Index](my-folder/README.md) text after' + ); + + await deleteFile(readme.uri); + await deleteFile(uri); + }); + it('should show info message when no wikilink at cursor', async () => { const { uri } = await createFile('Text with no wikilinks'); const { editor } = await showInEditor(uri); diff --git a/packages/foam-vscode/src/vscode/features/editing/convert-links.ts b/packages/foam-vscode/src/vscode/features/editing/convert-links.ts index 501f875aa..7a59555af 100644 --- a/packages/foam-vscode/src/vscode/features/editing/convert-links.ts +++ b/packages/foam-vscode/src/vscode/features/editing/convert-links.ts @@ -63,7 +63,10 @@ async function convertWikilinkToMarkdown(foam: Foam): Promise { } const linkInfo = MarkdownLink.analyzeLink(targetLink); - const targetResource = foam.workspace.find(linkInfo.target); + // Resolve the link the way navigation does. resolveLink may embed the + // section as a URI fragment; strip it to get the resource URI. + const targetUri = foam.workspace.resolveLink(resource, targetLink).asPlain(); + const targetResource = foam.workspace.find(targetUri); if (!targetResource) { throw new Error(`Resource "${linkInfo.target}" not found`); }