File size: 1,244 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { h } from "hastscript"
import { Heading } from "mdast"
import { visit } from "unist-util-visit"

export function remarkCard() {
  return (tree: any) => {
    visit(tree, (node) => {
      if (node.type !== "containerDirective") return
      if (node.name !== "card") return

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

      const heading = node.children.find(
        (child: any) => child.type === "heading",
      ) as Heading

      const depth = heading?.depth ?? 2
      node.attributes.depth = depth

      if (heading) {
        const [text] = heading.children
        if (text.type === "text") {
          node.attributes.title = text.value
        }
      }

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

      node.children = node.children.filter(
        (child: any) => child.type !== "heading",
      )
    })

    visit(tree, (node) => {
      if (node.type !== "containerDirective") return
      if (node.name !== "card-group") return

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

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