// @vitest-environment node
import { toDocx } from "@m2d/core";
import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import { unified } from "unified";
import { describe, expect, it } from "vitest";
import { htmlNodePlugin, nodeHtmlPlugin } from "./html-node";
describe("html-node", () => {
it("should run in pure node without browser globals", () => {
expect(typeof window).toBe("undefined");
expect(typeof document).toBe("undefined");
});
it("should process HTML in markdown on Node.js", async () => {
const markdown =
'
Content with bold text
';
const ast = unified().use(remarkParse).use(remarkGfm).parse(markdown);
const docxBlob = (await toDocx(
ast,
{},
{ plugins: [nodeHtmlPlugin()] },
"uint8array",
)) as Uint8Array;
expect(docxBlob).toBeInstanceOf(Uint8Array);
expect(docxBlob.length).toBeGreaterThan(0);
});
it("should parse HTML tables on Node.js", async () => {
const markdown =
'';
const ast = unified().use(remarkParse).use(remarkGfm).parse(markdown);
const docxBlob = (await toDocx(
ast,
{},
{ plugins: [htmlNodePlugin()] },
"uint8array",
)) as Uint8Array;
expect(docxBlob).toBeInstanceOf(Uint8Array);
expect(docxBlob.length).toBeGreaterThan(0);
});
it("should support custom document injection", () => {
const customPlugin = nodeHtmlPlugin({ document: undefined });
expect(customPlugin.preprocess).toBeDefined();
});
interface AstTestNode {
type: string;
value?: string;
depth?: number;
data?: Record;
children?: AstTestNode[];
}
it("should parse
## Section Heading
| Col A | Col B |
| :--- | :--- |
| Val 1 | Val 2 |
`;
const ast = unified().use(remarkParse).use(remarkGfm).parse(markdown);
const plugin = htmlNodePlugin();
plugin.preprocess?.(ast, {});
const children = ast.children as unknown as AstTestNode[];
// Verify