Skip to content

Commit

Permalink
Fix support for comments
Browse files Browse the repository at this point in the history
Closes GH-61.
Closes GH-62.

Co-authored-by: Nauman Umer <nmanumr@gmail.com>
  • Loading branch information
wooorm and nmanumr committed Aug 23, 2021
1 parent 1809416 commit b65a156
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/rehype-katex/index.js
Expand Up @@ -39,7 +39,7 @@ export default function rehypeKatex(options) {
return
}

const value = toText(element)
const value = toText(element, {whitespace: 'pre'})

/** @type {string} */
let result
Expand Down
2 changes: 1 addition & 1 deletion packages/rehype-katex/package.json
Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"@types/hast": "^2.0.0",
"@types/katex": "^0.11.0",
"hast-util-to-text": "^3.0.0",
"hast-util-to-text": "^3.1.0",
"katex": "^0.13.0",
"rehype-parse": "^8.0.0",
"unified": "^10.0.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/rehype-katex/test.js
Expand Up @@ -189,5 +189,29 @@ test('rehype-katex', (t) => {
'should support `strict: ignore`'
)

t.deepEqual(
unified()
.use(rehypeParse, {fragment: true})
.use(rehypeKatex, {errorColor: 'orange', strict: 'ignore'})
.use(rehypeStringify)
.processSync(
'<div class="math math-display">\\begin{split}\n f(-2) &= \\sqrt{-2+4} \\\\\n &= x % Test Comment\n\\end{split}</div>'
)
.toString(),
unified()
.use(rehypeParse, {fragment: true})
.use(rehypeStringify)
.processSync(
'<div class="math math-display">' +
katex.renderToString(
'\\begin{split}\n f(-2) &= \\sqrt{-2+4} \\\\\n &= x % Test Comment\n\\end{split}',
{displayMode: true}
) +
'</div>'
)
.toString(),
'should support comments'
)

t.end()
})
8 changes: 5 additions & 3 deletions packages/rehype-mathjax/lib/create-renderer.js
Expand Up @@ -42,9 +42,11 @@ export function createRenderer(options, output) {

return {
render(node, options) {
// @ts-expect-error: assume mathml nodes can be handled by
// `hast-util-from-dom`.
const domNode = fromDom(doc.convert(toText(node), options))
const domNode = fromDom(
// @ts-expect-error: assume mathml nodes can be handled by
// `hast-util-from-dom`.
doc.convert(toText(node, {whitespace: 'pre'}), options)
)
// @ts-expect-error: `fromDom` returns an element for a given element.
node.children = [domNode]
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rehype-mathjax/package.json
Expand Up @@ -52,7 +52,7 @@
"@types/mathjax": "^0.0.36",
"@types/web": "^0.0.15",
"hast-util-from-dom": "^4.0.0",
"hast-util-to-text": "^3.0.0",
"hast-util-to-text": "^3.1.0",
"jsdom": "^16.0.0",
"mathjax-full": "^3.0.0",
"unified": "^10.0.0",
Expand Down

0 comments on commit b65a156

Please sign in to comment.