Skip to content

Commit

Permalink
Merge pull request #3724 from owid/remove-mathjax
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber authored Jun 19, 2024
2 parents d5f7f2d + dd61524 commit fe1e576
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 136 deletions.
71 changes: 0 additions & 71 deletions baker/formatWordpressPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ import {
formatDataValue,
formatImages,
} from "./formatting.js"
import { mathjax } from "mathjax-full/js/mathjax.js"
import { TeX } from "mathjax-full/js/input/tex.js"
import { SVG } from "mathjax-full/js/output/svg.js"
import { liteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor.js"
import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html.js"
import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js"
import { replaceIframesWithExplorerRedirectsInWordPressPost } from "./replaceExplorerRedirects.js"
import { EXPLORERS_ROUTE_FOLDER } from "../explorer/ExplorerConstants.js"
import {
Expand Down Expand Up @@ -62,65 +56,6 @@ import { RELATED_CHARTS_CLASS_NAME } from "../site/blocks/RelatedCharts.js"
import { logErrorAndMaybeSendToBugsnag } from "../serverUtils/errorLog.js"
import { KnexReadonlyTransaction } from "../db/db.js"

const initMathJax = () => {
const adaptor = liteAdaptor()
RegisterHTMLHandler(adaptor)

const tex = new TeX({ packages: AllPackages })
const svg = new SVG({ fontCache: "none" })
const doc = mathjax.document("", {
InputJax: tex,
OutputJax: svg,
})

return function format(latex: string): string {
const node = doc.convert(latex, {
display: true,
})
return adaptor.outerHTML(node) // as HTML
}
}

const formatMathJax = initMathJax()

const extractLatex = (html: string): [string, string[]] => {
const latexBlocks: string[] = []
html = html.replace(/\[latex\]([\s\S]*?)\[\/latex\]/gm, (_, latex) => {
latexBlocks.push(
latex.replace("\\[", "").replace("\\]", "").replace(/\$\$/g, "")
)
return "[latex]"
})
return [html, latexBlocks]
}

const formatLatex = async (
html: string,
latexBlocks?: string[]
): Promise<string> => {
if (!latexBlocks) [html, latexBlocks] = extractLatex(html)

// return early so we don't do unnecessary work for sites without latex
if (!latexBlocks.length) return html

const compiled: string[] = []
for (const latex of latexBlocks) {
try {
compiled.push(formatMathJax(latex))
} catch (err) {
compiled.push(
`${latex} (Could not format equation due to MathJax error)`
)
}
}

let i = -1
return html.replace(/\[latex\]/g, () => {
i += 1
return compiled[i]
})
}

export const formatWordpressPost = async (
post: FullPost,
formattingOptions: FormattingOptions,
Expand All @@ -144,12 +79,6 @@ export const formatWordpressPost = async (
// Strip comments
html = html.replace(/<!--[^>]+-->/g, "")

// Need to skirt around wordpress formatting to get proper latex rendering
let latexBlocks
;[html, latexBlocks] = extractLatex(html)

html = await formatLatex(html, latexBlocks)

// Footnotes
const footnotes: string[] = []
html = html.replace(/{ref}([\s\S]*?){\/ref}/gm, (_, footnote) => {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"jsonwebtoken": "^9.0.0",
"knex": "^3.1.0",
"lodash": "^4.17.21",
"mathjax-full": "^3.1.0",
"md5": "^2.3.0",
"mdast-util-find-and-replace": "1.1.1",
"mdast-util-from-markdown": "^0.8.0",
Expand Down
11 changes: 0 additions & 11 deletions site/owid.scss
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,6 @@ h6:hover .deep-link {
blockquote {
font-style: italic;
}

/* Center latex equations that occur on their own line e.g. https://ourworldindata.org/projections-of-future-education#methodology */
mjx-container:only-child {
display: block;
margin: auto;
text-align: center;

svg {
max-width: 100%; // e.g. https//ourworldindata.org/coronavirus#the-definition-of-the-case-fatality-rate-cfr
}
}
}

/* Major sections e.g. I. Empirical View */
Expand Down
53 changes: 0 additions & 53 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7591,13 +7591,6 @@ __metadata:
languageName: node
linkType: hard

"commander@npm:^6.0.0":
version: 6.2.1
resolution: "commander@npm:6.2.1"
checksum: 10/25b88c2efd0380c84f7844b39cf18510da7bfc5013692d68cdc65f764a1c34e6c8a36ea6d72b6620e3710a930cf8fab2695bdec2bf7107a0f4fa30a3ef3b7d0e
languageName: node
linkType: hard

"commander@npm:^7.2.0":
version: 7.2.0
resolution: "commander@npm:7.2.0"
Expand Down Expand Up @@ -11097,7 +11090,6 @@ __metadata:
lerna: "npm:^8.1.3"
lint-staged: "npm:^15.2.2"
lodash: "npm:^4.17.21"
mathjax-full: "npm:^3.1.0"
md5: "npm:^2.3.0"
mdast-util-find-and-replace: "npm:1.1.1"
mdast-util-from-markdown: "npm:^0.8.0"
Expand Down Expand Up @@ -13985,17 +13977,6 @@ __metadata:
languageName: node
linkType: hard

"mathjax-full@npm:^3.1.0":
version: 3.1.0
resolution: "mathjax-full@npm:3.1.0"
dependencies:
esm: "npm:^3.2.25"
mj-context-menu: "npm:^0.6.1"
speech-rule-engine: "npm:^3.1.0"
checksum: 10/aaff9dbd17978909ad5ced2cbbec96cdcd1f1280a0b12f348d2b85323e42cf2996898dfa2fb9cc3d5304e3f81dec4597e5787b9de9e579f97ffe3fa686fcda30
languageName: node
linkType: hard

"md5@npm:^2.3.0":
version: 2.3.0
resolution: "md5@npm:2.3.0"
Expand Down Expand Up @@ -14444,13 +14425,6 @@ __metadata:
languageName: node
linkType: hard

"mj-context-menu@npm:^0.6.1":
version: 0.6.1
resolution: "mj-context-menu@npm:0.6.1"
checksum: 10/5b9d6e3dabd9098eb37b9583a55b5e23d9d4f80a97c295c2f4ca318233801519e5abc7b5d7907817fea5cc7dcf334f192ac9622e2d4da55d350b78c8e31e7e3a
languageName: node
linkType: hard

"mkdirp-classic@npm:^0.5.2, mkdirp-classic@npm:^0.5.3":
version: 0.5.3
resolution: "mkdirp-classic@npm:0.5.3"
Expand Down Expand Up @@ -18579,19 +18553,6 @@ __metadata:
languageName: node
linkType: hard

"speech-rule-engine@npm:^3.1.0":
version: 3.1.0
resolution: "speech-rule-engine@npm:3.1.0"
dependencies:
commander: "npm:^6.0.0"
wicked-good-xpath: "npm:^1.3.0"
xmldom-sre: "npm:^0.1.31"
bin:
sre: ./bin/sre
checksum: 10/b54380b3c5f161102383a09bfe25f63ab4243cbf0385b411a929b4cf84f2a2f79176692425afc0e108aabd50779d83f41e939b354e36f38410decd3ccd3504ff
languageName: node
linkType: hard

"spel2js@npm:^0.2.8":
version: 0.2.8
resolution: "spel2js@npm:0.2.8"
Expand Down Expand Up @@ -20615,13 +20576,6 @@ __metadata:
languageName: node
linkType: hard

"wicked-good-xpath@npm:^1.3.0":
version: 1.3.0
resolution: "wicked-good-xpath@npm:1.3.0"
checksum: 10/e4d7d6c1b48ccb4f406d69217b2fe1073988bcb970645c64be359a1cc81e5aec49d348a7763ad54c7c4a05b6a92986dff7c23edcc5462b54bf1260d52217ace4
languageName: node
linkType: hard

"wide-align@npm:^1.1.2, wide-align@npm:^1.1.5":
version: 1.1.5
resolution: "wide-align@npm:1.1.5"
Expand Down Expand Up @@ -20841,13 +20795,6 @@ __metadata:
languageName: node
linkType: hard

"xmldom-sre@npm:^0.1.31":
version: 0.1.31
resolution: "xmldom-sre@npm:0.1.31"
checksum: 10/0aad5fda92cab061cb47be4b5e50dedef007dbd394f3ce9134c957203561aace5bd0b5fac10cb573fc326440e852390de74110424d6afd7a7d700466b1b171d9
languageName: node
linkType: hard

"xtend@npm:^4.0.0, xtend@npm:~4.0.1":
version: 4.0.2
resolution: "xtend@npm:4.0.2"
Expand Down

0 comments on commit fe1e576

Please sign in to comment.