-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
304 lines (273 loc) · 10.3 KB
/
index.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const sortBy = require("lodash.sortby");
const glob = require("glob");
const markdownIt = require("markdown-it");
const meta = require("markdown-it-meta");
const { lstatSync, readdirSync, readFileSync, existsSync } = require("fs");
const { join, normalize, sep } = require("path");
//const startCase = require("lodash.startcase");
const escapeRegExp = require("lodash.escaperegexp");
const slugify = require("transliteration").slugify;
const { titleize } = require("inflection");
const isDirectory = source => lstatSync(source).isDirectory();
const getDirectories = source =>
readdirSync(source).filter(name => !(name === ".vuepress") && isDirectory(join(source, name)));
const hasReadme = source =>
readdirSync(source).findIndex(name => name.toLowerCase() === "readme.md" && !isDirectory(join(source, name))) > -1;
/**
* Translate chinese to pinyin.
* Compatible with vuepress-pluin-permalink-pinyin.
* @param {Array} navArr
*/
function transliteratePinyin(navArr) {
return navArr.map(nav => {
const result = { ...nav };
if (nav.link) {
result.link = slugify(nav.link, { ignore: ["/", "."] });
}
if (nav.items) {
result.items = transliteratePinyin(nav.items);
}
return result;
});
}
/**
* Returns name to be used in menus after removing navigation prefix, prefix numbers used for ordering and `.`, `-`, `_` and spaces.
*
* @param {string} path - File path to get name for.
* @param {Object} options - Options
* @param {string} options.navPrefix - Navigation order prefix if present.
* @param {boolean} options.stripNumbers - Whether to strip numbers.
* @returns {string} - Name to be used in navigation.
* @example
* getName("/some/path/nav-01-how", { navPrefix: "nav", stripNumbers: true }); // how
* getName("/some/path/nav.01.how", { navPrefix: "nav", stripNumbers: true }); // how
*/
function getName(path, { navPrefix, stripNumbers } = {}) {
let name = path.split(sep).pop();
const argsIndex = name.lastIndexOf("--");
if (argsIndex > -1) {
name = name.substring(0, argsIndex);
}
if (navPrefix) {
// "nav.001.xyz" or "nav-001.xyz" or "nav_001.xyz" or "nav 001.xyz" -> "nav"
const pattern = new RegExp(`^${escapeRegExp(navPrefix)}[.\-_ ]?`);
name = name.replace(pattern, "");
}
if (stripNumbers) {
// "001.guide" or "001-guide" or "001_guide" or "001 guide" -> "guide"
name = name.replace(/^\d+[.\-_ ]?/, "");
}
return titleize(name.replace("-", " "));
}
// Load all MD files in a specified directory and order by metadata 'order' value
function getChildren(parent_path, dir, recursive = true) {
// CREDITS: https://github.com/benjivm (from: https://github.com/vuejs/vuepress/issues/613#issuecomment-495751473)
parent_path = normalize(parent_path);
parent_path = parent_path.endsWith(sep) ? parent_path.slice(0, -1) : parent_path; // Remove last / if exists.
const pattern = recursive ? "/**/*.md" : "/*.md";
files = glob.sync(parent_path + (dir ? `/${dir}` : "") + pattern).map(path => {
// Instantiate MarkdownIt
md = new markdownIt();
// Add markdown-it-meta
md.use(meta);
// Get the order value
file = readFileSync(path, "utf8");
md.render(file);
order = md.meta.order;
// Remove "parent_path" and ".md"
path = path.slice(parent_path.length + 1, -3);
// Remove "README", making it the de facto index page
if (path.toLowerCase().endsWith("readme")) {
path = path.slice(0, -6);
}
return {
path,
order: path === "" && order === undefined ? 0 : order // README is first if it hasn't order
};
});
// Return the ordered list of files, sort by 'order' then 'path'
return sortBy(files, ["order", "path"]).map(file => file.path);
}
/**
* Return sidebar config for given baseDir.
*
* @param {String} baseDir - Absolute path of directory to get sidebar config for.
* @param {Object} options - Options
* @param {String} relativeDir - Relative directory to add to baseDir
* @param {Number} currentLevel - Current level of items.
* @returns {Array.<String|Object>} - Recursion level
*/
function side(
baseDir,
{ stripNumbers, maxLevel, navPrefix, skipEmptySidebar, addReadMeToFirstGroup, mixDirectoriesAndFilesAlphabetically },
relativeDir = "",
currentLevel = 1
) {
const fileLinks = getChildren(baseDir, relativeDir, currentLevel > maxLevel);
if (currentLevel <= maxLevel) {
getDirectories(join(baseDir, relativeDir))
.filter(subDir => !subDir.startsWith(navPrefix))
.forEach(subDir => {
const children = side(
baseDir,
{ stripNumbers, maxLevel, navPrefix, skipEmptySidebar },
join(relativeDir, subDir),
currentLevel + 1
);
if (children.length > 0 || !skipEmptySidebar) {
// Where to put '02-folder' in ['01-file', { title: 'Other Folder', children: ['03-folder/file'] }]
const sortedFolderPosition = fileLinks.findIndex(
link => subDir < (link.children ? (link.children[0] || "").split(sep)[0] : link)
);
const insertPosition =
mixDirectoriesAndFilesAlphabetically && sortedFolderPosition > -1 ? sortedFolderPosition : fileLinks.length;
fileLinks.splice(insertPosition, 0, {
title: getName(subDir, { stripNumbers, navPrefix }),
...parseSidebarParameters(subDir),
children
});
}
});
}
// Remove README.md from first position and add it to first group.
if (addReadMeToFirstGroup && fileLinks[0] === "" && typeof fileLinks[1] === "object") {
fileLinks.shift();
fileLinks[0].children.unshift("");
}
return fileLinks;
}
/**
* Gets sidebar parameters from directory name. Arguments are given after double dash `--` and separated by comma.
* - `nc` sets collapsable to `false`.
* - `dX` sets sidebarDepth to `X`.
*
* @param {String} dirname - Name of the directory.
* @returns {Object} - sidebar parameters.
* @example
* parseSidebarParameters("docs/api--nc,d2"); { collapsable: false, sidebarDepth: 2 }
*/
function parseSidebarParameters(dirname) {
const index = dirname.lastIndexOf("--");
if (index === -1) {
return {};
}
const args = dirname.substring(index + 2).split(",");
const parameters = {};
args.forEach(arg => {
if (arg === "nc") {
parameters.collapsable = false;
} else if (arg.match(/d\d+/)) {
parameters.sidebarDepth = Number(arg.substring(1));
}
});
return parameters;
}
/**
* Returns navbar configuration for given path.
* @param {String} rootDir - Path of the directory to get navbar configuration for.
* @param {OBject} options - Options
* @param {String} relativeDir - (Used internally for recursion) Relative directory to `rootDir` to get navconfig for.
* @param {Number} currentNavLevel - (Used internally for recursion) Recursion level.
* @returns {Array.<Object>}
*/
function nav(rootDir, { navPrefix, stripNumbers, skipEmptyNavbar }, relativeDir = "/", currentNavLevel = 1) {
relativeDir = relativeDir.replace(/\\/g, "/");
const baseDir = join(rootDir, relativeDir);
const childrenDirs = getDirectories(baseDir).filter(subDir => subDir.startsWith(navPrefix));
const options = { navPrefix, stripNumbers, skipEmptyNavbar };
let result;
if (currentNavLevel > 1 && childrenDirs.length === 0) {
if (!hasReadme(baseDir)) {
if (skipEmptyNavbar) {
return;
} else {
throw new Error(
`README.md file cannot be found in ${baseDir}. VuePress would return 404 for that NavBar link.`
);
}
}
result = { text: getName(baseDir, { stripNumbers, navPrefix }), link: relativeDir + "/" };
} else if (childrenDirs.length > 0) {
const items = childrenDirs
.map(subDir => nav(rootDir, options, join(relativeDir, subDir), currentNavLevel + 1))
.filter(Boolean);
result = currentNavLevel === 1 ? items : { text: getName(baseDir, { stripNumbers, navPrefix }), items };
}
return result;
}
/**
* Returns multiple sidebars for given directory.
* @param {String} rootDir - Directory to get navbars for.
* @param {Object} nav - Navigation configuration (Used for calculating sidebars' roots.)
* @param {Object} options - Options
* @param {Number} currentLevel - Recursion level.
* @returns {Object} - Multiple navbars.
*/
function multiSide(rootDir, nav, options, currentLevel = 1) {
const sideBar = {};
nav.forEach(navItem => {
if (navItem.link) {
sideBar[navItem.link] = side(join(rootDir, navItem.link), options);
} else {
Object.assign(sideBar, multiSide(rootDir, navItem.items, options), currentLevel + 1);
}
});
if (options.skipEmptySidebar) {
Object.keys(sideBar).forEach(key => {
if (sideBar[key].length === 0) {
delete sideBar[key];
}
});
}
if (currentLevel === 1) {
const fallBackSide = side(rootDir, options);
if (!options.skipEmptySidebar || fallBackSide.length > 0) {
sideBar["/"] = side(rootDir, options);
}
}
return sideBar;
}
/**
* Returns `nav` and `sidebar` configuration for VuePress calculated using structrue of directory and files in given path.
* @param {String} rootDir - Directory to get configuration for.
* @param {Object} options - Options
* @returns {Object} - { nav: ..., sidebar: ... } configuration.
*/
function getConfig(
rootDir,
{
stripNumbers = true,
maxLevel = 2,
navPrefix = "nav",
skipEmptySidebar = true,
skipEmptyNavbar = true,
multipleSideBar = true,
addReadMeToFirstGroup = true,
mixDirectoriesAndFilesAlphabetically = true,
pinyinNav
} = {}
) {
rootDir = normalize(rootDir);
rootDir = rootDir.endsWith(sep) ? rootDir.slice(0, -1) : rootDir; // Remove last / if exists.
const options = {
stripNumbers,
maxLevel,
navPrefix,
skipEmptySidebar,
skipEmptyNavbar,
multipleSideBar,
addReadMeToFirstGroup,
mixDirectoriesAndFilesAlphabetically,
pinyinNav
};
const navItems = nav(rootDir, options);
const result = {
nav: navItems || [],
sidebar: multipleSideBar && navItems ? multiSide(rootDir, navItems, options) : side(rootDir, options)
};
if (options.pinyinNav && nav.length) {
result.nav = transliteratePinyin(result.nav);
}
return result;
}
module.exports = getConfig;