-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (134 loc) · 4.13 KB
/
index.js
File metadata and controls
175 lines (134 loc) · 4.13 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const fs = require('fs')
const MarkdownIt = require('markdown-it')
/**
* TODO: 使用 markdown-it-multimd-table 插件识别表格,使用 markdown-it-emoji 插件识别emoji
*/
const md = new MarkdownIt()
// 识别at([@xxx@])语法
md.core.ruler.push('at', (state) => {
const tokens = state.tokens
const inlines = tokens.filter((token) => token.type === 'inline')
if (inlines.length === 0) return false
inlines.forEach((inline) => {
const children = Array.from(inline.children)
if (children.length === 0) return false
const history = []
const calcOffset = (index) => {
// 输入原始index,计算offset后的index
for (const item of history) {
const limit = item.limit
const value = item.value
if (index > limit) {
index += value
}
}
return index
}
children.forEach((child, index) => {
const type = child.type
const content = child.content
if (type !== 'text') return false
if (!content.includes('[@') || !content.includes('@]')) return false
const ats = content.match(/(?<=\[@).*?(?=@\])/g)
const spilted = content.split(/\[\@[^\[^@]+\@\]/g)
if (ats.length === 0) return false
inline.children.splice(calcOffset(index), 1)
history.push({
limit: index,
value: -1
})
ats.forEach((at, offset) => {
inline.children.splice(calcOffset(index + offset + 1), 0, {
type: 'text',
content: spilted.shift(),
level: 0,
})
history.push({
limit: index + offset + 1,
value: 1
})
inline.children.splice(calcOffset(index + offset + 2), 0, {
type: 'at',
content: at,
level: 0,
children: []
})
history.push({
limit: index + offset + 2,
value: 1
})
})
})
})
return true
})
// 识别latex($$xxx$$)语法
md.core.ruler.push('latex', (state) => {
const tokens = state.tokens
const inlines = tokens.filter((token) => token.type === 'inline')
if (inlines.length === 0) return false
inlines.forEach((inline) => {
const children = Array.from(inline.children)
if (children.length === 0) return false
const history = []
// 输入原始index,计算经过了处理后的index
const calcOffset = (index) => {
for (const item of history) {
const limit = item.limit
const value = item.value
if (index > limit) {
index += value
}
}
return index
}
children.forEach((child, index) => {
const type = child.type
const content = child.content
if (type !== 'text') return false
if (!content.includes('$$') || content.match(/\$\$/g).length % 2 !== 0) return false
const latexs = content.match(/(?<=\$\$).*?(?=\$\$)/g)
const spilted = content.split(/\$\$[^\$]+\$\$/g)
if (latexs.length === 0) return false
// 删除原始的text节点
inline.children.splice(calcOffset(index), 1)
history.push({
limit: index,
value: -1
})
// 插入新的text节点和latex节点
latexs.forEach((latex, offset) => {
inline.children.splice(calcOffset(index + offset + 1), 0, {
type: 'text',
content: spilted.shift(),
level: 0,
})
history.push({
limit: index + offset + 1,
value: 1
})
inline.children.splice(calcOffset(index + offset + 2), 0, {
type: 'latex',
content: latex,
level: 0,
children: []
})
history.push({
limit: index + offset + 2,
value: 1
})
})
})
})
})
md.renderer.rules.at = (tokens, idx) => {
const token = tokens[idx]
return `<span class="at">${token.content}</span>`
}
md.renderer.rules.latex = (tokens, idx) => {
const token = tokens[idx]
return `<span class="latex">${token.content}</span>`
}
const input = fs.readFileSync('input.md', 'utf8')
fs.writeFileSync('output.html', md.render(input))
fs.writeFileSync('output.json', JSON.stringify(md.parse(input, {}), null, 2))