-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfix-typedoc-html.js
More file actions
114 lines (90 loc) · 3.47 KB
/
Copy pathfix-typedoc-html.js
File metadata and controls
114 lines (90 loc) · 3.47 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
import { readFileSync, writeFileSync, readdirSync, statSync, existsSync } from 'fs'
import { join } from 'path'
function walkDir(dir, callback) {
const files = readdirSync(dir)
for (const file of files) {
const filePath = join(dir, file)
const stat = statSync(filePath)
if (stat.isDirectory()) {
walkDir(filePath, callback)
} else if (file.endsWith('.md')) {
callback(filePath)
}
}
}
function fixMarkdownFile(filePath) {
let content = readFileSync(filePath, 'utf8')
let modified = false
// 修复各种HTML标签问题
const fixes = [
// 1. 修复标题中的泛型参数 - 这是主要问题源
[/# (抽象\s+类|类|接口|类型别名|函数): ([^<\n]+)<([^>\n]+)>/g, '# $1: $2\\<$3\\>'],
// 2. 修复继承关系中的泛型参数
[/- \[`([^<`]+)<([^>`]+)>`\]/g, '- [`$1\\<$2\\>`]'],
// 3. 修复构造函数和方法签名中的泛型参数
[/> \*\*new ([^<*]+)<([^>*]+)>\*\*/g, '> **new $1\\<$2\\>**'],
[/> \*\*([^<*]+)<([^>*]+)>\*\*/g, '> **$1\\<$2\\>**'],
// 4. 修复返回类型中的泛型参数
[/\): `([^<`]+)<([^>`]+)>`/g, '): `$1\\<$2\\>`'],
[/\): ([^<`\n]+)<([^>`\n]+)>/g, '): $1\\<$2\\>'],
// 5. 修复链接中的泛型参数
[/\[`([^<\]`]+)<([^>\]`]+)>`\]/g, '[`$1\\<$2\\>`]'],
[/\[([^<\]`]+)<([^>\]`]+)>\]/g, '[$1\\<$2\\>]'],
// 6. 修复类型定义中的泛型参数(在代码块外)
[/(?<!`)([A-Za-z_$][A-Za-z0-9_$]*)<([A-Za-z0-9_,\s|&\[\]]+)>(?!`)/g, '$1\\<$2\\>'],
// 7. 修复已经被错误转义的泛型类型(在代码块中应该正常显示)
[/`Promise`\\<`([^`]+)`\\>/g, '`Promise<$1>`'],
[/`Array`\\<`([^`]+)`\\>/g, '`Array<$1>`'],
[/`Set`\\<`([^`]+)`\\>/g, '`Set<$1>`'],
[/`Map`\\<`([^`]+)`,\s*`([^`]+)`\\>/g, '`Map<$1, $2>`'],
[/`Record`\\<`([^`]+)`,\s*`([^`]+)`\\>/g, '`Record<$1, $2>`'],
[/`Partial`\\<`([^`]+)`\\>/g, '`Partial<$1>`'],
[/`Required`\\<`([^`]+)`\\>/g, '`Required<$1>`'],
[/`Readonly`\\<`([^`]+)`\\>/g, '`Readonly<$1>`'],
// 8. 通用泛型类型修复(代码块中)
[/`([A-Za-z_$][A-Za-z0-9_$]*)`\\<`([^`]+)`\\>/g, '`$1<$2>`'],
// 9. 修复嵌套泛型
[/`([A-Za-z_$][A-Za-z0-9_$]*)`\\<`([^`]+)`\\<`([^`]+)`\\>\\>/g, '`$1<$2<$3>>`'],
// 10. 修复类型参数定义
[/### ([A-Z])\n\n`\1` = `([^`]+)`/g, '### $1\n\n`$1` = `$2`'],
// 11. 修复实现接口的泛型参数
[/- \[`([^<`]+)`\]\([^)]+\)<`([^>`]+)`>/g, '- [`$1`]($&)\\<`$2`\\>'],
// 12. 修复类型别名中的复杂泛型
[/> \*\*([^*]+)\*\* = ([^<\n]+)<([^>\n]+)>/g, '> **$1** = $2\\<$3\\>'],
]
for (const [pattern, replacement] of fixes) {
const newContent = content.replace(pattern, replacement)
if (newContent !== content) {
content = newContent
modified = true
}
}
if (modified) {
writeFileSync(filePath, content, 'utf8')
console.log(`已修复: ${filePath}`)
return true
}
return false
}
// 主函数
function main() {
const docsDir = './docs/guide/api/types'
let fixedCount = 0
// 检查目录是否存在
if (!existsSync(docsDir)) {
console.log(`目录 ${docsDir} 不存在,跳过修复过程`)
return
}
try {
walkDir(docsDir, (filePath) => {
if (fixMarkdownFile(filePath)) {
fixedCount++
}
})
console.log(`\n总共修复了 ${fixedCount} 个文件`)
} catch (error) {
console.error('修复过程中出错:', error.message)
process.exit(1)
}
}
main()