import MdMTables from 'markdown-it-multimd-table'
// @ts-expect-error
import MdReg from 'markdown-it-regexp'
import type { MarkdownRenderer } from 'vitepress'
import { headersPlugin } from '../markdown/headers'
import { figure } from '@mdit/plugin-figure'
import { imgLazyload } from '@mdit/plugin-img-lazyload'
import { align } from '@mdit/plugin-align'
import { imgSize } from '@mdit/plugin-img-size'
import { tabsMarkdownPlugin } from 'vitepress-plugin-tabs'
import { emojiRender } from './emoji'
import { getTooltip } from '../../utils/tooltips'
import { attrs } from '@mdit/plugin-attrs'
export function configureMarkdown(md: MarkdownRenderer) {
md.use(emojiRender)
md.use(imgLazyload)
md.use(align)
md.use(figure)
md.use(tabsMarkdownPlugin)
md.use(imgSize)
md.use(headersPlugin)
md.use(MdMTables, {
multiline: true,
rowspan: true,
headerless: true,
multibody: true,
aotolabel: true
})
md.use(attrs)
renderTooltip(md)
renderInlineTooltip(md)
}
function renderInlineTooltip(md: MarkdownRenderer) {
md.use(
MdReg(
/\^\[(.*?)\]\((.+?)\)/,
([, cont, hint]: string[]) =>
'' +
span(md.renderInline(cont)) +
'' +
md.renderInline(hint) +
''
)
)
}
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
}