Skip to content

Commit

Permalink
Add catching of mathjax exceptions
Browse files Browse the repository at this point in the history
Closes GH-112.
  • Loading branch information
wooorm committed Feb 20, 2025
1 parent ce40b18 commit 7250ec6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
36 changes: 33 additions & 3 deletions packages/rehype-mathjax/lib/create-plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @import {ElementContent, Element, Root} from 'hast'
* @import {VFile} from 'vfile'
*/

/**
Expand Down Expand Up @@ -210,10 +211,12 @@ export function createPlugin(createRenderer) {
*
* @param {Root} tree
* Tree.
* @param {VFile} file
* File.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
return function (tree, file) {
const renderer = createRenderer(options || emptyOptions)
let found = false
/** @type {Element | Root} */
Expand Down Expand Up @@ -265,11 +268,38 @@ export function createPlugin(createRenderer) {
found = true

const text = toText(scope, {whitespace: 'pre'})
const result = renderer.render(text, {display})
/** @type {Array<ElementContent> | undefined} */
let result

try {
result = renderer.render(text, {display})
} catch (error) {
const cause = /** @type {Error} */ (error)

file.message('Could not render math with mathjax', {
ancestors: [...parents, element],
cause,
place: element.position,
ruleId: 'mathjax-error',
source: 'rehype-mathjax'
})

result = [
{
type: 'element',
tagName: 'span',
properties: {
className: ['mathjax-error'],
style: 'color:#cc0000',
title: String(cause)
},
children: [{type: 'text', value: text}]
}
]
}

const index = parent.children.indexOf(scope)
parent.children.splice(index, 1, ...result)

return SKIP
})

Expand Down
3 changes: 2 additions & 1 deletion packages/rehype-mathjax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"hastscript": "^9.0.0",
"mathjax-full": "^3.0.0",
"unified": "^11.0.0",
"unist-util-visit-parents": "^6.0.0"
"unist-util-visit-parents": "^6.0.0",
"vfile": "^6.0.0"
},
"description": "rehype plugin to transform inline and block math with MathJax",
"exports": {
Expand Down
19 changes: 19 additions & 0 deletions packages/rehype-mathjax/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,23 @@ test('rehype-mathjax', async function (t) {
).trim()
)
})

await t.test('should catch mathjax exceptions', async function () {
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeMathJaxSvg)
.use(rehypeStringify)
.process('<code class=language-math>\\a{₹}</code>.')

const value = String(file).replace(/<style>[\s\S]*<\/style>/, '')

assert.equal(
value,
'<span class="mathjax-error" style="color:#cc0000" title="TypeError: Cannot read properties of null (reading &#x27;4&#x27;)">\\a{₹}</span>.'
)

assert.deepEqual(file.messages.map(String), [
'1:1-1:39: Could not render math with mathjax'
])
})
})

0 comments on commit 7250ec6

Please sign in to comment.