Unable to use remark-math
with rehype-remark
?
#241
-
Hello, Given the following Markdown: const test1 = await unified()
.use(remarkParse)
.use(remarkMath)
.use(() => {
return (tree) => {
console.log('mdast1:');
console.dir(tree, { depth: null });
};
})
.use(remarkStringify)
.process(`Hello $C_{L}$ you.`); The {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'Hello '
},
{
type: 'inlineMath',
value: 'C_{L}',
data: {
hName: 'code',
hProperties: { className: [ 'language-math', 'math-inline' ] },
hChildren: [ { type: 'text', value: 'C_{L}' } ]
}
},
{
type: 'text',
value: ' you.'
}
]
}
]
} and the resulting markdown is as expected: Hello $C_{L}$ you. Now trying the same with const test2 = await unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkMath)
.use(() => {
return (tree) => {
console.log('mdast2:');
console.dir(tree, { depth: null });
};
})
.use(remarkStringify)
.process(`Hello $C_{L}$ you.`); The {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'Hello $C_{L}$ you.'
}
]
}
]
} And the resulting markdown is not what I want (it has escape characters that break the maths): Hello \$C\_{L}\$ you. And without const test2 = await unified()
.use(rehypeParse)
.use(rehypeRemark)
// .use(remarkMath)
.use(() => {
return (tree) => {
console.log('mdast3:');
console.dir(tree, { depth: null });
};
})
.use(remarkStringify)
.process(`Hello $C_{L}$ you.`); {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'Hello $C_{L}$ you.'
}
]
}
]
} The Markdown: Hello $C\_{L}$ you. I'm not sure why Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Welcome @dmca-glasgow! 👋
|
Beta Was this translation helpful? Give feedback.
If you want math to be left alone, you may want to hook in even sooner, like the
unifiedLatexToHast
to prevent it from being converted to HTML.It sounds like you are ending up with a
text
node, which is correctly escaping special characters.You probably want to make it an
inlineMath
MDAST node and includeremark-math
, which also includes a serializer to takemath
andinlineMath
MDAST nodes back to markdown (with Latex appropriate escapes only).