Skip to content

Commit

Permalink
Fix to catch non-parse katex errors
Browse files Browse the repository at this point in the history
Closes GH-82.
  • Loading branch information
wooorm committed Apr 23, 2023
1 parent a323406 commit e28098a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/rehype-katex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ export default function rehypeKatex(options) {

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

// KaTeX can handle `ParseError` itself, but not others.
// Generate similar markup if this is an other error.
// See: <https://github.com/KaTeX/KaTeX/blob/5dc7af0/docs/error.md>.
if (error.name !== 'ParseError') {
element.children = [
{
type: 'element',
tagName: 'span',
properties: {
className: ['katex-error'],
title: String(error),
style: 'color:' + (settings.errorColor || '#cc0000')
},
children: [{type: 'text', value}]
}
]
return
}

result = katex.renderToString(
value,
assign({}, settings, {
Expand Down
19 changes: 19 additions & 0 deletions packages/rehype-katex/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,24 @@ test('rehype-katex', (t) => {
'should support comments'
)

t.deepEqual(
unified()
.use(rehypeParse, {fragment: true})
.use(rehypeKatex)
.use(rehypeStringify)
.processSync(
'<span class="math-display">\\begin{split}\n\\end{{split}}\n</span>'
)
.toString(),
unified()
.use(rehypeParse, {fragment: true})
.use(rehypeStringify)
.processSync(
'<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>'
)
.toString(),
'should not crash on non-parse errors'
)

t.end()
})

0 comments on commit e28098a

Please sign in to comment.