|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const { stdio } = require("pandoc-filter"); |
| 6 | +const R = require("ramda"); |
| 7 | + |
| 8 | +function processMathContent(mathContent) { |
| 9 | + // Clean up the math content and handle multi-line equations |
| 10 | + let cleaned = mathContent.trim(); |
| 11 | + |
| 12 | + // If the content contains \\, wrap it in an align* environment for proper LaTeX |
| 13 | + if (cleaned.includes("\\\\")) { |
| 14 | + // Split by \\ and join with proper line breaks for align environment |
| 15 | + const lines = cleaned |
| 16 | + .split("\\\\") |
| 17 | + .map((line) => line.trim()) |
| 18 | + .filter((line) => line.length > 0); |
| 19 | + cleaned = `\\begin{align*}\n${lines.join(" \\\\\n")}\n\\end{align*}`; |
| 20 | + } |
| 21 | + |
| 22 | + return cleaned; |
| 23 | +} |
| 24 | + |
| 25 | +function action({ t: type, c: value }, format, meta) { |
| 26 | + // Only process for EPUB format |
| 27 | + if (format !== "epub" && format !== "epub3") { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // Handle Math elements directly |
| 32 | + if (type === "Math") { |
| 33 | + const mathType = value[0].t; // "DisplayMath" or "InlineMath" |
| 34 | + const mathContent = value[1]; |
| 35 | + |
| 36 | + if (mathType === "DisplayMath") { |
| 37 | + const processedContent = processMathContent(mathContent); |
| 38 | + return { t: "Math", c: [{ t: "DisplayMath" }, processedContent] }; |
| 39 | + } else if (mathType === "InlineMath") { |
| 40 | + // For inline math, just clean up but don't wrap in align |
| 41 | + const cleaned = mathContent.trim(); |
| 42 | + return { t: "Math", c: [{ t: "InlineMath" }, cleaned] }; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Handle RawInline HTML that contains math expressions |
| 47 | + if (type === "RawInline" && value[0] === "html") { |
| 48 | + const htmlContent = value[1]; |
| 49 | + |
| 50 | + // Check if this HTML contains math expressions wrapped in $$ or $ |
| 51 | + const mathRegex = /\$\$([^$]+)\$\$/g; |
| 52 | + const inlineMathRegex = /\$([^$]+)\$/g; |
| 53 | + |
| 54 | + let match; |
| 55 | + |
| 56 | + // Handle display math ($$...$$) |
| 57 | + if ((match = mathRegex.exec(htmlContent)) !== null) { |
| 58 | + const mathContent = processMathContent(match[1]); |
| 59 | + return { t: "Math", c: [{ t: "DisplayMath" }, mathContent] }; |
| 60 | + } |
| 61 | + |
| 62 | + // Handle inline math ($...$) |
| 63 | + if ((match = inlineMathRegex.exec(htmlContent)) !== null) { |
| 64 | + const mathContent = match[1].trim(); |
| 65 | + return { t: "Math", c: [{ t: "InlineMath" }, mathContent] }; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Handle RawBlock HTML that contains math expressions |
| 70 | + if (type === "RawBlock" && value[0] === "html") { |
| 71 | + const htmlContent = value[1]; |
| 72 | + |
| 73 | + // Look for math expressions in HTML blocks |
| 74 | + const mathRegex = /\$\$([^$]+)\$\$/g; |
| 75 | + let match; |
| 76 | + |
| 77 | + if ((match = mathRegex.exec(htmlContent)) !== null) { |
| 78 | + const mathContent = processMathContent(match[1]); |
| 79 | + return { t: "Math", c: [{ t: "DisplayMath" }, mathContent] }; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +stdio(action); |
0 commit comments