-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
151 lines (129 loc) · 3.82 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { visit } from 'unist-util-visit'
import type { Paragraph, Root } from 'mdast'
import type { Plugin } from 'unified'
import { encodeSvg } from './encodeSvg'
import { DEFAULT_GITHUB_ICONS } from './icons'
export interface RemarkGitHubAlertsOptions {
/**
* List of markers to match.
* @default ['TIP', 'NOTE', 'IMPORTANT', 'WARNING', 'CAUTION']
*/
markers?: string[] | '*'
/**
* If markers case sensitively on matching.
* @default true
*/
matchCaseSensitive?: boolean
/**
* Custom icons for each marker. The key is the marker name, and the value is the html script represent the icon.
* The key is always lowercase.
*
* @default inline svg icons from GitHub
*/
icons?: Record<string, string>
/**
* Custom titles for each marker. The key is the marker name, and the value is the title.
* The key is always lowercase.
*
* When the title is not specified, the default title is the capitalized marker name.
*/
titles?: Record<string, string>
/**
* Prefix for the class names.
*
* @default 'markdown-alert'
*/
classPrefix?: string
// ^ options from MarkdownItGitHubAlertsOptions
/**
* Whether to ignore the square brackets in the marker.
*
* @default false
*/
ignoreSquareBracket?: boolean
}
function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
const remarkGithubAlerts: Plugin<RemarkGitHubAlertsOptions[], Root> = (
options = {},
) => {
const {
markers = ['TIP', 'NOTE', 'IMPORTANT', 'WARNING', 'CAUTION'],
icons = DEFAULT_GITHUB_ICONS,
matchCaseSensitive = true,
titles = {},
classPrefix = 'markdown-alert',
ignoreSquareBracket = false,
} = options
const markerNameRE = markers === '*' ? '\\w+' : markers.join('|')
const RE = new RegExp(
ignoreSquareBracket
? `^!(${markerNameRE})\\s?`
: `^\\[\\!(${markerNameRE})\\]\\s`,
matchCaseSensitive ? '' : 'i',
)
return (tree) => {
visit(tree, 'blockquote', (node, index, parent) => {
const children = node.children as Paragraph[]
const firstParagraph = children[0]
if (!firstParagraph)
return
let firstContent = firstParagraph.children[0]
if (!firstContent)
return
if (
!('value' in firstContent)
&& 'children' in firstContent
&& firstContent.children[0]
)
firstContent = firstContent.children[0]
if (firstContent.type !== 'text')
return
const match = firstContent.value.match(RE)
if (!match)
return
const type = match[1]?.toLowerCase() as keyof typeof icons
const title = match[2]?.trim() || (titles[type] ?? capitalize(type))
const iconDataUri = `data:image/svg+xml;utf8,${encodeSvg(icons[type])}`
if (index === undefined || !parent)
return
firstContent.value = firstContent.value.slice(match[0].length).trimStart()
node.data = {
hName: 'blockquote',
hProperties: {
class: `${classPrefix} ${classPrefix}-${type}`,
},
}
node.children = [
{
type: 'paragraph',
data: {
hName: 'p',
hProperties: {
class: `${classPrefix}-title`,
},
},
children: [
{
type: 'span' as any,
data: {
hName: 'span',
hProperties: {
class: `octicon octicon-${type}`,
style: `--oct-icon: url("${iconDataUri}")`,
},
},
// 替换为 CSS 方式以兼容 mdx, 不用再安装额外的 rehype-raw 插件
// value: icon
},
{ type: 'text', value: title },
],
},
...node.children,
]
})
return tree
}
}
export default remarkGithubAlerts