Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(markdown): bring the tabs extension to full coverage in commons
Elian Doran4d2d010922d4
diff --git a/packages/commons/src/lib/markdown_renderer.spec.ts b/packages/commons/src/lib/markdown_renderer.spec.tsindex 8756d71d5d..c920518151 100644--- a/packages/commons/src/lib/markdown_renderer.spec.ts+++ b/packages/commons/src/lib/markdown_renderer.spec.ts@@ -657,6 +657,47 @@ describe("renderToHtml", () => {});});+ describe("tabs blocks (MkDocs content tabs)", () => {+ const tab = (title: string, panel: string) =>+ `<section class="trilium-tab"><p class="trilium-tab-title">${title}</p>` ++ `<div class="trilium-tab-panel">${panel}</div></section>`;+ const tabs = (...items: string[]) => `<div class="trilium-tabs">${items.join("")}</div>`;++ it("groups consecutive tabs into one block and starts a new block at ===!", () => {+ const input = [+ "Before.",+ "=== \"Windows\"",+ "",+ " Run the **installer**.",+ "",+ "=== \"<b>Linux</b>\"",+ "\t- One",+ "\t- Two",+ "===! \"Other\"",+ " x",+ "",+ "After."+ ].join("\n");+ expect(render(input)).toBe(+ "<p>Before.</p>" ++ tabs(+ tab("Windows", "<p>Run the <strong>installer</strong>.</p>"),+ tab("<b>Linux</b>", "<ul><li>One</li><li>Two</li></ul>")+ ) ++ tabs(tab("Other", "<p>x</p>")) ++ "<p>After.</p>"+ );+ });++ it("fills an empty title and an empty panel at the end of the input", () => {+ expect(render("=== \"\"")).toBe(tabs(tab(" ", "<p> </p>")));+ });++ it("leaves === without a quoted title to the other tokenizers", () => {+ expect(render("=== Title")).toBe("<p>=== Title</p>");+ });+ });+describe("Obsidian syntax (obsidian option)", () => {it("turns %% comment %% into an HTML comment only when the obsidian flag is set", () => {expect(render("a %%secret%% b", "", { obsidian: true })).toBe("<p>a <!-- secret --> b</p>");diff --git a/packages/commons/src/lib/marked_extensions.ts b/packages/commons/src/lib/marked_extensions.tsindex 3904d24618..5af35e117a 100644--- a/packages/commons/src/lib/marked_extensions.ts+++ b/packages/commons/src/lib/marked_extensions.ts@@ -262,20 +262,19 @@ export function createTabsExtensions(): TokenizerAndRendererExtension[] {let raw = "";let rest = src;for (;;) {- const header = TAB_HEADER.exec(rest);- if (!header || (header[1] && tabs.length)) {+ const match = TAB.exec(rest);+ if (!match || (match[1] && tabs.length)) {break;}- const body = TAB_BODY.exec(rest.slice(header[0].length))?.[0] ?? "";- const content = body.replace(/^(?: {4}|\t)/gm, "");+ const content = match[3].replace(/^(?: {4}|\t)/gm, "");tabs.push({type: "tab",- raw: header[0] + body,- title: header[2],+ raw: match[0],+ title: match[2],tokens: this.lexer.blockTokens(content, [])});- raw += header[0] + body;- rest = rest.slice(header[0].length + body.length);+ raw += match[0];+ rest = rest.slice(match[0].length);}if (tabs.length) {@@ -299,11 +298,11 @@ export function createTabsExtensions(): TokenizerAndRendererExtension[] {];}-/** `=== "Title"`, or `===! "Title"` to start a new block; the title runs to the last quote. */-const TAB_HEADER = /^===(!)? +"(.*)" *(?:\n|$)/;--/** The lines of a tab's panel: blank, or indented by four spaces or a tab. */-const TAB_BODY = /^(?:[ \t]*\n|(?: {4}|\t).*(?:\n|$))*/;+/**+ * One tab: the `=== "Title"` line (`===! "Title"` starts a new block; the title runs to the last+ * quote), then its panel, the following lines that are blank or indented by four spaces or a tab.+ */+const TAB = /^===(!)? +"(.*)" *(?:\n|$)((?:[ \t]*\n|(?: {4}|\t).*(?:\n|$))*)/;/** Pre-configured wiki-link extension for server-side (uses /noteId format) */export const wikiLinkExtension = createWikiLinkExtension();