Skip to content

Commit e28098a

Browse files
committed
Fix to catch non-parse katex errors
Closes GH-82.
1 parent a323406 commit e28098a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/rehype-katex/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ export default function rehypeKatex(options) {
5656

5757
file[fn](error.message, element.position, origin)
5858

59+
// KaTeX can handle `ParseError` itself, but not others.
60+
// Generate similar markup if this is an other error.
61+
// See: <https://github.com/KaTeX/KaTeX/blob/5dc7af0/docs/error.md>.
62+
if (error.name !== 'ParseError') {
63+
element.children = [
64+
{
65+
type: 'element',
66+
tagName: 'span',
67+
properties: {
68+
className: ['katex-error'],
69+
title: String(error),
70+
style: 'color:' + (settings.errorColor || '#cc0000')
71+
},
72+
children: [{type: 'text', value}]
73+
}
74+
]
75+
return
76+
}
77+
5978
result = katex.renderToString(
6079
value,
6180
assign({}, settings, {

packages/rehype-katex/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,24 @@ test('rehype-katex', (t) => {
214214
'should support comments'
215215
)
216216

217+
t.deepEqual(
218+
unified()
219+
.use(rehypeParse, {fragment: true})
220+
.use(rehypeKatex)
221+
.use(rehypeStringify)
222+
.processSync(
223+
'<span class="math-display">\\begin{split}\n\\end{{split}}\n</span>'
224+
)
225+
.toString(),
226+
unified()
227+
.use(rehypeParse, {fragment: true})
228+
.use(rehypeStringify)
229+
.processSync(
230+
'<span class="math-display"><span class="katex-error" title="Error: Expected node of type textord, but got node of type ordgroup" style="color:#cc0000">\\begin{split}\n\\end{{split}}\n</span></span>'
231+
)
232+
.toString(),
233+
'should not crash on non-parse errors'
234+
)
235+
217236
t.end()
218237
})

0 commit comments

Comments
 (0)