forked from vuejs-translations/docs-fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headerMdPlugin.ts
57 lines (50 loc) · 1.57 KB
/
headerMdPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* A markdown-it plugin to support custom header metadata
* Headers that end with * are Options API only
* Headers that end with ** are Composition API only
* This plugin strips the markers and augments the extracted header data,
* which can be then used by the theme to filter headers.
*
* TODO: we will likely also need special syntax for preserving the same anchor
* links across translations similar to the one at
* https://github.com/vitejs/docs-cn/tree/main/.vitepress/markdown-it-custom-anchor
*/
import MarkdownIt from 'markdown-it'
import { Header } from 'vitepress'
export interface AugmentedHeader extends Header {
compositionOnly?: boolean
optionsOnly?: boolean
}
export const headerPlugin = (md: MarkdownIt) => {
md.renderer.rules.heading_open = (tokens, i, options, env, self) => {
for (const child of tokens[i + 1].children!) {
if (child.type === 'text' && child.content.endsWith('*')) {
child.content = child.content.replace(/\s*\*+$/, '')
}
}
return self.renderToken(tokens, i, options)
}
const render = md.render
md.render = (content, env) => {
const res = render(content, env)
if (env && env.headers) {
processHeaders(env.headers)
}
return res
}
}
function processHeaders(headers: AugmentedHeader[]) {
for (const h of headers) {
if (h.title.endsWith('*')) {
if (h.title.endsWith('**')) {
h.compositionOnly = true
} else {
h.optionsOnly = true
}
h.title = h.title.replace(/\s*\*+$/, '')
}
if (h.children) {
processHeaders(h.children)
}
}
}