2024-09-07 07:56:51 +08:00
|
|
|
import type { MarkdownRenderer } from 'vitepress'
|
2022-11-05 11:40:39 +08:00
|
|
|
|
2024-09-07 07:56:51 +08:00
|
|
|
export default (md: MarkdownRenderer): void => {
|
2022-11-05 11:40:39 +08:00
|
|
|
md.renderer.rules.tooltip = (tokens, idx) => {
|
|
|
|
const token = tokens[idx]
|
|
|
|
|
|
|
|
return `<api-typing type="${token.content}" details="${token.info}" />`
|
|
|
|
}
|
|
|
|
|
|
|
|
md.inline.ruler.before('emphasis', 'tooltip', (state, silent) => {
|
2022-11-05 15:15:29 +08:00
|
|
|
const tooltipRegExp = /^\^\[([^\]]*)\](`[^`]*`)?/
|
2022-11-05 11:40:39 +08:00
|
|
|
const str = state.src.slice(state.pos, state.posMax)
|
|
|
|
|
|
|
|
if (!tooltipRegExp.test(str)) return false
|
|
|
|
if (silent) return true
|
|
|
|
|
|
|
|
const result = str.match(tooltipRegExp)
|
|
|
|
|
|
|
|
if (!result) return false
|
|
|
|
|
|
|
|
const token = state.push('tooltip', 'tooltip', 0)
|
2022-11-05 15:15:29 +08:00
|
|
|
token.content = result[1].replace(/\\\|/g, '|')
|
|
|
|
token.info = (result[2] || '').replace(/^`(.*)`$/, '$1')
|
2022-11-05 11:40:39 +08:00
|
|
|
token.level = state.level
|
|
|
|
state.pos += result[0].length
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|