|
| 1 | +import type { Transformer } from 'unified'; |
| 2 | +import type { BlockContent, Heading, Root, RootContent } from 'mdast'; |
| 3 | +import { visit } from 'unist-util-visit'; |
| 4 | +import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx'; |
| 5 | + |
| 6 | +export interface RemarkStepsOptions { |
| 7 | + /** |
| 8 | + * Class name for steps container |
| 9 | + * |
| 10 | + * @defaultValue fd-steps |
| 11 | + */ |
| 12 | + steps?: string; |
| 13 | + |
| 14 | + /** |
| 15 | + * Class name for step container |
| 16 | + * |
| 17 | + * @defaultValue fd-step |
| 18 | + */ |
| 19 | + step?: string; |
| 20 | +} |
| 21 | + |
| 22 | +const StepRegex = /^(\d+)\.\s(.+)$/; |
| 23 | + |
| 24 | +/** |
| 25 | + * Convert headings in the format of `1. Hello World` into steps. |
| 26 | + */ |
| 27 | +export function remarkSteps({ |
| 28 | + steps = 'fd-steps', |
| 29 | + step = 'fd-step', |
| 30 | +}: RemarkStepsOptions = {}): Transformer<Root, Root> { |
| 31 | + function convertToSteps(nodes: RootContent[]): MdxJsxFlowElement { |
| 32 | + const depth = (nodes[0] as Heading).depth; |
| 33 | + const children: MdxJsxFlowElement[] = []; |
| 34 | + |
| 35 | + for (const node of nodes) { |
| 36 | + if (node.type === 'heading' && node.depth === depth) { |
| 37 | + children.push({ |
| 38 | + type: 'mdxJsxFlowElement', |
| 39 | + name: 'div', |
| 40 | + attributes: [ |
| 41 | + { |
| 42 | + type: 'mdxJsxAttribute', |
| 43 | + name: 'className', |
| 44 | + value: step, |
| 45 | + }, |
| 46 | + ], |
| 47 | + children: [node], |
| 48 | + }); |
| 49 | + } else { |
| 50 | + children[children.length - 1].children.push(node as BlockContent); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return { |
| 55 | + type: 'mdxJsxFlowElement', |
| 56 | + name: 'div', |
| 57 | + attributes: [ |
| 58 | + { |
| 59 | + type: 'mdxJsxAttribute', |
| 60 | + name: 'className', |
| 61 | + value: steps, |
| 62 | + }, |
| 63 | + ], |
| 64 | + data: { |
| 65 | + _fd_step: true, |
| 66 | + } as object, |
| 67 | + children, |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + return (tree) => { |
| 72 | + visit(tree, (parent) => { |
| 73 | + if (!('children' in parent) || parent.type === 'heading') return; |
| 74 | + if (parent.data && '_fd_step' in parent.data) return 'skip'; |
| 75 | + |
| 76 | + let startIdx = -1; |
| 77 | + let lastNumber = 0; |
| 78 | + let i = 0; |
| 79 | + |
| 80 | + const onEnd = () => { |
| 81 | + if (startIdx === -1) return; |
| 82 | + // range: start index to i - 1 |
| 83 | + const item = {}; |
| 84 | + const nodes = parent.children.splice( |
| 85 | + startIdx, |
| 86 | + i - startIdx, |
| 87 | + item as RootContent, |
| 88 | + ); |
| 89 | + Object.assign(item, convertToSteps(nodes)); |
| 90 | + i = startIdx + 1; |
| 91 | + startIdx = -1; |
| 92 | + }; |
| 93 | + |
| 94 | + for (; i < parent.children.length; i++) { |
| 95 | + const node = parent.children[i]; |
| 96 | + |
| 97 | + if (node.type !== 'heading') continue; |
| 98 | + if (startIdx !== -1) { |
| 99 | + const startDepth = (parent.children[startIdx] as Heading).depth; |
| 100 | + |
| 101 | + if (node.depth > startDepth) continue; |
| 102 | + else if (node.depth < startDepth) onEnd(); |
| 103 | + } |
| 104 | + |
| 105 | + const head = node.children.filter((c) => c.type === 'text').at(0); |
| 106 | + if (!head) { |
| 107 | + onEnd(); |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + const match = StepRegex.exec(head.value); |
| 112 | + if (!match) { |
| 113 | + onEnd(); |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + const num = Number(match[1]); |
| 118 | + head.value = match[2]; |
| 119 | + |
| 120 | + if (startIdx !== -1 && num !== lastNumber + 1) onEnd(); |
| 121 | + if (startIdx === -1) startIdx = i; |
| 122 | + lastNumber = num; |
| 123 | + } |
| 124 | + |
| 125 | + onEnd(); |
| 126 | + }); |
| 127 | + }; |
| 128 | +} |
0 commit comments