Skip to content

Commit

Permalink
Fix #878 - Added support for (wiki)links in titles
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoferretti committed Dec 16, 2021
1 parent dc76660 commit 2eeb2e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
29 changes: 29 additions & 0 deletions packages/foam-vscode/src/core/markdown-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/foam-vscode/src/core/markdown-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 2eeb2e1

Please sign in to comment.