48e67fe053
* encode ampersands in markdown code blocks * chore: add changeset * nit: fixing test case description
17 lines
575 B
TypeScript
17 lines
575 B
TypeScript
import { visit } from 'unist-util-visit';
|
|
|
|
export default function rehypeEscape(): any {
|
|
return function (node: any): any {
|
|
return visit(node, 'element', (el) => {
|
|
if (el.tagName === 'code' || el.tagName === 'pre') {
|
|
el.properties['is:raw'] = true;
|
|
// 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) => {
|
|
raw.value = raw.value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
});
|
|
}
|
|
return el;
|
|
});
|
|
};
|
|
}
|