astro-ghostcms/.pnpm-store/v3/files/fd/0435fc67064520c1bae6e36e0ff...

26 lines
930 B
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
import { visit } from "unist-util-visit";
import { escape, needsEscape, replaceAttribute } from "./utils.js";
const rehypeEscape = ({ s }) => {
return (tree) => {
visit(tree, (node) => {
if (node.type === "text" || node.type === "comment") {
if (needsEscape(node.value)) {
s.overwrite(node.position.start.offset, node.position.end.offset, escape(node.value));
}
} else if (node.type === "element") {
for (const [key, value] of Object.entries(node.properties ?? {})) {
const newKey = needsEscape(key) ? escape(key) : key;
const newValue = needsEscape(value) ? escape(value) : value;
if (newKey === key && newValue === value)
continue;
replaceAttribute(s, node, key, value === "" ? newKey : `${newKey}="${newValue}"`);
}
}
});
};
};
var escape_default = rehypeEscape;
export {
escape_default as default
};