/** * All Rights Reserved * * Copyright (c) 2025 taskylizard * * 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. */ import MdMTables from 'markdown-it-multimd-table' // @ts-expect-error import { align } from '@mdit/plugin-align' import { attrs } from '@mdit/plugin-attrs' import { figure } from '@mdit/plugin-figure' import { imgLazyload } from '@mdit/plugin-img-lazyload' import { imgSize } from '@mdit/plugin-img-size' import MdReg from 'markdown-it-regexp' import type { MarkdownRenderer } from 'vitepress' import { getMirror } from '../../utils/mirrors' import { getCollapsibleHeadingAnchor, getTabAnchor, getTabHeadingAnchor, getUniqueAnchor, parseTabLabel } from '../../utils/tabAnchors' import { getTooltip } from '../../utils/tooltips' import { headersPlugin } from '../markdown/headers' import { emojiRender } from './emoji' import { extRepoPlugin } from './extRepoPlugin' import { nestedContainersPlugin } from './nestedContainers' import { scrapeTablePlugin } from './scrapeTablePlugin' import markdownSteps from './steps' import { tableMorePlugin } from './tableMore' import { tabsMarkdownPlugin } from './tabs' import { themeRepoPlugin } from './themeRepoPlugin' export function configureMarkdown(md: MarkdownRenderer) { md.use(emojiRender) md.use(imgLazyload) md.use(align) md.use(figure) md.use(tabsMarkdownPlugin) md.use(nestedContainersPlugin) md.use(imgSize) md.use(headersPlugin) md.use(MdMTables, { multiline: true, rowspan: true, headerless: true, multibody: true, aotolabel: true }) md.use(attrs) renderMirrorTooltip(md) renderLibrarySize(md) renderTooltip(md) renderInlineTooltip(md) md.use(markdownSteps) md.use(tableMorePlugin) md.use(scrapeTablePlugin) md.use(extRepoPlugin) md.use(themeRepoPlugin) renderHighlight(md) } function renderLibrarySize(md: MarkdownRenderer) { md.use( MdReg(/==l:(.+?)==/, ([, name]: string[]) => { const site = name.trim().replace(/"/g, '"') return `` }) ) } function renderInlineTooltip(md: MarkdownRenderer) { md.use( MdReg( /\^\[(.*?)\]\((.+?)\)/, ([, cont, hint]: string[]) => '' + span(md.renderInline(cont)) + '' ) ) } function renderMirrorTooltip(md: MarkdownRenderer) { md.use( MdReg(/==m:(.+?)==/, ([, cont]: string[]) => { const id = cont.trim() if (/^https?:\/\/\S+$/i.test(id)) { const safeUrl = id.replace(/"/g, '"') return `` } const item = getMirror(id) if (!item) return `No mirror found for ${id}` const title = (item.title || item.id).replace(/"/g, '"') const src = (item.src || '').replace(/"/g, '"') const note = (item.note || '').replace(/"/g, '"') const mirrorsAttr = JSON.stringify(item.mirrors).replace(/'/g, ''') return `` }) ) } function renderTooltip(md: MarkdownRenderer) { md.use( MdReg(/==(.+?)==/, ([, cont]: string[]) => { const item = getTooltip(cont) if (!item) return `No tooltip found for ${cont}` const icon = item.frontmatter.icon ? `icon="${item.frontmatter.icon}"` : '' const title = item.frontmatter.title ? `title="${item.frontmatter.title}"` : item.id ? `title="${item.id}"` : '' /** Impossible */ const props = icon + title const renderedContent = md.render(item.content) return `` + renderedContent + '' }) ) } function span( content: string, attrs: Record | undefined = undefined ) { let html = '${content}` return html } function renderHighlight(md: MarkdownRenderer) { const original = md.render.bind(md) md.render = (src, env) => { const withSearchHeadings = injectSearchHeadings(src) // Replace [||text||](url) — linked pill let replaced = withSearchHeadings.replace( /\[([!x]?)\|\|(.+?)\|\|\]\((.+?)\)/g, (_, prefix, cont, url) => { const variant = prefix === '!' ? 'warning' : prefix === 'x' ? 'danger' : 'default' const inner = md.renderInline(cont, env) return `${inner}` } ) // Replace standalone ||text|| replaced = replaced.replace(/([!x]?)\|\|(.+?)\|\|/g, (_, prefix, cont) => { const variant = prefix === '!' ? 'warning' : prefix === 'x' ? 'danger' : 'default' return `${md.renderInline(cont, env)}` }) return original(replaced, env) } } interface SearchHeading { level: number title: string } function getHeadingTitle(value: string) { return value.replace(/\s+\{[^}]*\}\s*$/, '').trim() } function injectSearchHeadings(src: string) { const lines = src.split('\n') const containerStack: string[] = [] const headingStack: (SearchHeading | undefined)[] = [] const tabResetStack: (SearchHeading | undefined)[] = [] const tabAnchorCounts = new Map() const collapsibleAnchorCounts = new Map() const getCurrentHeading = () => { for (let i = headingStack.length - 1; i >= 0; i--) { if (headingStack[i]) return headingStack[i] } } const isHeadingLine = (line: string | undefined) => /^#{1,6}\s+/.test(line?.trim() ?? '') for (let i = 0; i < lines.length; i++) { const headingMatch = lines[i].match(/^(#{1,6})\s+(.+)/) if (headingMatch && !containerStack.includes('tabs')) { const level = headingMatch[1].length const title = getHeadingTitle(headingMatch[2]) if (title) { headingStack[level - 1] = { level, title } headingStack.length = level } } const containerOpenMatch = lines[i].match( /^\s*:{3,}\s*([A-Za-z][\w-]*)\b/ ) if (containerOpenMatch) { const containerName = containerOpenMatch[1].toLowerCase() containerStack.push(containerName) if (containerName === 'tabs') tabResetStack.push(getCurrentHeading()) } else if (/^\s*:{3,}\s*$/.test(lines[i])) { const closed = containerStack.pop() if (closed === 'tabs') { const resetHeading = tabResetStack.pop() let nextLine = i + 1 while (nextLine < lines.length && lines[nextLine].trim() === '') { nextLine++ } if ( resetHeading && nextLine < lines.length && !isHeadingLine(lines[nextLine]) ) { const indent = lines[i].match(/^(\s*):{3,}\s*$/)?.[1] ?? '' lines.splice( i + 1, 0, '', `${indent}${ '#'.repeat(resetHeading.level) } ${resetHeading.title} {.tab-search-heading .ignore-header}`, '' ) i += 3 } } continue } const tabMatch = lines[i].match(/^(\s*)==\s+(.+)$/) if (tabMatch && containerStack.includes('tabs')) { const [, indent, title] = tabMatch const parsed = parseTabLabel(title) const headingTitle = parsed.label if (!headingTitle) continue const anchor = getTabAnchor(title, tabAnchorCounts) const headingAnchor = getTabHeadingAnchor(anchor) lines.splice( i + 1, 0, '', `${indent}### ${headingTitle} {#${headingAnchor} .tab-search-heading .ignore-header}`, '' ) i += 3 continue } const match = lines[i].match( /^(\s*)]*\btitle=(['"])(.*?)\2)[^>]*>\s*$/i ) if (!match) continue const [, indent, , rawTitle] = match const parsedTitle = parseTabLabel(rawTitle) const headingTitle = parsedTitle.label if (!headingTitle) continue const collapsibleAnchor = parsedTitle.anchor || getUniqueAnchor(headingTitle, collapsibleAnchorCounts) lines[i] = lines[i].replace( /(]*?\btitle=)(['"]).*?\2/i, (_full, pre: string, quote: string) => `${pre}${quote}${headingTitle}${quote} anchor=${quote}${collapsibleAnchor}${quote}` ) let nextLine = i + 1 while (nextLine < lines.length && lines[nextLine].trim() === '') { nextLine++ } if (/^#{1,6}\s+/.test(lines[nextLine]?.trim() ?? '')) continue const headingLevel = containerStack.includes('tabs') ? 4 : 3 lines.splice( i + 1, 0, '', `${indent}${'#'.repeat(headingLevel)} ${headingTitle} {#${ getCollapsibleHeadingAnchor(collapsibleAnchor) } .collapsible-search-heading}`, '' ) i += 3 } return lines.join('\n') }