import { describe, it, expect } from "vitest"; import { getContent, renderCode, type Result } from "./content_renderer.js"; import { trimIndentation } from "@triliumnext/commons"; import { buildShareNote, buildShareNotes } from "../test/shaca_mocking.js"; describe("content_renderer", () => { beforeAll(() => { vi.mock("../becca/becca_loader.js", () => ({ default: { load: vi.fn(), loaded: Promise.resolve() } })); }); it("Reports protected notes not being renderable", () => { const note = buildShareNote({ isProtected: true }); const result = getContent(note); expect(result.content).toStrictEqual("

Protected note cannot be displayed

"); }); describe("Text note", () => { it("parses simple note", () => { const content = trimIndentation`\

Welcome to Trilium Notes!

`; const note = buildShareNote({ content }); const result = getContent(note); expect(result.content).toStrictEqual(content); }); it("renders included notes", () => { buildShareNotes([ { id: "subnote1", content: `

Foo

Bar
` }, { id: "subnote2", content: `Baz` } ]); const note = buildShareNote({ id: "note1", content: trimIndentation`\

Before

 
 

After

` }); const result = getContent(note); expect(result.content).toStrictEqual(trimIndentation`\

Before

Foo

Bar
Baz

After

`); }); it("handles syntax highlight for code blocks with escaped syntax", () => { const note = buildShareNote({ id: "note", content: trimIndentation`\

Defining the options

                    <t t-name="module.SectionWidthOption">
                    <BuilderRow label.translate="Section Width">
                    </BuilderRow>
                    </t>
                    
` }); const result = getContent(note); expect(result.content).toStrictEqual(trimIndentation`\

Defining the options

                <t t-name="module.SectionWidthOption">
                <BuilderRow label.translate="Section Width">
                </BuilderRow>
                </t>
                
`) }); describe("Reference links", () => { it("handles attachment link", () => { const content = trimIndentation`\

Test

5863845791835102555.mp4  

`; const note = buildShareNote({ content, attachments: [ { id: "q14s2Id7V6pp", title: "5863845791835102555.mp4" } ] }); const result = getContent(note); expect(result.content).toStrictEqual(trimIndentation`\

Test

5863845791835102555.mp4  

`); }); it("handles protected notes", () => { buildShareNote({ id: "MSkxxCFbBsYP", title: "Foo", isProtected: true }); const note = buildShareNote({ id: "note", content: trimIndentation`\

Foo

` }); const result = getContent(note); expect(result.content).toStrictEqual(trimIndentation`\

[protected]

`); }); it("handles missing notes", () => { const note = buildShareNote({ id: "note", content: trimIndentation`\

Foo

` }); const result = getContent(note); const content = (result.content as string).replaceAll(/\s/g, ""); expect(content).toStrictEqual("

Foo

"); }); it("properly escapes note title", () => { buildShareNote({ id: "MSkxxCFbBsYP", title: "The quick brown fox" }); const note = buildShareNote({ id: "note", content: trimIndentation`\

Hi

` }); const result = getContent(note); expect(result.content).toStrictEqual(trimIndentation`\

The quick <strong>brown</strong> fox

`); }); }); }); describe("renderCode", () => { it("identifies empty content", () => { const emptyResult: Result = { header: "", content: " " }; renderCode(emptyResult); expect(emptyResult.isEmpty).toBeTruthy(); }); it("identifies unsupported content type", () => { const emptyResult: Result = { header: "", content: Buffer.from("Hello world") }; renderCode(emptyResult); expect(emptyResult.isEmpty).toBeTruthy(); }); it("wraps code in
", () => {
            const result: Result = {
                header: "",
                content: "\tHello\nworld"
            };
            renderCode(result);
            expect(result.isEmpty).toBeFalsy();
            expect(result.content).toBe("
\tHello\nworld
"); }); }); });