-
Hi Codeimport { unified } from "unified"
import remarkParse from "remark-parse"
import remarkRehype from "remark-rehype"
import rehypeStringify from "rehype-stringify"
export const transformer = async (markdown: string) => {
const processor = unified()
processor.use(remarkParse)
processor.use(remarkRehype, {
allowDangerousHtml: true,
})
processor.use(rehypeStringify, {
allowDangerousCharacters: true,
allowDangerousHtml: true,
})
processor.use(() => {
return (node) => {
console.log(JSON.stringify(node, null, 4)) // No characters are not encoded
}
})
const result = await processor.process(markdown)
console.log(result.value) // Some characters are encoded (and I don't know why!)
return result.value
} Input`bg-radial-<color>`
```js
{
/* <plugin-name> */
}
``` Output<p><code>bg-radial-<color></code></p>
<pre><code class="language-js">{
/* <plugin-name> */
}
</code></pre> |
Beta Was this translation helpful? Give feedback.
Answered by
babakfp
Mar 17, 2024
Replies: 2 comments 8 replies
-
Welcome @babakfp! 👋 Rehype needs to encode the |
Beta Was this translation helpful? Give feedback.
7 replies
-
I think I solved it.
export const STRINGIFY_ENTITIES_DEFAULT_DANGEROUS_CHARACTERS = [
'"',
"&",
"'",
"<",
">",
"`",
] as const
export const STRINGIFY_ENTITIES_DEFAULT_SVELTE_DANGEROUS_CHARACTERS = [
"{",
"}",
] as const
import { stringifyEntities } from "stringify-entities"
import { visit } from "unist-util-visit"
import type { Plugin } from "unified"
import {
STRINGIFY_ENTITIES_DEFAULT_DANGEROUS_CHARACTERS,
STRINGIFY_ENTITIES_DEFAULT_SVELTE_DANGEROUS_CHARACTERS,
} from "./constants.js"
export const rehypeSanitizeMdCode: Plugin = () => {
return (tree) => {
visit(tree, "element", (node) => {
// @ts-ignore
if (node.tagName === "code") {
visit(node, "text", (childNode) => {
// @ts-ignore
childNode.type = "raw"
// @ts-ignore
childNode.value = stringifyEntities(
// @ts-ignore
childNode.value,
{
subset: [
...STRINGIFY_ENTITIES_DEFAULT_DANGEROUS_CHARACTERS,
...STRINGIFY_ENTITIES_DEFAULT_SVELTE_DANGEROUS_CHARACTERS,
],
}
)
})
}
})
}
} Thanks for helping 🌸. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
babakfp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I solved it.
constants.ts
:rehype-sanitize-md-code.ts
: