docs/.vitepress/configs/markdown/index.ts

2f464e5a9a3e01dd35ab1b09ef3bf7b6f40933b7
wotaku/docs/.vitepress/configs/markdown/index.ts

160 lines · 4492 bytes

  1/**
  2 *  All Rights Reserved
  3 *
  4 *  Copyright (c) 2025 taskylizard
  5 *
  6 *  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
  7 */
  8import MdMTables from 'markdown-it-multimd-table'
  9// @ts-expect-error
 10import { align } from '@mdit/plugin-align'
 11import { attrs } from '@mdit/plugin-attrs'
 12import { figure } from '@mdit/plugin-figure'
 13import { imgLazyload } from '@mdit/plugin-img-lazyload'
 14import { imgSize } from '@mdit/plugin-img-size'
 15import MdReg from 'markdown-it-regexp'
 16import type { MarkdownRenderer } from 'vitepress'
 17import { tabsMarkdownPlugin } from 'vitepress-plugin-tabs'
 18import { getTooltip } from '../../utils/tooltips'
 19import { headersPlugin } from '../markdown/headers'
 20import { emojiRender } from './emoji'
 21import { scrapeTablePlugin } from './scrapeTablePlugin'
 22import markdownSteps from './steps'
 23
 24export function configureMarkdown(md: MarkdownRenderer) {
 25  md.use(emojiRender)
 26  md.use(imgLazyload)
 27  md.use(align)
 28  md.use(figure)
 29  md.use(tabsMarkdownPlugin)
 30  md.use(imgSize)
 31  md.use(headersPlugin)
 32  md.use(MdMTables, {
 33    multiline: true,
 34    rowspan: true,
 35    headerless: true,
 36    multibody: true,
 37    aotolabel: true
 38  })
 39  md.use(attrs)
 40  renderTooltip(md)
 41  renderInlineTooltip(md)
 42  md.use(markdownSteps)
 43  md.use(scrapeTablePlugin)
 44  renderHighlight(md)
 45}
 46
 47function renderInlineTooltip(md: MarkdownRenderer) {
 48  md.use(
 49    MdReg(
 50      /\^\[(.*?)\]\((.+?)\)/,
 51      ([, cont, hint]: string[]) =>
 52        '<VTooltip theme="hint">' +
 53        span(md.renderInline(cont)) +
 54        '<template v-slot:popper>' +
 55        md.renderInline(hint) +
 56        '</template></VTooltip>'
 57    )
 58  )
 59}
 60
 61function renderTooltip(md: MarkdownRenderer) {
 62  md.use(
 63    MdReg(/==(.+?)==/, ([, cont]: string[]) => {
 64      const item = getTooltip(cont)
 65      if (!item) return `No tooltip found for ${cont}`
 66
 67      const icon = item.frontmatter.icon
 68        ? `icon="${item.frontmatter.icon}"`
 69        : ''
 70
 71      const title = item.frontmatter.title
 72        ? `title="${item.frontmatter.title}"`
 73        : item.id
 74        ? `title="${item.id}"`
 75        : '' /** Impossible */
 76      const props = icon + title
 77      const renderedContent = md.render(item.content)
 78
 79      return `<Tooltip ${props}>` + renderedContent + '</Tooltip>'
 80    })
 81  )
 82}
 83
 84function span(
 85  content: string,
 86  attrs: Record<string, unknown> | undefined = undefined
 87) {
 88  let html = '<span'
 89  if (attrs) {
 90    for (const [key, value] of Object.entries(attrs)) {
 91      html += ` ${key}="${value}"`
 92    }
 93  }
 94  html += `>${content}</span>`
 95  return html
 96}
 97
 98function renderHighlight(md: MarkdownRenderer) {
 99  const original = md.render.bind(md)
100  md.render = (src, env) => {
101    const withCollapsibleHeadings = injectCollapsibleSearchHeadings(src)
102
103    // Replace [||text||](url) — linked pill
104    let replaced = withCollapsibleHeadings.replace(
105      /\[([!x]?)\|\|(.+?)\|\|\]\((.+?)\)/g,
106      (_, prefix, cont, url) => {
107        const variant = prefix === '!'
108          ? 'warning'
109          : prefix === 'x'
110          ? 'danger'
111          : 'default'
112        const inner = md.renderInline(cont, env)
113        return `<a href="${url}" target="_blank" rel="noopener noreferrer"><hl variant="${variant}" linked="true">${inner}</hl></a>`
114      }
115    )
116    // Replace standalone ||text||
117    replaced = replaced.replace(/([!x]?)\|\|(.+?)\|\|/g, (_, prefix, cont) => {
118      const variant = prefix === '!'
119        ? 'warning'
120        : prefix === 'x'
121        ? 'danger'
122        : 'default'
123      return `<hl variant="${variant}">${md.renderInline(cont, env)}</hl>`
124    })
125    return original(replaced, env)
126  }
127}
128
129function injectCollapsibleSearchHeadings(src: string) {
130  const lines = src.split('\n')
131
132  for (let i = 0; i < lines.length; i++) {
133    const match = lines[i].match(
134      /^(\s*)<Collapsible\b(?=[^>]*\btitle=(['"])(.*?)\2)[^>]*>\s*$/i
135    )
136    if (!match) continue
137
138    let nextLine = i + 1
139    while (nextLine < lines.length && lines[nextLine].trim() === '') {
140      nextLine++
141    }
142
143    if (/^#{1,6}\s+/.test(lines[nextLine]?.trim() ?? '')) continue
144
145    const [, indent, , title] = match
146    const headingTitle = title.trim()
147    if (!headingTitle) continue
148
149    lines.splice(
150      i + 1,
151      0,
152      '',
153      `${indent}### ${headingTitle} {.collapsible-search-heading}`,
154      ''
155    )
156    i += 3
157  }
158
159  return lines.join('\n')
160}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form