From 0e821d09cf31581baaca001fa640af0c0a8abe94 Mon Sep 17 00:00:00 2001 From: Riccardo Ferretti Date: Fri, 22 Sep 2023 10:47:27 +0200 Subject: [PATCH] Added support for linking to sections in current file And improved support for links to sections that need to be slugified --- .../preview/wikilink-navigation.spec.ts | 9 ++++++++ .../features/preview/wikilink-navigation.ts | 23 +++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/foam-vscode/src/features/preview/wikilink-navigation.spec.ts b/packages/foam-vscode/src/features/preview/wikilink-navigation.spec.ts index b20fbf745..1a6be695c 100644 --- a/packages/foam-vscode/src/features/preview/wikilink-navigation.spec.ts +++ b/packages/foam-vscode/src/features/preview/wikilink-navigation.spec.ts @@ -52,6 +52,15 @@ describe('Link generation in preview', () => { ); }); + it('generates a link to a section within the note', () => { + expect(md.render(`[[#sec]]`)).toEqual( + `

#sec

\n` + ); + expect(md.render(`[[#Section Name]]`)).toEqual( + `

#Section Name

\n` + ); + }); + it('generates a link to a note with a specific section', () => { expect(md.render(`[[note-b#sec2]]`)).toEqual( `

note-b#sec2

\n` diff --git a/packages/foam-vscode/src/features/preview/wikilink-navigation.ts b/packages/foam-vscode/src/features/preview/wikilink-navigation.ts index f91af7379..b02c656ca 100644 --- a/packages/foam-vscode/src/features/preview/wikilink-navigation.ts +++ b/packages/foam-vscode/src/features/preview/wikilink-navigation.ts @@ -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, @@ -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 `${label}`; + )}`; + return getResourceLink( + `${resource.title}${formattedSection}`, + `${resourceLink}${linkSection}`, + label + ); } catch (e) { Logger.error( `Error while creating link for [[${wikilink}]] in Preview panel`, @@ -52,4 +64,7 @@ export const markdownItWikilinkNavigation = ( const getPlaceholderLink = (content: string) => `${content}`; +const getResourceLink = (title: string, link: string, label: string) => + `${label}`; + export default markdownItWikilinkNavigation;