-
Notifications
You must be signed in to change notification settings - Fork 0
/
after-build.js
82 lines (76 loc) · 2.44 KB
/
after-build.js
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
const fs = require('fs-extra')
const path = require('path')
const html = path.resolve(__dirname, 'docs/.vitepress/dist/index.html')
const htmlDir = path.resolve(__dirname, 'docs/.vitepress/dist/detail')
const json = require('./docs/.vitepress/sync-doc.json')
const algoliasearch = require('algoliasearch')
const addMeta = (file, id) => {
const item = id ? find(id) : null
fs.outputFile(
file,
fs.readFileSync(file, 'utf8')
.replace('</body>', `
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?6d281b558aa5550c06d5ab68b17b734f";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>`)
.replace('</head>', ` <meta name="google-site-verification" content="TbvyCK9sEBOqr5fAbXQ2uLNMgTDgn4wmpBM747LhOwk" />
<link rel="stylesheet" href="/gitalk/gitalk.css">
<script src="/gitalk/gitalk.min.js"></script>
</head>`)
.replace('</title>', item ? `</title>
<meta name="keywords" content="${item.tag.split('、').join(',')}" />` : '</title>')
.replace('jmingzi的个人博客', (item ? (item.tag.split('、').join(',') + ',') : '') + 'jmingzi的个人博客')
)
}
const find = (id) => {
for (const it of json) {
for (const child of it.items) {
if (child.id === id) {
return child
}
}
}
}
addMeta(html)
fs.readdirSync(htmlDir).forEach(h => {
addMeta(path.join(htmlDir, h), h.split('.')[0])
})
const algoliaResult = []
;(async () => {
const algoliaToken = process.env.ALGOLIA
if (!algoliaToken) {
console.log('algolia admin key is empty!')
return
}
const client = algoliasearch('QY2UJ6SVZF', algoliaToken)
const index = client.initIndex('ym')
for (const it of json) {
for (const child of it.items) {
// 读取文档内容
const content = await fs.readFile(path.resolve(__dirname, 'docs/detail', `${child.id}.md`), 'utf8')
algoliaResult.push({
type: 'lvl1',
url: `https://iming.work/detail/${child.id}.html`,
content: content,
hierarchy: {
lvl0: it.text,
lvl1: child.text
},
})
}
}
console.log('algolia 更新数据', algoliaResult.length, '条')
try {
await index.clearObjects()
await index.saveObjects(algoliaResult, { autoGenerateObjectIDIfNotExist: true })
} catch (e) {
console.log(e.message)
}
})()