mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 01:52:28 +08:00
feat(import/markdown): start parsing wikilinks
This commit is contained in:
parent
1c3cd9e7ca
commit
3190aa6fe6
@ -281,4 +281,10 @@ $$`;
|
||||
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! I’m 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! I’m 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);
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -23,7 +23,9 @@ class CustomMarkdownRenderer extends Renderer {
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -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);
|
||||
}
|
||||
|
||||
function processWikiLinks(paragraph: string) {
|
||||
paragraph = paragraph.replaceAll(/\[\[([^\[\]]+)\]\]/g, `<a class="reference-link" href="$1">$1</a>`);
|
||||
return paragraph;
|
||||
}
|
||||
|
||||
const renderer = new CustomMarkdownRenderer({ async: false });
|
||||
|
||||
export default {
|
||||
|
Loading…
x
Reference in New Issue
Block a user