File size: 1,320 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 { visit } from "unist-util-visit"

export function remarkCallout() {
  return (tree: any) => {
    visit(tree, (node) => {
      if (node.type !== "containerDirective") return
      if (
        node.name !== "callout" &&
        node.name !== "info" &&
        node.name !== "warning" &&
        node.name !== "danger" &&
        node.name !== "tip" &&
        node.name !== "success" &&
        node.name !== "note"
      )
        return

      // @ts-expect-error
      const label = node.children.find((child) => child.data?.directiveLabel)
        ?.children[0].value

      const data = node.data || (node.data = {})
      const tagName = "callout"

      if (label) {
        node.children = node.children.filter(
          (child: any) => !child.data?.directiveLabel,
        )
        node.children.unshift({
          type: "paragraph",
          data: { hProperties: { "data-callout-title": true } },
          children: [
            {
              type: "strong",
              children: [{ type: "text", value: label }],
            },
          ],
        })
      }

      data.hName = tagName
      data.hProperties = {
        ...h(tagName, node.attributes || {}).properties,
        "data-type": node.name !== "callout" ? node.name : true,
      }
    })
  }
}