File size: 1,352 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
51
52
53
import { h } from "hastscript"
import type { Heading } from "mdast"
import { visit } from "unist-util-visit"

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

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

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

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

      const depth = heading?.depth ?? 2

      let currentChild: any
      const children: any[] = []

      node.children.forEach((child: any) => {
        if (child.type === "heading" && child.depth === depth) {
          if (currentChild && currentChild.children.length > 0) {
            children.push(currentChild)
          }

          currentChild = {
            type: "paragraph",
            children: [],
            data: {
              hName: "step",
              hProperties: {
                "data-index": children.length,
                "data-depth": depth,
              },
            },
          } as any
        }

        currentChild!.children.push(child)
      })

      children.push(currentChild)
      node.children = children
    })
  }
}