0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00
astro/packages/markdown/remark/src/codeblock.ts
2021-10-22 16:25:36 -06:00

44 lines
1.4 KiB
TypeScript

import { visit } from 'unist-util-visit';
import type { Element, Root as HastRoot, Properties } from 'hast';
import type { Root as MdastRoot } from 'mdast';
/** */
export function remarkCodeBlock() {
return function (tree: MdastRoot) {
visit(tree, 'code', (node) => {
const { data, meta } = node;
let lang = node.lang || 'html'; // default to html to match GFM behavior.
let currentClassName = (data?.hProperties as Properties)?.class ?? '';
node.data = node.data || {};
node.data.hProperties = node.data.hProperties || {};
node.data.hProperties = { ...(node.data.hProperties as Properties), class: `language-${lang} ${currentClassName}`.trim(), lang, meta };
});
};
}
/** */
export function rehypeCodeBlock() {
return function (tree: HastRoot) {
const escapeCode = (code: Element): void => {
code.children = code.children.map((child) => {
if (child.type === 'text') {
return { ...child, value: `{\`${child.value.replace(/\$\{/g, '\\$\\{').replace(/`/g, '\\`')}\`}` };
}
return child;
});
};
visit(tree, 'element', (node) => {
if (node.tagName === 'code') {
escapeCode(node);
return;
}
if (node.tagName !== 'pre') return;
if (!node.children[0]) return;
const code = node.children[0];
if (code.type !== 'element' || code.tagName !== 'code') return;
node.properties = { ...code.properties };
});
};
}