Skip to content

Commit b65a156

Browse files
wooormnmanumr
andcommitted
Fix support for comments
Closes GH-61. Closes GH-62. Co-authored-by: Nauman Umer <[email protected]>
1 parent 1809416 commit b65a156

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

packages/rehype-katex/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function rehypeKatex(options) {
3939
return
4040
}
4141

42-
const value = toText(element)
42+
const value = toText(element, {whitespace: 'pre'})
4343

4444
/** @type {string} */
4545
let result

packages/rehype-katex/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dependencies": {
4141
"@types/hast": "^2.0.0",
4242
"@types/katex": "^0.11.0",
43-
"hast-util-to-text": "^3.0.0",
43+
"hast-util-to-text": "^3.1.0",
4444
"katex": "^0.13.0",
4545
"rehype-parse": "^8.0.0",
4646
"unified": "^10.0.0",

packages/rehype-katex/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,29 @@ test('rehype-katex', (t) => {
189189
'should support `strict: ignore`'
190190
)
191191

192+
t.deepEqual(
193+
unified()
194+
.use(rehypeParse, {fragment: true})
195+
.use(rehypeKatex, {errorColor: 'orange', strict: 'ignore'})
196+
.use(rehypeStringify)
197+
.processSync(
198+
'<div class="math math-display">\\begin{split}\n f(-2) &= \\sqrt{-2+4} \\\\\n &= x % Test Comment\n\\end{split}</div>'
199+
)
200+
.toString(),
201+
unified()
202+
.use(rehypeParse, {fragment: true})
203+
.use(rehypeStringify)
204+
.processSync(
205+
'<div class="math math-display">' +
206+
katex.renderToString(
207+
'\\begin{split}\n f(-2) &= \\sqrt{-2+4} \\\\\n &= x % Test Comment\n\\end{split}',
208+
{displayMode: true}
209+
) +
210+
'</div>'
211+
)
212+
.toString(),
213+
'should support comments'
214+
)
215+
192216
t.end()
193217
})

packages/rehype-mathjax/lib/create-renderer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export function createRenderer(options, output) {
4242

4343
return {
4444
render(node, options) {
45-
// @ts-expect-error: assume mathml nodes can be handled by
46-
// `hast-util-from-dom`.
47-
const domNode = fromDom(doc.convert(toText(node), options))
45+
const domNode = fromDom(
46+
// @ts-expect-error: assume mathml nodes can be handled by
47+
// `hast-util-from-dom`.
48+
doc.convert(toText(node, {whitespace: 'pre'}), options)
49+
)
4850
// @ts-expect-error: `fromDom` returns an element for a given element.
4951
node.children = [domNode]
5052
},

packages/rehype-mathjax/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@types/mathjax": "^0.0.36",
5353
"@types/web": "^0.0.15",
5454
"hast-util-from-dom": "^4.0.0",
55-
"hast-util-to-text": "^3.0.0",
55+
"hast-util-to-text": "^3.1.0",
5656
"jsdom": "^16.0.0",
5757
"mathjax-full": "^3.0.0",
5858
"unified": "^10.0.0",

0 commit comments

Comments
 (0)