feat(import/markdown): start parsing wikilinks

This commit is contained in:
Elian Doran 2025-06-20 18:28:08 +03:00
parent 1c3cd9e7ca
commit 3190aa6fe6
No known key found for this signature in database
2 changed files with 14 additions and 1 deletions

View File

@ -281,4 +281,10 @@ $$`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected); expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
}); });
it("supports wikilink with root-relative path", () => {
const input = `oh no my banana I bought on [[journal/monday]] has gone off! Im taking it back to the [[other/shop]] for a refund`;
const expected = `<p>oh no my banana I bought on <a class="reference-link" href="journal/monday">journal/monday</a> has gone off! Im taking it back to the <a class="reference-link" href="other/shop">other/shop</a> for a refund</p>`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
}); });

View File

@ -23,7 +23,9 @@ class CustomMarkdownRenderer extends Renderer {
} }
override paragraph(data: Tokens.Paragraph): string { override paragraph(data: Tokens.Paragraph): string {
return super.paragraph(data).trimEnd(); let text = super.paragraph(data).trimEnd();
text = processWikiLinks(text);
return text;
} }
override code({ text, lang }: Tokens.Code): string { override code({ text, lang }: Tokens.Code): string {
@ -212,6 +214,11 @@ function restoreFromMap(text: string, map: Map<string, string>): string {
return text.replace(new RegExp(pattern, 'g'), match => map.get(match) ?? match); return text.replace(new RegExp(pattern, 'g'), match => map.get(match) ?? match);
} }
function processWikiLinks(paragraph: string) {
paragraph = paragraph.replaceAll(/\[\[([^\[\]]+)\]\]/g, `<a class="reference-link" href="$1">$1</a>`);
return paragraph;
}
const renderer = new CustomMarkdownRenderer({ async: false }); const renderer = new CustomMarkdownRenderer({ async: false });
export default { export default {