-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
411 lines (393 loc) · 12.3 KB
/
vite.config.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { sveltekit } from '@sveltejs/kit/vite'
import { defineConfig } from 'vite'
import wasm from 'vite-plugin-wasm'
import * as Markdown from 'vite-plugin-markdown'
import topLevelAwait from 'vite-plugin-top-level-await'
import hljs from 'highlight.js'
import MarkdownIt from 'markdown-it'
import { textVide } from 'text-vide'
import { katex } from '@mdit/plugin-katex'
import Token from 'markdown-it/lib/token'
import * as fs from 'node:fs'
import path from 'node:path'
export default defineConfig({
plugins: [
sveltekit(),
wasm(),
topLevelAwait(),
Markdown.plugin({
mode: [Markdown.Mode.HTML, Markdown.Mode.TOC],
markdownIt: markdown_preprocess()
})
],
server: {
fs: {
allow: ['./wasm/target'],
}
}
})
/**
* markdown预处理
*/
function markdown_preprocess(): MarkdownIt {
const markdown = new MarkdownIt({
html: false,
highlight: (str, lang) => {
if (lang) {
return highlight_process(str, lang)
} else {
return str
}
}
})
// katex
markdown.use(katex)
// 表格标头
markdown.use(plugin_table_caption)
// 嵌入图片
markdown.use(plugin_embed_image)
// 超链接的addr
markdown.use(plugin_section_id)
// 断行
markdown.use(plugin_line_break)
// bionic reading
markdown.use(plugin_bionic_reading)
return markdown
}
/**
* 表格标头
*
* 该插件识别"!table-caption:XXX"开头的段落, 并替换为合适的表格标头
*/
function plugin_table_caption(md: MarkdownIt): void {
md.core.ruler.push('table-caption', state => {
state.tokens.forEach(token => {
if (token.type !== 'inline') {
return
}
const caption = token.content.match(/!table-caption:(.+)/)
if (caption === null) {
return
}
const caption_text = caption[1]
// 生成table id
const caption_id = as_table_id_name(caption_text)
// 重新生成caption text
token.children = []
token.children.push(make_html_token('<span id="' + caption_id + '" class="table-caption">'))
token.children.push(make_text_token(caption_text))
token.children.push(make_html_token('</span>'))
})
})
md.enable(['table-caption'])
}
/**
* 嵌入图片
*/
function plugin_embed_image(md: MarkdownIt): void {
md.core.ruler.push('embed-image', state => {
state.tokens.forEach(token => {
if (token.type === 'inline') {
if (token.children === null) {
return
}
if (token.children.length !== 1) {
return
}
if (token.children[0].type !== 'image') {
return
}
// 图片转换为base64
const img_child = token.children[0]
const img_path = img_child.attrGet('src')
if (img_path !== null) {
const img_base64 = readfile_as_base64(img_path)
img_child.attrSet('src', img_base64)
}
// 添加图注
// 总而言之它以一种很奇怪的方式跑了起来...
const img_alt = img_child.attrGet('alt') ?? ''
let img_alt_content: string
if (img_alt.length > 0) {
img_alt_content = img_alt
}
else {
img_alt_content = img_child.content
}
const text_token = make_html_token(img_alt_content)
token.children.push(make_html_token('<span class="img-caption">'))
token.children.push(text_token)
token.children.push(make_html_token('</span>'))
// 添加图片id
img_child.attrSet('id', as_figure_id_name(img_alt_content))
}
})
})
md.enable(['embed-image'])
}
/**
* 段落id
*/
function plugin_section_id(md: MarkdownIt): void {
md.core.ruler.push('toc-hyper', state => {
state.tokens.forEach((token, index, self) => {
if (token.tag.match(/h[0-9]/g) && token.nesting === 1) {
// <hxx>认为是一个section</hxx>
// 检查下一个内容
const content = ((pos, arr) => {
if (pos >= arr.length) {
return ''
}
const token = arr[pos]
return token.content
})(index + 1, self)
// section的id
const section_id = as_section_id_name(content)
// push attr
token.attrPush(['id', section_id])
}
})
})
md.enable(['toc-hyper'])
}
/**
* 转换为合适的id名
*
* 该函数会优先考虑 [0-9] { `.` [0-9]} 的范式生成id
* 否则会生成完整的内容
*/
function as_section_id_name(content: string): string {
const patt = content.match(/[0-9](\.[0-9]+)*/g)
const section_id = patt ? patt[0] : content
return 'section_' + replace_escape_char(section_id.toLowerCase())
}
/**
* 转换为合适的id名
*
* 该函数会优先考虑 Figure[0-9] { `.` [0-9]} 的范式生成id
* 否则会生成完整的内容
*/
function as_figure_id_name(content: string): string {
const patt = content.match(/(Figure|figure|图)( *)([0-9](\.[0-9]+)*)/)
const figure_id = patt ? patt[3] : content
return 'figure_' + replace_escape_char(figure_id.toLowerCase())
}
/**
* 转换为合适的id名
*
* 该函数会优先考虑 Table[0-9] { `.` [0-9]} 的范式生成id
* 否则会生成完整的内容
*/
function as_table_id_name(content: string): string {
const patt = content.match(/(Table|table|表)( *)([0-9](\.[0-9]+)*)/)
const table_id = patt ? patt[3] : content
return 'table_' + replace_escape_char(table_id.toLowerCase())
}
/**
* 把<, >等字符替换为_
*/
function replace_escape_char(content: string): string {
const lut = new Set([
'<', '>', '&', '"', '\'', ' ', '-', ':', '.'
])
return content.replace(/[<>&". -:]/g, (c: string) => {
return lut.has(c) ? '_' : c
})
}
/**
* 断行
*/
function plugin_line_break(md: MarkdownIt): void {
md.core.ruler.push('code-line-break', state => {
state.tokens.forEach(token => {
token.children?.forEach(token => {
if (token.type === 'code_inline') {
token.attrPush(['class', 'wrap_code_inline'])
// 理论上不应该存在children
if (token.children !== null) {
throw 'Token children is NOT null.'
}
// 处理内容, 插入断行
const content = token.content
token.content = insert_line_break_marker(content)
}
})
})
})
md.enable(['code-line-break'])
}
/**
* 插入0宽度空格方便进行断行
*/
function insert_line_break_marker(str: string): string {
const arr = str.split('_')
return arr.join('_\u200B')
}
/**
* bionic-reading
*/
function plugin_bionic_reading(md: MarkdownIt): void {
md.core.ruler.push('bionic-reading', state => {
let process_index = 0
state.tokens.forEach((token, index, self) => {
if (token.type === 'paragraph_open' && index >= process_index) {
const begin_index = index + 1
// 找到end
let it = process_index + 1
while (it < self.length && self[it].type !== 'paragraph_close') {
it += 1
}
process_index = it
const end_index = it
// begin_index ... end_index即是一段的内容, 处理段落
const paragraph = self.slice(begin_index, end_index)
paragraph.forEach(token => {
const children: Token[] = []
// 处理文本
token.children?.forEach(token => {
if (token.type === 'text') {
if (token.children != null) {
throw 'Markdown parser: Cannot process a text with children'
}
const content = token.content
if (/^[\x00-\x7f]+$/.test(content)) {
const result = add_bionic_reading(token.content)
children.push(...result)
} else {
// 非ascii
children.push(token)
}
} else {
children.push(token)
}
})
// 更新children
token.children = children
})
}
})
})
md.enable(['bionic-reading'])
}
/**
* 从text生成token stream, 插入bionic-reading的标签
*/
function add_bionic_reading(str: string): Token[] {
const BEGIN_SPEC = '\ufff0'
const END_SPEC = '\ufff1'
const HIGHLIGHT_BEGIN = '<span class="bio-reading-bold">'
const HIGHLIGHT_END = '</span>'
// 处理内容
const output_str = bionic_reading_process(str, BEGIN_SPEC, END_SPEC)
// 生成token
let buffer = ''
let nesting_check = 0
const result: Token[] = []
for (const ch of output_str) {
if (ch !== BEGIN_SPEC && ch !== END_SPEC) {
buffer += ch
} else if (buffer.length > 0) {
// flush
result.push(make_text_token(buffer))
buffer = ''
}
// 处理begin和end
if (ch === BEGIN_SPEC) {
nesting_check += 1
result.push(make_html_token(HIGHLIGHT_BEGIN))
} else if (ch === END_SPEC) {
nesting_check -= 1
result.push(make_html_token(HIGHLIGHT_END))
}
}
if (buffer.length > 0) {
result.push(make_text_token(buffer))
}
if (nesting_check !== 0) {
throw 'Nesting check fault'
}
return result
}
/**
* bionic_reading处理
*/
function bionic_reading_process(str: string, split_beg: string, split_end: string): string {
if (str.match(/\[toc\]|\[TOC\]/) === null) {
return textVide(str, {
ignoreHtmlTag: true,
sep: [split_beg, split_end]
})
} else {
return str
}
}
/**
* 高亮处理
*/
function highlight_process(str: string, lang: string): string {
if (hljs.getLanguage(lang)) {
const html = hljs.highlight(str, { language: lang }).value
return '<pre class="hljs"><code>' + html + '</code></pre>'
} else {
return str
}
}
/**
* 读取文件并转为base64
*/
function readfile_as_base64(file: string): string {
const src_path = path.resolve('./src/texts')
const full_path = path.resolve(src_path, file)
const file_ext_name = path.extname(full_path)
// 读取并转换base64
const fs_data = fs.readFileSync(full_path)
const base64 = fs_data.toString('base64')
// 转换mime type
const mime_lut = new Map([
['.gif', 'image/gif'],
['.png', 'image/png'],
['.jpg', 'image/jpeg'],
['.jpeg', 'image/jpeg'],
['.webp', 'image/webp'],
])
const mime_type = mime_lut.get(file_ext_name)
if (mime_type) {
return 'data:' + mime_type + ';base64,' + base64
} else {
throw `No mime type for extname ${file_ext_name}`
}
}
/**
* 构造text的标签
*/
function make_text_token(str: string): Token {
const token = new Token('text', '', 0)
token.block = false
token.hidden = false
token.attrs = null
token.map = null
token.level = 0
token.children = null
token.content = str
token.markup = ''
token.info = ''
token.meta = null
return token
}
/**
* 构造HTML的标签
*/
function make_html_token(tag: string): Token {
const token = new Token('html_inline', '', 0)
token.attrs = null
token.map = null
token.level = 0
token.children = null
token.content = tag
token.markup = ''
token.info = ''
token.meta = null
token.block = false
token.hidden = false
return token
}