summaryrefslogtreecommitdiff
path: root/packages/markdown-support/src/codeblock.ts
blob: 32adc0151c339ad0620bbe77b209f1040213228f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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, 'ASTRO_ESCAPED_LEFT_CURLY_BRACKET\0') };
        }
        return child;
      });
    };
    visit(tree, 'element', (node) => {
      if (node.tagName === 'code') {
        escapeCode(node);
        return;
      }

      if (node.tagName !== 'pre') return;
      const code = node.children[0];
      if (code.type !== 'element' || code.tagName !== 'code') return;
      node.properties = { ...code.properties };
    });
  };
}