Skip to content

Commit fe26fb8

Browse files
committed
fix: mathjax in epub builds
1 parent 14bf027 commit fe26fb8

File tree

5 files changed

+104
-10
lines changed

5 files changed

+104
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@codemirror/lang-python": "^6.1.2",
1616
"@codemirror/language": "^6.6.0",
1717
"@codemirror/view": "^6.9.3",
18-
"@csound/browser": "^7.0.0-beta5",
18+
"@csound/browser": "^7.0.0-beta6",
1919
"@emotion/core": "^11.0.0",
2020
"@emotion/react": "^11.10.6",
2121
"@emotion/styled": "^11.10.6",

pandoc/build_epub.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ const {
1313

1414
execSync(
1515
`pandoc book/*.md -f ${MARKDOWN_EXTENSIONS.join("+")} \
16+
--filter=pandoc/filters/epub_math.js \
1617
--filter=pandoc/filters/epub_images.js \
1718
--epub-metadata=resources/epub-metadata.yml \
19+
--metadata title="The Csound FLOSS Manual" \
1820
-o ${EPUB_OUTPUT} \
19-
--mathjax`,
21+
--mathml`,
2022
{ encoding: "utf-8" }
2123
);
2224

pandoc/filters/epub_math.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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);

resources/epub-metadata.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
title:
2-
- type: main
3-
text: Csound Floss Manual
4-
css:
5-
resources/styles/common.css
2+
- type: main
3+
text: The Csound FLOSS Manual
4+
- type: subtitle
5+
text: Release 7 (work in progress)
6+
creator:
7+
- role: author
8+
text: Joachim Heintz, Iain McCurdy and contributors
9+
publisher: CC BY
10+
language: en-US
11+
subject: reference
12+
rights: Copyright 2010 The contributors. Licensed under Creative Commons BY
13+
css: resources/styles/common.css

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,10 +2525,10 @@
25252525
style-mod "^4.0.0"
25262526
w3c-keyname "^2.2.4"
25272527

2528-
"@csound/browser@^7.0.0-beta5":
2529-
version "7.0.0-beta5"
2530-
resolved "https://registry.yarnpkg.com/@csound/browser/-/browser-7.0.0-beta5.tgz#26342367ffdc959ab77df6345d15752a99f2f9ce"
2531-
integrity sha512-fpE2CRINucbvt02eA6I9+fQUOTUpHzVvX4ZlVJi3Tuf0kWYxALy0OQSH6j9Qsv25kfKiBNrGyj6gnfw9GoyeZw==
2528+
"@csound/browser@^7.0.0-beta6":
2529+
version "7.0.0-beta7"
2530+
resolved "https://registry.yarnpkg.com/@csound/browser/-/browser-7.0.0-beta7.tgz#efbd7c5f3279279904f4a8b8cee0aae125690db8"
2531+
integrity sha512-dlYxWvY4oJTThonPHpQMGhy+IGT2Fo0enTwlK8jpJ6sYGxYTHqi0DKIdEy7zmqUTvt7WXUsnbj/nxtV7JyVzcQ==
25322532
dependencies:
25332533
eventemitter3 "^4.0.7"
25342534
google-closure-compiler "^20221102.0.1"

0 commit comments

Comments
 (0)