2022-06-06 16:49:53 +00:00
|
|
|
import { visit } from 'unist-util-visit';
|
2021-11-10 23:34:31 +00:00
|
|
|
|
|
|
|
export default function rehypeEscape(): any {
|
|
|
|
return function (node: any): any {
|
|
|
|
return visit(node, 'element', (el) => {
|
|
|
|
if (el.tagName === 'code' || el.tagName === 'pre') {
|
2022-03-03 17:34:36 +00:00
|
|
|
el.properties['is:raw'] = true;
|
2022-05-31 17:16:43 +00:00
|
|
|
// Visit all raw children and escape HTML tags to prevent Markdown code
|
|
|
|
// like "This is a `<script>` tag" from actually opening a script tag
|
|
|
|
visit(el, 'raw', (raw) => {
|
2022-06-20 19:09:35 +00:00
|
|
|
raw.value = raw.value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
2022-05-31 17:16:43 +00:00
|
|
|
});
|
2021-11-10 23:34:31 +00:00
|
|
|
}
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|