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

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

114 lines · 3556 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'
 22import { scrapeTablePlugin } from './scrapeTablePlugin'
 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    // Replace [||text||](url) — linked pill
102    let replaced = src.replace(/\[([!x]?)\|\|(.+?)\|\|\]\((.+?)\)/g, (_, prefix, cont, url) => {
103      const variant = prefix === '!' ? 'warning' : prefix === 'x' ? 'danger' : 'default'
104      const inner = md.renderInline(cont, env)
105      return `<a href="${url}" target="_blank" rel="noopener noreferrer"><hl variant="${variant}" linked="true">${inner}</hl></a>`
106    })
107    // Replace standalone ||text||
108    replaced = replaced.replace(/([!x]?)\|\|(.+?)\|\|/g, (_, prefix, cont) => {
109      const variant = prefix === '!' ? 'warning' : prefix === 'x' ? 'danger' : 'default'
110      return `<hl variant="${variant}">${md.renderInline(cont, env)}</hl>`
111    })
112    return original(replaced, env)
113  }
114}