chore: prepare for docs sidebar

This commit is contained in:
endiliey 2018-09-07 13:59:17 +08:00
parent 2141e6ea90
commit b477863a30
9 changed files with 350 additions and 2 deletions

View file

@ -1,4 +1,6 @@
const path = require('path');
const fm = require('front-matter');
const escapeStringRegexp = require('escape-string-regexp');
const fs = require('fs-extra');
const genPath = path.resolve(__dirname, '../core/generated');
@ -40,9 +42,40 @@ function fileToComponentName(file) {
return ext ? ext.toUpperCase() + str : str;
}
function idx(target, keyPaths) {
return (
target &&
(Array.isArray(keyPaths)
? keyPaths.reduce((obj, key) => obj && obj[key], target)
: target[keyPaths])
);
}
function getSubFolder(file, refDir) {
const separator = escapeStringRegexp(path.sep);
const baseDir = escapeStringRegexp(path.basename(refDir));
const regexSubFolder = new RegExp(
`${baseDir}${separator}(.*?)${separator}.*`
);
const match = regexSubFolder.exec(file);
return match && match[1];
}
function parse(fileString) {
if (!fm.test(fileString)) {
return {metadata: null, content: fileString};
}
const {attributes: metadata, body: content} = fm(fileString);
return {metadata, content};
}
module.exports = {
encodePath,
generate,
fileToPath,
fileToComponentName
fileToComponentName,
getSubFolder,
idx,
parse
};