File size: 968 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { h } from "hastscript"
import type { BlockContent, DefinitionContent } from "mdast"
import { visit } from "unist-util-visit"

export function remarkCodeGroup() {
  return (tree: any) => {
    visit(tree, (node) => {
      if (node.type !== "containerDirective") return
      if (node.name !== "code-group") return

      node.data ||= {}
      const data = node.data
      const tagName = "div"

      data.hName = "code-group"
      data.hProperties = h(tagName, node.attributes || {}).properties

      node.children = node.children
        .map((child: any) => {
          const match = "meta" in child && child?.meta?.match(/\[(.*)\]/)
          return {
            type: "paragraph",
            children: [child],
            data: {
              hName: "div",
              hProperties: match ? { "data-title": match[1] } : undefined,
            },
          }
        })
        .filter(Boolean) as (BlockContent | DefinitionContent)[]
    })
  }
}