verce-vue-test 0.0.53 → 0.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/mini2.0-main 2/src/App.vue +1 -1
- package/mini2.0-main 2/src/api/announcement.ts +405 -0
- package/mini2.0-main 2/src/config/plugin.properties.test +2 -2
- package/mini2.0-main 2/src/utils/code-highlight.ts +165 -0
- package/mini2.0-main 2/src/utils/copy.ts +36 -0
- package/mini2.0-main 2/src/views/article-detail/index.vue +105 -7
- package/mini2.0-main 2/src/views/article-edit/index.vue +121 -16
- package/mini2.0-main 2/src/views/articles/index.vue +8 -8
- package/package.json +1 -1
- package/src/utils/code-highlight.ts +200 -0
- package/src/views/NoticeDetailView.vue +69 -1
|
@@ -4,7 +4,7 @@ const router = useRouter()
|
|
|
4
4
|
|
|
5
5
|
const tabItems = [
|
|
6
6
|
{ title: '首页', path: '/', icon: 'wap-home-o' },
|
|
7
|
-
{ title: '
|
|
7
|
+
{ title: '公告', path: '/articles', icon: 'records-o' },
|
|
8
8
|
{ title: '我的', path: '/mine', icon: 'manager-o' },
|
|
9
9
|
]
|
|
10
10
|
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import { request } from '@/core/request'
|
|
2
|
+
import type { ApiResponse } from '@/types/api'
|
|
3
|
+
|
|
4
|
+
export interface Article {
|
|
5
|
+
id: string
|
|
6
|
+
title: string
|
|
7
|
+
content: string
|
|
8
|
+
author: string
|
|
9
|
+
createdAt: string
|
|
10
|
+
updatedAt: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ArticlePayload {
|
|
14
|
+
title: string
|
|
15
|
+
content: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type RawRecord = Record<string, unknown>
|
|
19
|
+
|
|
20
|
+
const LIST_URL = '/uaa/announcement/get/restricted'
|
|
21
|
+
const DETAIL_URL = '/uaa/announcement/get/id'
|
|
22
|
+
const ADD_URL = '/uaa/announcement/add'
|
|
23
|
+
const UPDATE_URL = '/uaa/announcement/update'
|
|
24
|
+
const DEFAULT_ANNOUNCEMENT_FIELDS = {
|
|
25
|
+
annouZrFteLag: '0',
|
|
26
|
+
annouSts: '0',
|
|
27
|
+
department: '100011',
|
|
28
|
+
efDt: '2026-07-01 00:00:00',
|
|
29
|
+
expireDt: '2026-07-31 00:00:00',
|
|
30
|
+
orgList: [],
|
|
31
|
+
orgNo: '25001',
|
|
32
|
+
roleList: [],
|
|
33
|
+
topFlag: '0',
|
|
34
|
+
userList: [],
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const ALLOWED_TAGS = new Set([
|
|
38
|
+
'A',
|
|
39
|
+
'B',
|
|
40
|
+
'BLOCKQUOTE',
|
|
41
|
+
'BR',
|
|
42
|
+
'DIV',
|
|
43
|
+
'EM',
|
|
44
|
+
'H2',
|
|
45
|
+
'H3',
|
|
46
|
+
'I',
|
|
47
|
+
'LI',
|
|
48
|
+
'OL',
|
|
49
|
+
'P',
|
|
50
|
+
'PRE',
|
|
51
|
+
'S',
|
|
52
|
+
'SPAN',
|
|
53
|
+
'STRONG',
|
|
54
|
+
'U',
|
|
55
|
+
'UL',
|
|
56
|
+
'CODE',
|
|
57
|
+
])
|
|
58
|
+
|
|
59
|
+
export async function queryArticles() {
|
|
60
|
+
const response = await request<unknown>({
|
|
61
|
+
url: LIST_URL,
|
|
62
|
+
method: 'GET',
|
|
63
|
+
})
|
|
64
|
+
const data = unwrapResponse(response)
|
|
65
|
+
|
|
66
|
+
return extractRecords(data).map((record) => mapAnnouncementRecord(record))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function getArticleDetail(annouId: string) {
|
|
70
|
+
const response = await request<unknown>({
|
|
71
|
+
url: DETAIL_URL,
|
|
72
|
+
method: 'GET',
|
|
73
|
+
params: { annouId },
|
|
74
|
+
})
|
|
75
|
+
const data = unwrapResponse(response)
|
|
76
|
+
const record = extractDetailRecord(data)
|
|
77
|
+
|
|
78
|
+
return mapAnnouncementRecord(record, annouId)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function createArticle(payload: ArticlePayload) {
|
|
82
|
+
const content = sanitizeArticleHtml(payload.content)
|
|
83
|
+
const response = await request<unknown, RawRecord>({
|
|
84
|
+
url: ADD_URL,
|
|
85
|
+
method: 'POST',
|
|
86
|
+
data: buildAnnouncementPayload({ ...payload, content }),
|
|
87
|
+
})
|
|
88
|
+
const data = unwrapResponse(response)
|
|
89
|
+
const record = extractDetailRecord(data)
|
|
90
|
+
|
|
91
|
+
return mapAnnouncementRecord(record, readString(record, ['annouId', 'id']))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function updateArticle(annouId: string, payload: ArticlePayload) {
|
|
95
|
+
const content = sanitizeArticleHtml(payload.content)
|
|
96
|
+
const response = await request<unknown, RawRecord>({
|
|
97
|
+
url: UPDATE_URL,
|
|
98
|
+
method: 'POST',
|
|
99
|
+
data: buildAnnouncementPayload({ ...payload, content }, annouId),
|
|
100
|
+
})
|
|
101
|
+
const data = unwrapResponse(response)
|
|
102
|
+
const record = extractDetailRecord(data)
|
|
103
|
+
|
|
104
|
+
return mapAnnouncementRecord(record, annouId)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function formatArticleDate(value: string) {
|
|
108
|
+
const date = new Date(value)
|
|
109
|
+
|
|
110
|
+
if (Number.isNaN(date.getTime())) {
|
|
111
|
+
return ''
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return new Intl.DateTimeFormat('zh-CN', {
|
|
115
|
+
year: 'numeric',
|
|
116
|
+
month: '2-digit',
|
|
117
|
+
day: '2-digit',
|
|
118
|
+
hour: '2-digit',
|
|
119
|
+
minute: '2-digit',
|
|
120
|
+
}).format(date)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function extractTextFromHtml(html: string) {
|
|
124
|
+
if (!html.trim()) {
|
|
125
|
+
return ''
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const doc = new DOMParser().parseFromString(html, 'text/html')
|
|
129
|
+
|
|
130
|
+
return doc.body.textContent?.replace(/\s+/g, ' ').trim() || ''
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function sanitizeArticleHtml(html: string) {
|
|
134
|
+
if (!html.trim()) {
|
|
135
|
+
return ''
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const doc = new DOMParser().parseFromString(`<article>${html}</article>`, 'text/html')
|
|
139
|
+
const root = doc.body.firstElementChild
|
|
140
|
+
|
|
141
|
+
if (!root) {
|
|
142
|
+
return ''
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
sanitizeChildren(root, doc)
|
|
146
|
+
|
|
147
|
+
return root.innerHTML.trim()
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function buildAnnouncementPayload(payload: ArticlePayload, annouId = '') {
|
|
151
|
+
return {
|
|
152
|
+
annouTitle: payload.title.trim(),
|
|
153
|
+
annouConte: payload.content,
|
|
154
|
+
annouId,
|
|
155
|
+
...DEFAULT_ANNOUNCEMENT_FIELDS,
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function unwrapResponse(response: ApiResponse<unknown> | unknown) {
|
|
160
|
+
if (!isRecord(response)) {
|
|
161
|
+
return response
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if ('code' in response && 'data' in response) {
|
|
165
|
+
return response.data
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return response
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function extractRecords(data: unknown): RawRecord[] {
|
|
172
|
+
const candidates = [
|
|
173
|
+
data,
|
|
174
|
+
getValue(data, ['list']),
|
|
175
|
+
getValue(data, ['records']),
|
|
176
|
+
getValue(data, ['rows']),
|
|
177
|
+
getValue(data, ['resultList']),
|
|
178
|
+
getValue(data, ['content']),
|
|
179
|
+
getValue(data, ['body', 'list']),
|
|
180
|
+
getValue(data, ['body', 'records']),
|
|
181
|
+
getValue(data, ['body', 'rows']),
|
|
182
|
+
getValue(data, ['body', 'resultList']),
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
for (const candidate of candidates) {
|
|
186
|
+
if (Array.isArray(candidate)) {
|
|
187
|
+
return candidate.filter(isRecord)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return []
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function extractDetailRecord(data: unknown): RawRecord {
|
|
195
|
+
const records = extractRecords(data)
|
|
196
|
+
|
|
197
|
+
if (records.length > 0) {
|
|
198
|
+
return records[0]!
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const candidates = [
|
|
202
|
+
data,
|
|
203
|
+
getValue(data, ['announcement']),
|
|
204
|
+
getValue(data, ['announcementInfo']),
|
|
205
|
+
getValue(data, ['article']),
|
|
206
|
+
getValue(data, ['detail']),
|
|
207
|
+
getValue(data, ['info']),
|
|
208
|
+
getValue(data, ['record']),
|
|
209
|
+
getValue(data, ['body']),
|
|
210
|
+
getValue(data, ['body', 'announcement']),
|
|
211
|
+
getValue(data, ['body', 'detail']),
|
|
212
|
+
getValue(data, ['body', 'info']),
|
|
213
|
+
]
|
|
214
|
+
|
|
215
|
+
return candidates.find(isRecord) || {}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function mapAnnouncementRecord(record: RawRecord, fallbackId = ''): Article {
|
|
219
|
+
const now = new Date().toISOString()
|
|
220
|
+
const id =
|
|
221
|
+
readString(record, ['annouId', 'announcementId', 'articleId', 'id', 'uuid']) ||
|
|
222
|
+
fallbackId
|
|
223
|
+
const title = readString(record, [
|
|
224
|
+
'annouTitle',
|
|
225
|
+
'announTitle',
|
|
226
|
+
'announcementTitle',
|
|
227
|
+
'title',
|
|
228
|
+
'name',
|
|
229
|
+
'subject',
|
|
230
|
+
])
|
|
231
|
+
const content = readString(record, [
|
|
232
|
+
'annouConte',
|
|
233
|
+
'announContent',
|
|
234
|
+
'announcementContent',
|
|
235
|
+
'articleContent',
|
|
236
|
+
'contentHtml',
|
|
237
|
+
'htmlContent',
|
|
238
|
+
'content',
|
|
239
|
+
])
|
|
240
|
+
const createdAt =
|
|
241
|
+
readString(record, ['createTime', 'createdAt', 'publishTime', 'pubTime', 'gmtCreate']) || now
|
|
242
|
+
const updatedAt =
|
|
243
|
+
readString(record, ['updateTime', 'updatedAt', 'modifyTime', 'gmtModified']) || createdAt
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
id,
|
|
247
|
+
title: title || '未命名公告',
|
|
248
|
+
content: sanitizeArticleHtml(content),
|
|
249
|
+
author:
|
|
250
|
+
readString(record, ['author', 'publisherName', 'pubUserName', 'creatorName', 'createBy']) ||
|
|
251
|
+
'管理员',
|
|
252
|
+
createdAt: normalizeDate(createdAt),
|
|
253
|
+
updatedAt: normalizeDate(updatedAt),
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function readString(record: RawRecord, keys: string[]) {
|
|
258
|
+
for (const key of keys) {
|
|
259
|
+
const value = record[key]
|
|
260
|
+
|
|
261
|
+
if (typeof value === 'string' && value.trim()) {
|
|
262
|
+
return value.trim()
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (typeof value === 'number') {
|
|
266
|
+
return String(value)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return ''
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function getValue(value: unknown, path: string[]) {
|
|
274
|
+
return path.reduce<unknown>((current, key) => {
|
|
275
|
+
if (!isRecord(current)) {
|
|
276
|
+
return undefined
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return current[key]
|
|
280
|
+
}, value)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function normalizeDate(value: string) {
|
|
284
|
+
if (/^\d{4}-\d{2}-\d{2}/.test(value)) {
|
|
285
|
+
return value
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (/^\d{14}$/.test(value)) {
|
|
289
|
+
return `${value.slice(0, 4)}-${value.slice(4, 6)}-${value.slice(6, 8)}T${value.slice(8, 10)}:${value.slice(10, 12)}:${value.slice(12, 14)}`
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (/^\d{8}$/.test(value)) {
|
|
293
|
+
return `${value.slice(0, 4)}-${value.slice(4, 6)}-${value.slice(6, 8)}T00:00:00`
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return value
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function isRecord(value: unknown): value is RawRecord {
|
|
300
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function sanitizeChildren(parent: ParentNode, doc: Document) {
|
|
304
|
+
Array.from(parent.childNodes).forEach((child) => {
|
|
305
|
+
if (child.nodeType === Node.COMMENT_NODE) {
|
|
306
|
+
child.remove()
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (child.nodeType !== Node.ELEMENT_NODE) {
|
|
311
|
+
return
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const element = child as HTMLElement
|
|
315
|
+
|
|
316
|
+
if (!ALLOWED_TAGS.has(element.tagName)) {
|
|
317
|
+
const fragment = doc.createDocumentFragment()
|
|
318
|
+
|
|
319
|
+
while (element.firstChild) {
|
|
320
|
+
fragment.appendChild(element.firstChild)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
element.replaceWith(fragment)
|
|
324
|
+
sanitizeChildren(parent, doc)
|
|
325
|
+
return
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
sanitizeAttributes(element)
|
|
329
|
+
sanitizeChildren(element, doc)
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function sanitizeAttributes(element: HTMLElement) {
|
|
334
|
+
Array.from(element.attributes).forEach((attribute) => {
|
|
335
|
+
const name = attribute.name.toLowerCase()
|
|
336
|
+
|
|
337
|
+
if (element.tagName === 'A' && name === 'href') {
|
|
338
|
+
if (!isSafeHref(attribute.value)) {
|
|
339
|
+
element.removeAttribute(attribute.name)
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
element.setAttribute('target', '_blank')
|
|
344
|
+
element.setAttribute('rel', 'noopener noreferrer')
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (element.tagName === 'A' && (name === 'target' || name === 'rel')) {
|
|
349
|
+
return
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (element.tagName === 'CODE' && name === 'class') {
|
|
353
|
+
const languageClass = normalizeCodeLanguageClass(attribute.value)
|
|
354
|
+
|
|
355
|
+
if (languageClass) {
|
|
356
|
+
element.setAttribute('class', languageClass)
|
|
357
|
+
return
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (element.tagName === 'CODE' && name === 'data-language') {
|
|
362
|
+
const language = normalizeCodeLanguage(attribute.value)
|
|
363
|
+
|
|
364
|
+
if (language) {
|
|
365
|
+
element.setAttribute('data-language', language)
|
|
366
|
+
return
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
element.removeAttribute(attribute.name)
|
|
371
|
+
})
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function normalizeCodeLanguageClass(value: string) {
|
|
375
|
+
const language = value
|
|
376
|
+
.split(/\s+/)
|
|
377
|
+
.find((className) => className.startsWith('language-'))
|
|
378
|
+
?.replace(/^language-/, '')
|
|
379
|
+
|
|
380
|
+
if (!language) {
|
|
381
|
+
return ''
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return `language-${normalizeCodeLanguage(language)}`
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function normalizeCodeLanguage(value: string) {
|
|
388
|
+
return value.trim().toLowerCase().replace(/[^a-z0-9_-]/g, '').slice(0, 24)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function isSafeHref(value: string) {
|
|
392
|
+
const href = value.trim()
|
|
393
|
+
|
|
394
|
+
if (href.startsWith('#')) {
|
|
395
|
+
return true
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
try {
|
|
399
|
+
const url = new URL(href, window.location.origin)
|
|
400
|
+
|
|
401
|
+
return ['http:', 'https:', 'mailto:', 'tel:'].includes(url.protocol)
|
|
402
|
+
} catch {
|
|
403
|
+
return false
|
|
404
|
+
}
|
|
405
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
const KEYWORDS = new Set([
|
|
2
|
+
'abstract',
|
|
3
|
+
'async',
|
|
4
|
+
'await',
|
|
5
|
+
'boolean',
|
|
6
|
+
'break',
|
|
7
|
+
'case',
|
|
8
|
+
'catch',
|
|
9
|
+
'class',
|
|
10
|
+
'const',
|
|
11
|
+
'continue',
|
|
12
|
+
'default',
|
|
13
|
+
'delete',
|
|
14
|
+
'do',
|
|
15
|
+
'else',
|
|
16
|
+
'enum',
|
|
17
|
+
'export',
|
|
18
|
+
'extends',
|
|
19
|
+
'false',
|
|
20
|
+
'finally',
|
|
21
|
+
'for',
|
|
22
|
+
'from',
|
|
23
|
+
'function',
|
|
24
|
+
'if',
|
|
25
|
+
'import',
|
|
26
|
+
'in',
|
|
27
|
+
'interface',
|
|
28
|
+
'let',
|
|
29
|
+
'new',
|
|
30
|
+
'null',
|
|
31
|
+
'number',
|
|
32
|
+
'private',
|
|
33
|
+
'protected',
|
|
34
|
+
'public',
|
|
35
|
+
'return',
|
|
36
|
+
'static',
|
|
37
|
+
'string',
|
|
38
|
+
'super',
|
|
39
|
+
'switch',
|
|
40
|
+
'this',
|
|
41
|
+
'throw',
|
|
42
|
+
'true',
|
|
43
|
+
'try',
|
|
44
|
+
'type',
|
|
45
|
+
'typeof',
|
|
46
|
+
'undefined',
|
|
47
|
+
'var',
|
|
48
|
+
'void',
|
|
49
|
+
'while',
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
const TOKEN_PATTERN =
|
|
53
|
+
/(\/\*[\s\S]*?\*\/|\/\/[^\n]*|#[^\n]*|<!--[\s\S]*?-->|(["'`])(?:\\[\s\S]|(?!\2)[^\\])*\2|<\/?[A-Za-z][^>\n]*?>|\b\d+(?:\.\d+)?\b|\b[A-Za-z_$][\w$]*\b)/g
|
|
54
|
+
|
|
55
|
+
export function highlightArticleHtml(html: string) {
|
|
56
|
+
if (!html.trim()) {
|
|
57
|
+
return ''
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const doc = new DOMParser().parseFromString(`<article>${html}</article>`, 'text/html')
|
|
61
|
+
const root = doc.body.firstElementChild
|
|
62
|
+
|
|
63
|
+
if (!root) {
|
|
64
|
+
return html
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
root.querySelectorAll('pre code').forEach((code) => {
|
|
68
|
+
const rawCode = code.textContent || ''
|
|
69
|
+
const language = getLanguage(code)
|
|
70
|
+
|
|
71
|
+
code.innerHTML = highlightCode(rawCode)
|
|
72
|
+
code.classList.add('code-highlighted')
|
|
73
|
+
|
|
74
|
+
if (language) {
|
|
75
|
+
code.setAttribute('data-language', language)
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
return root.innerHTML
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function highlightCode(code: string) {
|
|
83
|
+
let html = ''
|
|
84
|
+
let lastIndex = 0
|
|
85
|
+
|
|
86
|
+
code.replace(TOKEN_PATTERN, (...args) => {
|
|
87
|
+
const token = args[0]
|
|
88
|
+
const offset = args[args.length - 2] as number
|
|
89
|
+
|
|
90
|
+
html += escapeHtml(code.slice(lastIndex, offset))
|
|
91
|
+
html += renderToken(token)
|
|
92
|
+
lastIndex = offset + token.length
|
|
93
|
+
|
|
94
|
+
return token
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
html += escapeHtml(code.slice(lastIndex))
|
|
98
|
+
|
|
99
|
+
return html
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function renderToken(token: string) {
|
|
103
|
+
if (isComment(token)) {
|
|
104
|
+
return wrapToken(token, 'hl-comment')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (isString(token)) {
|
|
108
|
+
return wrapToken(token, 'hl-string')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (isTag(token)) {
|
|
112
|
+
return wrapToken(token, 'hl-tag')
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (isNumber(token)) {
|
|
116
|
+
return wrapToken(token, 'hl-number')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (KEYWORDS.has(token)) {
|
|
120
|
+
return wrapToken(token, 'hl-keyword')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return escapeHtml(token)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function wrapToken(token: string, className: string) {
|
|
127
|
+
return `<span class="${className}">${escapeHtml(token)}</span>`
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function getLanguage(code: Element) {
|
|
131
|
+
const languageAttr = code.getAttribute('data-language')
|
|
132
|
+
const languageClass = Array.from(code.classList).find((className) =>
|
|
133
|
+
className.startsWith('language-'),
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
return languageAttr || languageClass?.replace(/^language-/, '') || ''
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function isComment(token: string) {
|
|
140
|
+
return (
|
|
141
|
+
token.startsWith('//') ||
|
|
142
|
+
token.startsWith('/*') ||
|
|
143
|
+
token.startsWith('#') ||
|
|
144
|
+
token.startsWith('<!--')
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function isString(token: string) {
|
|
149
|
+
return /^["'`]/.test(token)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isTag(token: string) {
|
|
153
|
+
return /^<\/?[A-Za-z]/.test(token)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function isNumber(token: string) {
|
|
157
|
+
return /^\d/.test(token)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function escapeHtml(value: string) {
|
|
161
|
+
const div = document.createElement('div')
|
|
162
|
+
div.textContent = value
|
|
163
|
+
|
|
164
|
+
return div.innerHTML
|
|
165
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export async function copyText(text: string) {
|
|
2
|
+
if (window.isSecureContext && navigator.clipboard?.writeText) {
|
|
3
|
+
try {
|
|
4
|
+
await navigator.clipboard.writeText(text)
|
|
5
|
+
return
|
|
6
|
+
} catch {
|
|
7
|
+
// HTTP 部署或宿主 WebView 里经常不可用,继续走兼容方案。
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
fallbackCopyText(text)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function fallbackCopyText(text: string) {
|
|
15
|
+
const textarea = document.createElement('textarea')
|
|
16
|
+
const previousFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null
|
|
17
|
+
|
|
18
|
+
textarea.value = text
|
|
19
|
+
textarea.setAttribute('readonly', 'true')
|
|
20
|
+
textarea.style.position = 'fixed'
|
|
21
|
+
textarea.style.left = '-9999px'
|
|
22
|
+
textarea.style.top = '0'
|
|
23
|
+
textarea.style.opacity = '0'
|
|
24
|
+
document.body.appendChild(textarea)
|
|
25
|
+
textarea.select()
|
|
26
|
+
textarea.setSelectionRange(0, textarea.value.length)
|
|
27
|
+
|
|
28
|
+
const copied = document.execCommand('copy')
|
|
29
|
+
|
|
30
|
+
document.body.removeChild(textarea)
|
|
31
|
+
previousFocus?.focus()
|
|
32
|
+
|
|
33
|
+
if (!copied) {
|
|
34
|
+
throw new Error('复制失败')
|
|
35
|
+
}
|
|
36
|
+
}
|