diff --git a/packages/rehype-katex/index.js b/packages/rehype-katex/index.js index 5577798..58847e0 100644 --- a/packages/rehype-katex/index.js +++ b/packages/rehype-katex/index.js @@ -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: . + 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, { diff --git a/packages/rehype-katex/test.js b/packages/rehype-katex/test.js index 5316bd7..608109e 100644 --- a/packages/rehype-katex/test.js +++ b/packages/rehype-katex/test.js @@ -214,5 +214,24 @@ test('rehype-katex', (t) => { 'should support comments' ) + t.deepEqual( + unified() + .use(rehypeParse, {fragment: true}) + .use(rehypeKatex) + .use(rehypeStringify) + .processSync( + '\\begin{split}\n\\end{{split}}\n' + ) + .toString(), + unified() + .use(rehypeParse, {fragment: true}) + .use(rehypeStringify) + .processSync( + '\\begin{split}\n\\end{{split}}\n' + ) + .toString(), + 'should not crash on non-parse errors' + ) + t.end() })