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

d7d026e8293de2bfb718df54a2bfd6b4d1c2ae8a
wotaku/docs/.vitepress/configs/markdown/index.ts history · blame · raw

105 lines · 2998 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 markdownSteps from './steps'
 22
 23export function configureMarkdown(md: MarkdownRenderer) {
 24  md.use(emojiRender)
 25  md.use(imgLazyload)
 26  md.use(align)
 27  md.use(figure)
 28  md.use(tabsMarkdownPlugin)
 29  md.use(imgSize)
 30  md.use(headersPlugin)
 31  md.use(MdMTables, {
 32    multiline: true,
 33    rowspan: true,
 34    headerless: true,
 35    multibody: true,
 36    aotolabel: true
 37  })
 38  md.use(attrs)
 39  renderTooltip(md)
 40  renderInlineTooltip(md)
 41  md.use(markdownSteps)
 42  renderHighlight(md)
 43}
 44
 45function renderInlineTooltip(md: MarkdownRenderer) {
 46  md.use(
 47    MdReg(
 48      /\^\[(.*?)\]\((.+?)\)/,
 49      ([, cont, hint]: string[]) =>
 50        '<VTooltip theme="hint">' +
 51        span(md.renderInline(cont)) +
 52        '<template v-slot:popper>' +
 53        md.renderInline(hint) +
 54        '</template></VTooltip>'
 55    )
 56  )
 57}
 58
 59function renderTooltip(md: MarkdownRenderer) {
 60  md.use(
 61    MdReg(/==(.+?)==/, ([, cont]: string[]) => {
 62      const item = getTooltip(cont)
 63      if (!item) return `No tooltip found for ${cont}`
 64
 65      const icon = item.frontmatter.icon
 66        ? `icon="${item.frontmatter.icon}"`
 67        : ''
 68
 69      const title = item.frontmatter.title
 70        ? `title="${item.frontmatter.title}"`
 71        : item.id
 72        ? `title="${item.id}"`
 73        : '' /** Impossible */
 74      const props = icon + title
 75      const renderedContent = md.render(item.content)
 76
 77      return `<Tooltip ${props}>` + renderedContent + '</Tooltip>'
 78    })
 79  )
 80}
 81
 82function span(
 83  content: string,
 84  attrs: Record<string, unknown> | undefined = undefined
 85) {
 86  let html = '<span'
 87  if (attrs) {
 88    for (const [key, value] of Object.entries(attrs)) {
 89      html += ` ${key}="${value}"`
 90    }
 91  }
 92  html += `>${content}</span>`
 93  return html
 94}
 95
 96function renderHighlight(md: MarkdownRenderer) {
 97  const original = md.render.bind(md)
 98  md.render = (src, env) => {
 99    const replaced = src.replace(/([!x]?)\|\|(.+?)\|\|/g, (_, prefix, cont) => {
100      const variant = prefix === '!' ? 'warning' : prefix === 'x' ? 'danger' : 'default'
101      return `<hl variant="${variant}">${cont}</hl>`
102    })
103    return original(replaced, env)
104  }
105}