hexo-swpp 2.1.0 → 2.2.0
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/index.js +65 -44
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -25,6 +25,41 @@ const {
|
|
|
25
25
|
} = pluginConfig?.enable ? require(findScript()) : {}
|
|
26
26
|
|
|
27
27
|
if (pluginConfig?.enable) {
|
|
28
|
+
// 排序
|
|
29
|
+
(() => {
|
|
30
|
+
const compare = (a, b) => {
|
|
31
|
+
const result = a.length === b.length ? a < b : a.length < b.length
|
|
32
|
+
return result ? -1 : 1
|
|
33
|
+
}
|
|
34
|
+
const sort = (obj, value) => {
|
|
35
|
+
if (!obj) return
|
|
36
|
+
const target = obj.data ?? obj
|
|
37
|
+
if (!target.sort) return
|
|
38
|
+
if (value !== false) target.sort((a, b) => compare(a[value], b[value]))
|
|
39
|
+
else target.sort(compare)
|
|
40
|
+
}
|
|
41
|
+
const list = {
|
|
42
|
+
posts: 'title',
|
|
43
|
+
pages: 'title',
|
|
44
|
+
tags: 'name',
|
|
45
|
+
categories: 'name'
|
|
46
|
+
}
|
|
47
|
+
Object.assign(list, pluginConfig.sort)
|
|
48
|
+
const Locals = require(`${nodePath.resolve('./', 'node_modules/hexo/lib/hexo/locals')}`).prototype
|
|
49
|
+
const get = Locals.get
|
|
50
|
+
Locals.get = function(name) {
|
|
51
|
+
const result = get.call(this, name)
|
|
52
|
+
if (name in list) sort(result, list[name])
|
|
53
|
+
if ('forEach' in result) {
|
|
54
|
+
result.forEach(it => {
|
|
55
|
+
for (let tag in list)
|
|
56
|
+
sort(it[tag], list[tag])
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
return result
|
|
60
|
+
}
|
|
61
|
+
})()
|
|
62
|
+
|
|
28
63
|
// 生成 update.json
|
|
29
64
|
hexo.extend.console.register('swpp', '生成前端更新需要的 json 文件以及相关缓存', {}, async () => {
|
|
30
65
|
if (!fs.existsSync(config.public_dir))
|
|
@@ -128,6 +163,7 @@ if (pluginConfig?.enable) {
|
|
|
128
163
|
</script>`
|
|
129
164
|
}, "default")
|
|
130
165
|
|
|
166
|
+
// 插入 sw-dom.js
|
|
131
167
|
if (!pluginConfig.dom?.custom) {
|
|
132
168
|
hexo.extend.injector.register('body_begin', () => {
|
|
133
169
|
// noinspection HtmlUnknownTarget
|
|
@@ -159,7 +195,7 @@ const eachAllFile = (root, cb) => {
|
|
|
159
195
|
|
|
160
196
|
/** 判断指定文件是否需要排除 */
|
|
161
197
|
const isExclude = pathname => {
|
|
162
|
-
for (let reg of pluginConfig.exclude) {
|
|
198
|
+
for (let reg of pluginConfig.json.exclude) {
|
|
163
199
|
if (pathname.match(new RegExp(reg, 'i'))) return true
|
|
164
200
|
}
|
|
165
201
|
return false
|
|
@@ -361,26 +397,26 @@ const compare = (oldCache, newCache) => {
|
|
|
361
397
|
|
|
362
398
|
/** 判断指定资源是否需要合并 */
|
|
363
399
|
const isMerge = (pathname, tidied) => {
|
|
364
|
-
const optional = pluginConfig.merge
|
|
400
|
+
const optional = pluginConfig.json.merge
|
|
401
|
+
if (!optional) return false
|
|
365
402
|
if (pathname.includes(`/${config.tag_dir}/`)) {
|
|
366
|
-
if (optional.tags ?? true)
|
|
367
|
-
tidied.tags = true
|
|
368
|
-
return true
|
|
369
|
-
}
|
|
403
|
+
if (optional.tags ?? true)
|
|
404
|
+
return tidied.tags = true
|
|
370
405
|
} else if (pathname.includes(`/${config.archive_dir}/`)) {
|
|
371
|
-
if (optional.archives ?? true)
|
|
372
|
-
tidied.archives = true
|
|
373
|
-
return true
|
|
374
|
-
}
|
|
406
|
+
if (optional.archives ?? true)
|
|
407
|
+
return tidied.archives = true
|
|
375
408
|
} else if (pathname.includes(`/${config.category_dir}/`)) {
|
|
376
|
-
if (optional.categories ?? true)
|
|
377
|
-
tidied.categories = true
|
|
378
|
-
return true
|
|
379
|
-
}
|
|
409
|
+
if (optional.categories ?? true)
|
|
410
|
+
return tidied.categories = true
|
|
380
411
|
} else if (pathname.startsWith('/page/') || pathname.length <= 1) {
|
|
381
|
-
if (optional.index ?? true)
|
|
382
|
-
tidied.index = true
|
|
383
|
-
|
|
412
|
+
if (optional.index ?? true)
|
|
413
|
+
return tidied.index = true
|
|
414
|
+
} else {
|
|
415
|
+
const list = optional.custom
|
|
416
|
+
if (!list) return false
|
|
417
|
+
for (let reg of list) {
|
|
418
|
+
if (pathname.startsWith(`/${reg}/`))
|
|
419
|
+
return tidied.custom[reg] = true
|
|
384
420
|
}
|
|
385
421
|
}
|
|
386
422
|
}
|
|
@@ -446,7 +482,7 @@ const mergeUpdateWithOld = (newInfo, oldUpdate, tidied) => {
|
|
|
446
482
|
global: (oldUpdate?.global ?? 0) + (tidied.updateGlobal ? 1 : 0),
|
|
447
483
|
info: [newInfo]
|
|
448
484
|
}
|
|
449
|
-
const charLimit = pluginConfig.charLimit ?? 1024
|
|
485
|
+
const charLimit = pluginConfig.json.charLimit ?? 1024
|
|
450
486
|
if (JSON.stringify(result).length > charLimit) {
|
|
451
487
|
return {
|
|
452
488
|
global: result.global,
|
|
@@ -498,13 +534,13 @@ const zipInfo = (newInfo, oldInfo) => {
|
|
|
498
534
|
|
|
499
535
|
// 将更新推送到 info
|
|
500
536
|
const pushUpdateToInfo = (info, tidied) => {
|
|
537
|
+
const merges = [] // 要合并的更新
|
|
501
538
|
// 推送页面更新
|
|
502
|
-
if (tidied.page.size > (pluginConfig.maxHtml ?? 15)) {
|
|
539
|
+
if (tidied.page.size > (pluginConfig.json.maxHtml ?? 15)) {
|
|
503
540
|
// 如果 html 数量超过阈值就直接清掉所有 html
|
|
504
541
|
info.change.push({flag: 'html'})
|
|
505
542
|
} else {
|
|
506
543
|
const pages = [] // 独立更新
|
|
507
|
-
const merges = [] // 要合并的更新
|
|
508
544
|
tidied.page.forEach(it => pages.push(it))
|
|
509
545
|
if (tidied.tags) merges.push(config.tag_dir)
|
|
510
546
|
if (tidied.archives) merges.push(config.archive_dir)
|
|
@@ -513,11 +549,12 @@ const pushUpdateToInfo = (info, tidied) => {
|
|
|
513
549
|
pages.push(clipPageName(root, false))
|
|
514
550
|
merges.push('page')
|
|
515
551
|
}
|
|
516
|
-
if (merges.length > 0)
|
|
517
|
-
info.change.push({flag: 'str', value: merges.map(it => `/${it}/`)})
|
|
518
552
|
if (pages.length > 0)
|
|
519
553
|
info.change.push({flag: 'page', value: pages})
|
|
520
554
|
}
|
|
555
|
+
for (let key in tidied.custom) merges.push(key)
|
|
556
|
+
if (merges.length > 0)
|
|
557
|
+
info.change.push({flag: 'str', value: merges.map(it => `/${it}/`)})
|
|
521
558
|
// 推送文件更新
|
|
522
559
|
if (tidied.file.size > 0) {
|
|
523
560
|
const list = []
|
|
@@ -526,7 +563,7 @@ const pushUpdateToInfo = (info, tidied) => {
|
|
|
526
563
|
}
|
|
527
564
|
}
|
|
528
565
|
|
|
529
|
-
// 将 diff 整理分类,并将 expand
|
|
566
|
+
// 将 diff 整理分类,并将 expand 整合到结果中
|
|
530
567
|
const tidyDiff = (dif, expand) => {
|
|
531
568
|
const tidied = {
|
|
532
569
|
/** 所有 HTML 页面 */
|
|
@@ -541,10 +578,12 @@ const tidyDiff = (dif, expand) => {
|
|
|
541
578
|
categories: false,
|
|
542
579
|
/** 标记 index 是否更新 */
|
|
543
580
|
index: false,
|
|
581
|
+
/** 自定义配置项 */
|
|
582
|
+
custom: {},
|
|
544
583
|
/** 标记是否更新 global 版本号 */
|
|
545
584
|
updateGlobal: expand?.global
|
|
546
585
|
}
|
|
547
|
-
const mode = pluginConfig.precisionMode
|
|
586
|
+
const mode = pluginConfig.json.precisionMode
|
|
548
587
|
for (let it of dif) {
|
|
549
588
|
const url = new URL(nodePath.join(root, it)) // 当前文件的 URL
|
|
550
589
|
const cache = findCache(url) // 查询缓存
|
|
@@ -553,8 +592,8 @@ const tidyDiff = (dif, expand) => {
|
|
|
553
592
|
continue
|
|
554
593
|
}
|
|
555
594
|
if (!cache.clean) tidied.updateGlobal = true
|
|
595
|
+
if (isMerge(url.pathname, tidied)) continue
|
|
556
596
|
if (it.match(/(\/|\.html)$/)) { // 判断缓存是否是 html
|
|
557
|
-
if (isMerge(it, tidied)) continue
|
|
558
597
|
if (mode.html ?? false) tidied.page.add(url.pathname)
|
|
559
598
|
else tidied.page.add(clipPageName(url.href, !it.endsWith('/')))
|
|
560
599
|
} else {
|
|
@@ -598,22 +637,4 @@ function replaceDevRequest(url) {
|
|
|
598
637
|
}
|
|
599
638
|
}
|
|
600
639
|
return url
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
/** 对 hexo 的全局变量进行排序,以保证每次生成的结果一致 */
|
|
604
|
-
(() => {
|
|
605
|
-
const locals = hexo.locals
|
|
606
|
-
const compare = (a, b) => a < b ? -1 : 1
|
|
607
|
-
const sort = (name, value) => locals.get(name).data.sort((a, b) => compare(a[value], b[value]))
|
|
608
|
-
const list = {
|
|
609
|
-
posts: 'title',
|
|
610
|
-
pages: 'title',
|
|
611
|
-
tags: 'name',
|
|
612
|
-
categories: 'name'
|
|
613
|
-
}
|
|
614
|
-
for (let key in list) sort(key, list[key])
|
|
615
|
-
locals.get('posts').forEach(it => {
|
|
616
|
-
it.tags.data.sort((a, b) => compare(a.name, b.name))
|
|
617
|
-
it.categories.data.sort((a, b) => compare(a.name, b.name))
|
|
618
|
-
})
|
|
619
|
-
})()
|
|
640
|
+
}
|