Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import { describe, expect, it } from "vitest";import { COLLAPSED_LINES, collapseCodeblocks } from "#lib/markup/collapse.ts";import { renderDocument, renderMarkup } from "#lib/markup/render.ts";import type { MarkupContext } from "#lib/markup/paths.ts";
const ctx: MarkupContext = { repo: "ada.test/infra", ref: "main" };
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 renderDocument("README.md", fence(40), ctx)).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"); expect(await renderMarkup(diagram, ctx)).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" ); });});