-
Notifications
You must be signed in to change notification settings - Fork 2
/
html-generator.ts
149 lines (133 loc) · 4.16 KB
/
html-generator.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
import fs from 'fs'
import * as htmlparser2 from 'htmlparser2'
import { render } from 'dom-serializer'
import { fillNumber, hasChinese, hasVueAttr, hasVueText, htmlTextVueExpressParse, includeCommonLang, includeLangMap, isExistsTextForLang, JsonMap, ReturnValue, setObjectAttr } from './common'
function htmlTextFilter(html: string) {
html = html.replace(/\n/gim, '')
html = html.trim()
return html
}
export function generatorHTML(html: string, filePrefix: string, index: number, commonLang: JsonMap): ReturnValue {
// DOM 树生成
const dom = htmlparser2.parseDocument(html, {
xmlMode: true,
lowerCaseTags: false,
lowerCaseAttributeNames: false,
})
const outputLanguageConfig: {
[key: string]: any
} = {}
// HTML 属性转换
function forEachAttr(node: any) {
for (const key in node.attribs) {
const text = node.attribs[key]
if (!hasChinese(text)) continue
if (hasVueAttr(key)) continue // 忽略 Vue 动态属性
const { tFnKey, isBreak } = includeLangMap(filePrefix, index, text, commonLang, outputLanguageConfig)
const textVueTemplate = `$t('${tFnKey}')`
// 删除旧的 HTML 属性,新增i18n绑定
delete node.attribs[key]
node.attribs[`:${key}`] = textVueTemplate
console.log('HTML 属性转换:', node.name, key, text, '->', textVueTemplate)
if (!isBreak) index += 1
}
}
// 文本常量转换
function constDefineOutput(keyword: string) {
if (hasVueText(keyword) || !hasChinese(keyword)) return keyword
const { tFnKey, isBreak } = includeLangMap(filePrefix, index, keyword, commonLang, outputLanguageConfig)
const textVueTemplate = `{{ $t('${tFnKey}') }}`
console.log('HTML 文本转换:', keyword, '->', textVueTemplate)
if (!isBreak) index += 1
return textVueTemplate
}
// Vue 表达式文本转换
function expressDefineOutput(keyword: string) {
let expressArray: string[] = []
try {
expressArray = htmlTextVueExpressParse(keyword)
} catch (error) {
return keyword
}
const vueParams: string[] = []
let paramsIndex = 0
for (let index = 0; index < expressArray.length; index++) {
const element = expressArray[index]
if (element.includes('{{') && element.includes('}}')) {
vueParams.push(element.slice(2, -2))
expressArray[index] = `{${paramsIndex}}`
paramsIndex++
}
}
const $tExpress = expressArray.join('')
const $tParams = JSON.stringify(vueParams).replace(/\"/gim, '')
const { tFnKey, isBreak } = includeLangMap(filePrefix, index, $tExpress, commonLang, outputLanguageConfig)
const textVueTemplate = `{{ $t('${tFnKey}', ${$tParams}) }}`
console.log('HTML 表达式转换:', keyword, '->', textVueTemplate)
if (!isBreak) index += 1
return textVueTemplate
}
// 递归DOM树,根据其特点批量转换所有节点
function walk(dom: any): any {
for (const node of dom.children) {
forEachAttr(node)
if (node.children?.length > 0) {
walk(node)
continue
}
if (node.type === 'text') {
const text = htmlTextFilter(node.data)
if (!hasChinese(text)) continue
if (!hasVueText(text)) {
node.data = constDefineOutput(text)
} else {
node.data = expressDefineOutput(text)
}
}
}
}
walk(dom)
// DOM 树生成 HTML
const outputHtml = render(dom, {
encodeEntities: false,
decodeEntities: false,
xmlMode: false,
selfClosingTags: false,
})
return {
index,
output: outputHtml,
lang: outputLanguageConfig,
}
}
// Node 类型
// Document {
// parent: null,
// prev: null,
// next: null,
// startIndex: null,
// endIndex: null,
// children: [
// Element {
// parent: [Circular *1],
// prev: null,
// next: [Text],
// startIndex: null,
// endIndex: null,
// children: [Array],
// name: 'template',
// attribs: {},
// type: 'tag'
// },
// Text {
// parent: [Circular *1],
// prev: [Element],
// next: null,
// startIndex: null,
// endIndex: null,
// data: '\n',
// type: 'text'
// }
// ],
// type: 'root'
// }