repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/extensions/web/pandoc.ts

Raw
File outline
// 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 = /<div[^>]*>\n?|<\/div>\n?/gi;
const RESIDUAL_SPAN_RE = /<span[^>]*>([\s\S]*?)<\/span>/gi;

export function stripResidualTags(md: string): string {
  return md
    .replace(RESIDUAL_DIV_RE, "")
    .replace(RESIDUAL_SPAN_RE, "$1")
    .replace(/\n{3,}/g, "\n\n");
}

// Strip problematic class attributes and fix table structures for pipe table conversion
export function cleanTableClasses(html: string): string {
  return html
    .replace(/class="[^"]*\[[^\]]*\][^"]*"/g, (match) => {
      const cleaned = match.replace(/\[[^\]]*\]/g, "");
      return cleaned === 'class=""' ? "" : cleaned;
    })
    .replace(/<th[^>]*>\s*<\/th>/g, "")
    .replace(/<\/?strong>/g, (m) => m.replace(/strong/, "b"))
    .replace(/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/gi, (_match, content) => {
      return "<code>" + content.replace(/\s+/g, " ").trim().replace(/`/g, "\\`") + "</code>";
    });
}

export function runPandoc(html: string, format: string, useFilter = true): Promise<string> {
  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<string> {
  return spawnToText("pandoc", [
    "--from=" + fromFormat,
    "--to=" + toFormat,
    "--wrap=none",
    filePath,
  ]);
}