mirror of
https://github.com/danbulant/design
synced 2026-05-19 04:08:46 +00:00
74 lines
No EOL
2.5 KiB
JavaScript
74 lines
No EOL
2.5 KiB
JavaScript
import { visit } from "unist-util-visit"
|
|
|
|
// Allows having highlight in blog posts.
|
|
|
|
// Very fun, debugging this stuff..
|
|
// Dirty, but it works.
|
|
|
|
// It has the unescape characters because sveltemdx escapes them. It doesn't do that when we use their highlighter - but that
|
|
// one doesn't support inline code.
|
|
// So we have to unescape those characters back to how they were before, but then escape them again because svelte
|
|
// would understand certain characters (namely {}. For some reason, `<>` was fine.) as svelte code.
|
|
|
|
export function remarkUnescapeHighlight() {
|
|
return (tree, file) => {
|
|
visit(tree, ["code"], (node) => {
|
|
node.value = node.value
|
|
.replace(/{/g, "{")
|
|
.replace(/}/g, "}")
|
|
.replace(/>/g, ">")
|
|
.replace(/</g, "<")
|
|
});
|
|
visit(tree, ["inlineCode"], (node) => {
|
|
node.value = node.value
|
|
.replace(/{/g, "{")
|
|
.replace(/}/g, "}")
|
|
.replace(/>/g, ">")
|
|
.replace(/</g, "<")
|
|
});
|
|
}
|
|
}
|
|
|
|
export function remarkEscapeInlineCode() {
|
|
return (tree, file) => {
|
|
visit(tree, ["inlineCode"], (node) => {
|
|
node.value = node.value
|
|
.replace(/{/g, "{")
|
|
.replace(/}/g, "}")
|
|
.replace(/>/g, ">")
|
|
.replace(/</g, "<")
|
|
});
|
|
}
|
|
}
|
|
|
|
export function rehypeEscapeHighlight() {
|
|
function visit(node) {
|
|
if(
|
|
(node.type === "raw" && node.value.startsWith("<pre><code")) ||
|
|
(node.type === "element" && node.tagName === "Components.code") ||
|
|
(node.type === "raw" && node.value.startsWith("<span data-mdx-pretty-code"))) {
|
|
if(node.value) {
|
|
node.value = node.value
|
|
.replace(/{/g, "{")
|
|
.replace(/}/g, "}")
|
|
} else {
|
|
node.children.forEach((child) => {
|
|
if(child.value) {
|
|
child.value = child.value
|
|
.replace(/{/g, "{")
|
|
.replace(/}/g, "}")
|
|
}
|
|
})
|
|
}
|
|
} else if(node.children) node.children.forEach(visit)
|
|
|
|
if (node.type === "raw") {
|
|
node.value = node.value
|
|
.replace(/<span style="color:#ABB2BF"> <\/span>/g , " ");
|
|
}
|
|
}
|
|
|
|
return (tree, file) => {
|
|
visit(tree);
|
|
}
|
|
} |