import { describe, it, expect } from "vitest";
import { page_to_markdown } from "./html_to_markdown.mjs";
const ctx = { url: "https://twinkleplop.pngwn.at/docs/x", origin: "https://twinkleplop.pngwn.at" };
const page = (body: string) =>
page_to_markdown(`${body}
`, ctx) ?? "";
describe("page_to_markdown", () => {
it("keeps a twoslash popover out of the code", () => {
// the shape @twinkleplop/twoslash emits
const md = page(`
a.ts copy
const greetingconst greeting: "hello world" = "hello world"
`);
expect(md).toContain('```ts title="a.ts"\nconst greeting = "hello world"\n```');
expect(md).not.toContain("twoslash");
});
it("gives a line annotation its own line, without opening a blank one", () => {
// the stylesheet makes these blocks; in the text stream they are inline
const md = page(`
a.ts
const x = 1;
x is 1const y = x + 1;
`);
expect(md).toContain("const x = 1;\nx is 1\nconst y = x + 1;");
expect(md).not.toContain("\n\nx is 1");
});
it("drops decorative heading glyphs", () => {
const md = page(
`# themes § 02
`,
);
expect(md.trim()).toBe("## themes");
});
it("wraps inline code in the shortest fence that works", () => {
const md = page("Use language() to start.
");
expect(md.trim()).toBe("Use `language()` to start.");
});
it("outruns backticks the code already contains", () => {
const md = page("a ` b
");
expect(md.trim()).toBe("``a ` b``");
});
it("reads a container of inline content as one paragraph", () => {
const md = page(
``,
);
expect(md.trim()).toBe(
"> Map its scope colours to [token types](https://twinkleplop.pngwn.at/docs/themes-ref).",
);
});
it("always opens a fence with a language", () => {
// parsers read the first word of the info string as the language
const md = page(
`terminal
pnpm add @twinkleplop/typescript
`,
);
expect(md).toContain('```text title="terminal"');
});
it("renders a table", () => {
const md = page(
``,
);
expect(md.trim()).toBe(["| option | default |", "| --- | --- |", '| lang | "ts" |'].join("\n"));
});
it("skips navigation and pages with no article", () => {
expect(page(`body
`).trim()).toBe(
"body",
);
expect(page_to_markdown("no main here
", ctx)).toBeNull();
});
});