Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import { describe, expect, it } from "vitest";import { COLLAPSED_LINES, collapseCodeblocks } from "$lib/markup/collapse";import { renderDocument, renderMarkup } from "$lib/markup/render";import type { MarkupContext } from "$lib/markup/paths";
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("drops a data-codeblock written in the source", async () => { expect(await renderMarkup('<div data-codeblock="99">x</div>', ctx)).not.toContain( "data-codeblock" ); });});