-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgulpfile.ts
221 lines (180 loc) · 6.86 KB
/
gulpfile.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import gulp from "gulp";
import fs from "fs";
import { join, basename } from "path";
import Gitdown from "gitdown";
import toc from "markdown-toc";
import gulpCopy from "gulp-copy";
import gulpClean from "gulp-clean";
const src = join(process.cwd(), "doc");
const out = join(process.cwd(), "wiki");
const sidebarName = "_Sidebar.md";
const readmeName = "README.md";
const readmeSrc = ".README";
const output: Record<string, string[]> = {};
interface File {
contents: any[];
fileName: string;
}
interface ToCEntry {
content: string;
lvl: number;
}
interface ToCCategory {
name?: string;
entries: ToCEntry[];
}
interface ToC {
categories: ToCCategory[];
}
gulp.task("gitdown", async () => {
const contents = fs.readdirSync(src);
let pageOutputFilename: string;
for (const categoryName of contents) {
const categoryPath = join(src, categoryName);
let pagePath = categoryPath;
const categoryStats = fs.statSync(categoryPath);
if (categoryStats.isDirectory()) {
const categoryPages = fs.readdirSync(categoryPath);
const categoryName = basename(categoryPath).substr(3);
console.log(`${categoryName}:`);
console.log(categoryPages);
for (const pageName of categoryPages) {
pagePath = join(categoryPath, pageName, "index.md");
pageOutputFilename = basename(pageName).substr(3);
await generateGitdownPage(
pagePath,
pageOutputFilename,
categoryName
);
}
} else {
pageOutputFilename = basename(categoryPath, ".md");
await generateGitdownPage(pagePath, pageOutputFilename, "default");
}
}
const gitdownReadme = await Gitdown.readFile(
join(process.cwd(), readmeSrc, readmeName)
);
await gitdownReadme.writeFile(join(process.cwd(), readmeName));
});
const generateGitdownPage = async (
pagePath: string,
pageOutputFilename: string,
categoryName: string
) => {
const gitdown = await Gitdown.readFile(pagePath);
const outputFile = join(out, pageOutputFilename + ".md");
console.log(`Generating ${basename(outputFile)}`);
await gitdown.writeFile(outputFile);
if (Object.keys(output).find((k) => k === categoryName)) {
output[categoryName].push(outputFile);
} else {
output[categoryName] = [];
output[categoryName].push(outputFile);
}
};
gulp.task("toc", async () => {
console.log(output);
const masterToC: ToCCategory[] = [
{
name: "Home",
entries: [
{
content: `[Home](Home)`,
lvl: 1,
},
],
},
];
for (const category in output) {
console.log(category);
let tocCategory = masterToC.find((c) => c.name === category);
if (tocCategory === undefined) {
masterToC.push({
name: category,
entries: [],
});
}
for (const file of output[category]) {
console.log(file);
const fileName = basename(file, ".md");
if (fileName !== basename(sidebarName, ".md")) {
const contents = fs.readFileSync(file).toString();
let contentsLines = contents.split(/\n\r?/g);
for (let i = 0; i < contentsLines.length; i++) {
const line = contentsLines[i];
const lineSearch = /^<!-- toc(?:(.*))? -->$/.exec(
line.trim()
);
if (lineSearch) {
const contentsBefore = contentsLines.slice(0, i);
const contentsAfter = contentsLines.slice(i + 1);
const tocOptions = {
maxDepth: 6,
headerSize: 2,
...(lineSearch[1] ? JSON.parse(lineSearch[1]) : {}),
};
const contentsToC = toc(contentsAfter.join("\n"), {
maxdepth: tocOptions.maxDepth,
});
contentsLines = contentsToC.content.length
? [
...contentsBefore,
"#".repeat(tocOptions.headerSize) +
" Table of Contents",
contentsToC.content,
...contentsAfter,
]
: [...contentsBefore, ...contentsAfter];
const title = fileName;
const url = fileName.replace(/ /g, "-");
let idx = masterToC.findIndex(
(c) => c.name === category
);
masterToC[idx].entries.push({
content: `[${title}](${url})`,
lvl: 1,
});
for (let pageItem of contentsToC.json) {
if (pageItem.lvl == 2) {
pageItem = toc.linkify(pageItem);
const linkEndIdx =
(pageItem.content as string).indexOf("](") +
2;
pageItem.content =
pageItem.content.substring(0, linkEndIdx) +
url +
pageItem.content.substring(linkEndIdx);
masterToC[idx].entries.push(pageItem);
}
}
}
}
fs.writeFileSync(file, contentsLines.join("\n"));
}
}
}
const sidebarExisting = fs.readFileSync(join(out, sidebarName));
let sidebarContent = sidebarExisting + "\n";
for (const category of masterToC) {
const categoryHeader =
category.name === "Home" || category.name === "default"
? ""
: `#### **${category.name}**\n`;
sidebarContent =
sidebarContent +
categoryHeader +
toc.bullets(category.entries, { highest: 1 }) +
"\n";
}
fs.writeFileSync(join(out, sidebarName), sidebarContent);
});
gulp.task("clean", () => gulp.src("wiki/*").pipe(gulpClean()));
gulp.task("copy-resources", () =>
gulp.src("doc/*/*/sections/*/**").pipe(gulpCopy("wiki", { prefix: 4 }))
);
gulp.task("doc", gulp.series("clean", "gitdown", "toc", "copy-resources"));
gulp.task("watcher", () => {
gulp.watch(["doc/**"], gulp.task("doc"));
});
gulp.task("watch", gulp.series("doc", "watcher"));