import { toHtml } from "hast-util-to-html"; import remarkGfm from "remark-gfm"; import remarkParse from "remark-parse"; import remarkRehype from "remark-rehype"; import { type PluggableList, unified } from "unified"; import { describe, expect, it } from "vite-plus/test"; import { remarkCodexDirectives } from "./codexMarkdownDirectives.ts"; import { remarkNormalizeListItemIndentation } from "./markdownListIndentation.ts"; import { remarkEnableLeadingWhitespace } from "./markdownLeadingWhitespace.ts"; const processor = unified() .use(remarkParse) .use([ remarkGfm, remarkNormalizeListItemIndentation, remarkCodexDirectives, ] satisfies PluggableList) .use(remarkRehype, { allowDangerousHtml: true }); function renderMarkdown(markdown: string): string { return toHtml(processor.runSync(processor.parse(markdown), markdown), { allowDangerousHtml: true, }); } describe("remarkNormalizeListItemIndentation", () => { it.each([ "- Read [the guide][guide].\n\n[guide]: https://example.com/guide", '> [guide]: https://example.com/guide\n> "Guide"\n\n- Read [the guide][GUIDE].', ">>[my\n> > guide]: https://example.com/guide\n\n- Read [the guide][my guide].", ])("uses document reference definitions in recovered items: %j", (markdown) => { expect(renderMarkdown(markdown)).toBe(renderMarkdown(markdown.replace("- ", "- "))); expect(renderMarkdown(markdown)).toContain(' bar'], ["- foo\n \tbar", "\nbar"], ])("keeps continuation indentation aligned to tab stops: %j", (markdown, expected) => { const userProcessor = processor().use(remarkEnableLeadingWhitespace); const html = toHtml(userProcessor.runSync(userProcessor.parse(markdown), markdown)); expect(html).toContain(expected); }); it("renders a leading directive in over-indented list content", () => { expect(renderMarkdown('- :codex-file-citation{path="src/a.ts"}')).toBe( '', ); }); it("renders same-line over-indented list content as list text", () => { const html = renderMarkdown(`why did you do this? - for (const step of rest.steps) { - if (step.request.body) { - step.request.body = ""; - } - }`); expect(html).not.toContain("
");
    expect(html).toContain("
  • for (const step of rest.steps) {
  • "); expect(html).toContain("
  • if (step.request.body) {
  • "); expect(html).toContain('
  • step.request.body = "";
  • '); }); it("preserves every recovered block separated by blank lines", () => { const html = renderMarkdown(`- **first block** [second block](https://example.com)`); expect(html).toContain("first block"); expect(html).toContain('second block'); expect( renderMarkdown( "- Read [the guide][guide].\n\n [guide]: /local\n\n[guide]: /document", ), ).toContain('the guide'); expect(renderMarkdown("- first\n\n ```\n \tcode\n ```")).toContain( "
    \tcode\n
    ", ); }); it("recursively normalizes lists in recovered tail blocks", () => { const html = renderMarkdown(`- first block - nested block`); expect(html).not.toContain("
    ");
        expect(html).toContain("
  • nested block
  • "); }); it("preserves fenced code blocks within list items", () => { const html = renderMarkdown(`- \`\`\`ts const value = 1; \`\`\``); expect(html).toContain('
    const value = 1;');
      });
    
      it("preserves indented code blocks that start below a list marker", () => {
        const html = renderMarkdown(`-
          const value = 1;`);
    
        expect(html).toContain("
    const value = 1;");
      });
    });