Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import { describe, expect, it } from "vitest";import { COLLAPSED_LINES, collapseCodeblocks } from "#lib/markup/collapse.ts";import { renderMarkup } from "#lib/markup/render.ts";import type { MarkupContext } from "#lib/markup/paths.ts";
const ctx: MarkupContext = { repo: "ada.test/infra", ref: "main", collapseCode: true };
const fence = (lines: number): string => ["```ts", ...Array.from({ length: lines }, (_, i) => `const x${i} = ${i};`), "```"].join("\n");
describe("collapseCodeblocks", () => { it("leaves short blocks alone", () => { const html = `<pre><code>${"a\n".repeat(COLLAPSED_LINES - 1)}</code></pre>`; expect(collapseCodeblocks(html)).toBe(html); });
it("wraps tall blocks with their line count and a toolbar slot", () => { const html = `<p>x</p><pre data-line="2"><code>${"a\n".repeat(COLLAPSED_LINES)}</code></pre>`; expect(collapseCodeblocks(html)).toBe( `<p>x</p><div data-codeblock="${COLLAPSED_LINES}"><div data-codeblock-toolbar></div><pre data-line="2"><code>${"a\n".repeat(COLLAPSED_LINES)}</code></pre></div>` ); });
it("counts a last line without a trailing newline", () => { const html = `<pre>${"a\n".repeat(COLLAPSED_LINES - 1)}a</pre>`; expect(collapseCodeblocks(html)).toContain(`data-codeblock="${COLLAPSED_LINES}"`); });
it("wraps only the innermost of nested pre elements", () => { const inner = `<pre>${"a\n".repeat(COLLAPSED_LINES)}</pre>`; const out = collapseCodeblocks(`<pre>x${inner}</pre>`); expect(out.match(/data-codeblock=/g)).toHaveLength(1); expect(out.startsWith("<pre>x<div data-codeblock=")).toBe(true); });});
describe("render", () => { it("collapses tall code in bodies", async () => { expect(await renderMarkup(fence(40), ctx)).toContain('data-codeblock="40"'); });
it("keeps tall code in documents expanded", async () => { expect(await renderMarkup(fence(40), { ...ctx, collapseCode: false })).not.toContain( "data-codeblock" ); });
it("keeps tall mermaid blocks whole", async () => { const diagram = [ "```mermaid", "flowchart TB", ...Array.from({ length: 40 }, (_, i) => ` n${i} --> n${i + 1}`), "```" ].join("\n"); const html = await renderMarkup(diagram, ctx); expect(html).toContain("<svg "); expect(html).not.toContain("data-codeblock"); });
it("drops a data-codeblock written in the source", async () => { expect(await renderMarkup('<div data-codeblock="99">x</div>', ctx)).not.toContain( "data-codeblock" ); });});