docs/.vitepress/configs/markdown/steps.ts
69 lines · 1823 bytes
2 symbols in this file
1import type { MarkdownRenderer } from 'vitepress'
2
3function getLineText(state: any, line: number) {
4 const startPos = state.bMarks[line] + state.tShift[line]
5 const maxPos = state.eMarks[line]
6 const lineText = state.src.slice(startPos, maxPos)
7
8 return lineText.replace(/:::\s+steps/, ':::steps')
9}
10
11export default function markdownSteps(md: MarkdownRenderer) {
12 md.block.ruler.before(
13 'paragraph',
14 'steps',
15 (state, startLine, endLine, silent) => {
16 const lineText = getLineText(state, startLine)
17
18 if (!lineText.startsWith(':::steps')) return false
19
20 const titleMatch = lineText.slice(8).trim()
21 const hasTitle = titleMatch.length > 0
22
23 if (silent) return true
24
25 let nextLine = startLine + 1
26 let openBlocks = 1
27 let token
28
29 token = state.push('steps_open', 'div', 1)
30 token.block = true
31 token.attrs = [['class', 'steps']]
32
33 if (hasTitle) {
34 token = state.push('paragraph_open', 'p', 1)
35 token.attrs = [['class', 'custom-title']]
36 token = state.push('text', '', 0)
37 token.content = titleMatch
38 token = state.push('paragraph_close', 'p', -1)
39 }
40
41 while (nextLine < endLine) {
42 const nextLineText = getLineText(state, nextLine)
43
44 if (nextLineText.startsWith(':::')) {
45 if (nextLineText === ':::') {
46 openBlocks--
47 if (openBlocks === 0) break
48 } else {
49 openBlocks++
50 }
51 }
52
53 nextLine++
54 }
55
56 state.md.block.tokenize(state, startLine + 1, nextLine)
57
58 token = state.push('steps_close', 'div', -1)
59 token.block = true
60
61 state.line = nextLine + 1
62
63 return true
64 }
65 )
66
67 md.renderer.rules.steps_open = () => '<div class="steps">\n'
68 md.renderer.rules.steps_close = () => '</div>\n'
69}