// Pandoc HTML→markdown conversion with LLM-friendly content extraction // Uses a lua filter to strip nav/sidebar/ads and fix code block languages // Then strips residual raw-HTML tags from commonmark output import { spawn } from "node:child_process"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { spawnToText } from "./spawn.js"; const __dirname = dirname(fileURLToPath(import.meta.url)); const LUA_FILTER = join(__dirname, "llm-extract.lua"); // [perf] Single-pass residual tag cleanup. // Merged 4 chained .replace() into 2 passes to reduce intermediate strings. const RESIDUAL_DIV_RE = /
]*>]*>([\s\S]*?)<\/code><\/pre>/gi,
(_match, content) => {
return (
"" +
content.replace(/\s+/g, " ").trim().replace(/`/g, "\\`") +
""
);
},
);
}
export function runPandoc(
html: string,
format: string,
useFilter = true,
): Promise {
return new Promise((resolve, reject) => {
const toFormat =
format === "text"
? "plain"
: format === "html"
? "html"
: format === "json"
? "json"
: "markdown-raw_html";
const args = ["--from=html", "--to=" + toFormat, "--wrap=none"];
if (useFilter) {
args.push("--lua-filter=" + LUA_FILTER);
}
const proc = spawn("pandoc", args);
let stdout = "";
let stderr = "";
proc.stdout.on("data", (d: Buffer) => (stdout += d));
proc.stderr.on("data", (d: Buffer) => (stderr += d));
proc.on("close", (code) => {
if (code === 0) {
resolve(
format === "html" || format === "json"
? stdout
: stripResidualTags(stdout),
);
} else {
reject(new Error(stderr || `pandoc: ${code}`));
}
});
proc.on("error", reject);
proc.stdin.write(cleanTableClasses(html));
proc.stdin.end();
});
}
// Convert non-HTML formats via pandoc (docx, odt, epub, rtf, etc.)
export function runPandocFile(
filePath: string,
fromFormat: string,
toFormat: string,
): Promise {
return spawnToText("pandoc", [
"--from=" + fromFormat,
"--to=" + toFormat,
"--wrap=none",
filePath,
]);
}