forked from nacos-group/nacos-group.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-ebook.js
116 lines (100 loc) · 3.26 KB
/
build-ebook.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
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
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from 'url';
import dotenv from "dotenv";
dotenv.config();
const curFilename = fileURLToPath(import.meta.url);
const curDirname = path.dirname(curFilename);
const request = async (url) => {
const res = await fetch(url, { headers: { 'X-Auth-Token': process.env.YUQUE_TOKEN } });
if (!res.ok) throw new Error(res.statusText);
const data = await res.json();
if (data.error) {
throw new Error(data.error)
} else {
return data;
}
};
const toc = await request('https://www.yuque.com/api/v2/repos/1645741/toc')
const bookToc = toc.data;
let sidebar = []
const slugList = [
'kbyo6n', 'pb40qx', 'iez8a4', 'un9fgs', 'szf3gh', 'agxdnq', 'ktwggk', 'sufa23', 'ki4dgp', 'ynstox', 'knk2h0', 'qrkw0g', 'dl5k6n',
]
/**
* @description: 生成电子书SideBar侧边栏
* @param {*} tocItem
* @param {*} lock
* @returns
*/
function generateSlugSidebar(tocItem, lock) {
let itemToc = {
label: tocItem.title,
translations: {
en: ''
},
items: []
}
if (tocItem.slug !== '#') {
itemToc.link = `docs/ebook/${tocItem.slug}/`;
if (lock) {
itemToc = {
...itemToc,
attrs: {
lock: true
}
}
}
}
return itemToc
}
/**
* @description: 生成电子书内容
* @param {*} tocItem
*/
const generateEbookContent = async (tocItem) => {
let lock = undefined
if (tocItem.slug !== '#') {
slugList.includes(tocItem.slug) ? lock = false : lock = true;
}
if (tocItem.depth == '1') {
sidebar.push(generateSlugSidebar(tocItem, lock))
}
if (tocItem.depth == '2') {
sidebar[sidebar.length - 1].items.push(generateSlugSidebar(tocItem, lock))
}
if (tocItem.depth == '3') {
// 取最后一个对象
const lastItem = sidebar[sidebar.length - 1];
// 取最后一个对象的items中的最后一个item
const lastSecItem = lastItem.items[lastItem.items.length - 1];
lastSecItem.items.push(
generateSlugSidebar(tocItem, lock)
)
}
if (tocItem.slug) {
const bookDir = path.resolve(curDirname, 'src/content/docs/ebook/zh-cn');
fs.mkdir(bookDir, { recursive: true });
const getMdContent = (body, frontMatter={}) => `---
title: ${frontMatter.title || "Nacos Eook"}
keywords: [ Nacos ]
description: ${frontMatter.description || "Nacos Eook"}
---
${body}`
if (slugList.includes(tocItem.slug)) {
const bookItem = await request(`https://www.yuque.com/api/v2/repos/nacos/ebook/docs/${tocItem.slug}`);
const body = bookItem.data.body;
if (body) {
fs.writeFile(path.resolve(bookDir, `${tocItem.slug}.mdx`), getMdContent(body, bookItem.data), 'utf8')
}
} else if(tocItem.slug !== '#') {
fs.writeFile(path.resolve(bookDir, `${tocItem.slug}`), getMdContent(`
import Lock from "@components/common/EbookLock.astro";
<Lock />`), 'utf8')
}
}
}
for (const tocItem of bookToc) {
await generateEbookContent(tocItem);
}
fs.writeFile(path.resolve(curDirname, 'src/content/docs/ebook/_sidebar.json'), JSON.stringify(sidebar), 'utf8')