docs/.vitepress/configs/markdown/nestedContainers.ts

9c6588973e856dbc27ebb283f0d963c792faa8df
wotaku/docs/.vitepress/configs/markdown/nestedContainers.ts history · blame · raw

153 lines · 3962 bytes

  1import type { MarkdownRenderer } from 'vitepress'
  2
  3const MARKER = ':'
  4const MIN_MARKERS = 3
  5
  6const NESTABLE_CONTAINER_NAMES = new Set([
  7  'tabs',
  8  'tip',
  9  'info',
 10  'warning',
 11  'danger',
 12  'details',
 13  'scrapetable',
 14  'steps',
 15  'left',
 16  'center',
 17  'right',
 18  'justify',
 19  'v-pre',
 20  'raw',
 21  'code-group'
 22])
 23
 24const OVERRIDDEN_CONTAINER_NAMES = [
 25  'tabs',
 26  'tip',
 27  'info',
 28  'warning',
 29  'danger',
 30  'details',
 31  'left',
 32  'center',
 33  'right',
 34  'justify',
 35  'v-pre',
 36  'raw',
 37  'code-group'
 38]
 39
 40function parseColonFence(state: any, line: number) {
 41  let pos = state.bMarks[line] + state.tShift[line]
 42  const max = state.eMarks[line]
 43
 44  if (state.src[pos] !== MARKER) return null
 45
 46  const start = pos
 47  while (pos < max && state.src[pos] === MARKER) pos++
 48
 49  const markerCount = pos - start
 50  if (markerCount < MIN_MARKERS) return null
 51
 52  const markup = state.src.slice(start, pos)
 53  const params = state.src.slice(pos, max)
 54  const trimmedParams = params.trim()
 55  const name = trimmedParams.match(/^([A-Za-z][\w-]*)\b/)?.[1]?.toLowerCase()
 56
 57  return { markerCount, markup, name, params, trimmedParams }
 58}
 59
 60function isFenceClose(fence: ReturnType<typeof parseColonFence>) {
 61  return fence !== null && fence.trimmedParams === ''
 62}
 63
 64function isNestableFenceOpen(fence: ReturnType<typeof parseColonFence>) {
 65  return fence !== null &&
 66    fence.name !== undefined &&
 67    NESTABLE_CONTAINER_NAMES.has(fence.name)
 68}
 69
 70function createNestedContainerRule(name: string) {
 71  return (state: any, startLine: number, endLine: number, silent: boolean) => {
 72    const openingFence = parseColonFence(state, startLine)
 73    if (!openingFence) return false
 74
 75    const paramsName = openingFence.trimmedParams
 76      .split(/\s+/, 1)[0]
 77      ?.toLowerCase()
 78    if (paramsName !== name) return false
 79
 80    if (silent) return true
 81
 82    let nextLine = startLine
 83    let autoClosed = false
 84    const markerStack = [openingFence.markerCount]
 85
 86    for (;;) {
 87      nextLine++
 88      if (nextLine >= endLine) break
 89
 90      const start = state.bMarks[nextLine] + state.tShift[nextLine]
 91      const max = state.eMarks[nextLine]
 92
 93      if (start < max && state.sCount[nextLine] < state.blkIndent) break
 94      if (state.sCount[nextLine] - state.blkIndent >= 4) continue
 95
 96      const fence = parseColonFence(state, nextLine)
 97      if (!fence) continue
 98
 99      if (isFenceClose(fence)) {
100        const currentMarkerCount = markerStack[markerStack.length - 1]
101        if (fence.markerCount < currentMarkerCount) continue
102
103        markerStack.pop()
104        if (markerStack.length === 0) {
105          autoClosed = true
106          break
107        }
108        continue
109      }
110
111      if (isNestableFenceOpen(fence)) markerStack.push(fence.markerCount)
112    }
113
114    const oldParent = state.parentType
115    const oldLineMax = state.lineMax
116
117    state.parentType = 'container'
118    state.lineMax = nextLine
119
120    const tokenOpen = state.push(`container_${name}_open`, 'div', 1)
121    tokenOpen.markup = openingFence.markup
122    tokenOpen.block = true
123    tokenOpen.info = openingFence.params
124    tokenOpen.map = [startLine, nextLine]
125
126    state.md.block.tokenize(state, startLine + 1, nextLine)
127
128    const tokenClose = state.push(`container_${name}_close`, 'div', -1)
129    tokenClose.markup = autoClosed
130      ? parseColonFence(state, nextLine)?.markup ?? openingFence.markup
131      : openingFence.markup
132    tokenClose.block = true
133
134    state.parentType = oldParent
135    state.lineMax = oldLineMax
136    state.line = nextLine + (autoClosed ? 1 : 0)
137
138    return true
139  }
140}
141
142export function nestedContainersPlugin(md: MarkdownRenderer) {
143  for (const name of OVERRIDDEN_CONTAINER_NAMES) {
144    try {
145      md.block.ruler.at(`container_${name}`, createNestedContainerRule(name), {
146        alt: ['paragraph', 'reference', 'blockquote', 'list']
147      })
148    } catch {
149      // Some containers are provided by optional plugins. Skip the ones that
150      // are not registered in the current markdown-it instance.
151    }
152  }
153}