diff --git a/packages/foam-vscode/src/core/markdown-provider.test.ts b/packages/foam-vscode/src/core/markdown-provider.test.ts index d6a75716..52007f66 100644 --- a/packages/foam-vscode/src/core/markdown-provider.test.ts +++ b/packages/foam-vscode/src/core/markdown-provider.test.ts @@ -199,6 +199,17 @@ this note has a title expect(note.title).toBe('Page A'); }); + it('should support wikilinks and urls in title', () => { + const note = createNoteFromMarkdown( + '/page-a.md', + ` +# Page A with [[wikilink]] and a [url](https://google.com) +this note has a title + ` + ); + expect(note.title).toBe('Page A with wikilink and a url'); + }); + it('should default to file name if heading does not exist', () => { const note = createNoteFromMarkdown( '/page-d.md', @@ -487,6 +498,24 @@ This is the content of section 2. expect(note.sections[2].label).toEqual('Section 2'); expect(note.sections[2].range).toEqual(Range.create(9, 0, 13, 0)); }); + + it('should support wikilinks and links in the section label', () => { + const note = createNoteFromMarkdown( + '/dir1/section-with-links.md', + ` +# Section with [[wikilink]] + +This is the content of section with wikilink + +## Section with [url](https://google.com) + +This is the content of section with url + ` + ); + expect(note.sections).toHaveLength(2); + expect(note.sections[0].label).toEqual('Section with wikilink'); + expect(note.sections[1].label).toEqual('Section with url'); + }); }); describe('parser plugins', () => { diff --git a/packages/foam-vscode/src/core/markdown-provider.ts b/packages/foam-vscode/src/core/markdown-provider.ts index cc0e00f1..53b523cb 100644 --- a/packages/foam-vscode/src/core/markdown-provider.ts +++ b/packages/foam-vscode/src/core/markdown-provider.ts @@ -177,9 +177,9 @@ export class MarkdownResourceProvider implements ResourceProvider { */ const getTextFromChildren = (root: Node): string => { let text = ''; - visit(root, 'text', node => { - if (node.type === 'text') { - text = text + (node as any).value; + visit(root, node => { + if (node.type === 'text' || node.type === 'wikiLink') { + text = text + ((node as any).value || ''); } }); return text; @@ -226,7 +226,7 @@ const sectionsPlugin: ParserPlugin = { visit: (node, note) => { if (node.type === 'heading') { const level = (node as any).depth; - const label = ((node as Parent)!.children?.[0] as any)?.value; + const label = getTextFromChildren(node); if (!label || !level) { return; } @@ -272,8 +272,8 @@ const titlePlugin: ParserPlugin = { node.type === 'heading' && (node as any).depth === 1 ) { - note.title = - ((node as Parent)!.children?.[0] as any)?.value || note.title; + const title = getTextFromChildren(node); + note.title = title.length > 0 ? title : note.title; } }, onDidFindProperties: (props, note) => {