Add comments

This commit is contained in:
Frank Li 2017-08-15 16:55:38 -07:00
parent df0f95e36f
commit 43ef3c8142
22 changed files with 114 additions and 62 deletions

View file

@ -38,7 +38,7 @@ function execute() {
}
];
}
// create the folder path for a file if it does not exist, then write the file
function writeFileAndCreateFolder(file, content) {
mkdirp.sync(file.replace(new RegExp("/[^/]*$"), ""));
@ -47,6 +47,8 @@ function execute() {
const TABLE_OF_CONTENTS_TOKEN = "<AUTOGENERATED_TABLE_OF_CONTENTS>";
// takes the content of a doc article and returns the content with a table of
// contents inserted
const insertTableOfContents = rawContent => {
const regexp = /\n###\s+(`.*`.*)\n/g;
let match;
@ -62,6 +64,8 @@ function execute() {
return rawContent.replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents);
};
// returns true if a file should be excluded from concatentation to
// default Docusaurus styles
function isSeparateCss(file) {
if (!siteConfig.separateCss) {
return false;
@ -76,6 +80,7 @@ function execute() {
console.log("generate.js triggered...");
// array of tags of enabled languages
const enabledLanguages = [];
languages.filter(lang => lang.enabled).map(lang => {
enabledLanguages.push(lang.tag);
@ -84,6 +89,8 @@ function execute() {
readMetadata.generateDocsMetadata();
const Metadata = require("../core/metadata.js");
// mdToHtml is a map from a markdown file name to its html link, used to
// change relative markdown links that work on GitHub into actual site links
const mdToHtml = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
@ -104,10 +111,10 @@ function execute() {
fs.removeSync(CWD + "/build");
// create html files for all docs
// create html files for all docs by going through all doc ids
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
// determine what file to use according to its id
let file;
if (metadata.original_id) {
if (ENABLE_TRANSLATION && metadata.language !== "en") {
@ -134,7 +141,7 @@ function execute() {
const language = metadata.language;
/* generate table of contents if appropriate */
// generate table of contents if appropriate
if (rawContent && rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) != -1) {
rawContent = insertTableOfContents(rawContent);
}
@ -146,7 +153,7 @@ function execute() {
)[0];
}
/* replace any links to markdown files to their website html links */
// replace any links to markdown files to their website html links
Object.keys(mdToHtml).forEach(function(key, index) {
let link = mdToHtml[key];
link = link.replace("/en/", "/" + language + "/");
@ -158,7 +165,8 @@ function execute() {
);
rawContent = rawContent.replace(new RegExp(key, "g"), link);
});
// replace any relative links to static assets to absolute links
rawContent = rawContent.replace(
/\]\(assets\//g,
"](" + siteConfig.baseUrl + "docs/assets/"
@ -176,7 +184,7 @@ function execute() {
writeFileAndCreateFolder(targetFile, str);
});
/* copy docs assets if they exist */
// copy docs assets if they exist
if (fs.existsSync(CWD + "/../docs/assets")) {
fs.copySync(
CWD + "/../docs/assets",
@ -184,7 +192,7 @@ function execute() {
);
}
// create html files for all blog posts
// create html files for all blog posts (each article)
if (fs.existsSync(__dirname + "../core/MetadataBlog.js")) {
fs.removeSync(__dirname + "../core/MetadataBlog.js");
}
@ -199,7 +207,7 @@ function execute() {
return;
}
/* convert filename ot use slashes */
// convert filename to use slashes
const filePath = path
.basename(file)
.replace("-", "/")
@ -233,7 +241,7 @@ function execute() {
CWD + "/build/" + siteConfig.projectName + "/blog/" + filePath;
writeFileAndCreateFolder(targetFile, str);
});
// create html files for all blog pages
// create html files for all blog pages (collections of article previews)
const BlogPageLayout = require("../core/BlogPageLayout.js");
const perPage = 10;
for (let page = 0; page < Math.ceil(MetadataBlog.length / perPage); page++) {
@ -258,7 +266,7 @@ function execute() {
writeFileAndCreateFolder(targetFile, str);
}
/* copy blog assets if they exist */
// copy blog assets if they exist
if (fs.existsSync(CWD + "/blog/assets")) {
fs.copySync(
CWD + "/blog/assets",
@ -266,7 +274,7 @@ function execute() {
);
}
/* copy all static files from docusaurus */
// copy all static files from docusaurus
files = glob.sync(__dirname + "/../static/**");
files.forEach(file => {
let targetFile =
@ -275,6 +283,7 @@ function execute() {
siteConfig.projectName +
"/" +
file.split("/static/")[1];
// parse css files to replace colors according to siteConfig
if (file.match(/\.css$/)) {
let cssContent = fs.readFileSync(file, "utf8");
@ -304,9 +313,10 @@ function execute() {
}
});
/* copy all static files from user */
// copy all static files from user
files = glob.sync(CWD + "/static/**");
files.forEach(file => {
// parse css files to replace colors according to siteConfig
if (file.match(/\.css$/) && !isSeparateCss(file)) {
const mainCss =
CWD + "/build/" + siteConfig.projectName + "/css/main.css";
@ -328,11 +338,12 @@ function execute() {
}
});
/* compile/copy pages from user */
// compile/copy pages from user
files = glob.sync(CWD + "/pages/**");
files.forEach(file => {
// render .js files to strings
if (file.match(/\.js$/)) {
/* make temp file for sake of require paths */
// make temp file for sake of require paths
const parts = file.split("pages");
let tempFile = __dirname + "/../pages" + parts[1];
tempFile = tempFile.replace(
@ -352,10 +363,10 @@ function execute() {
const match = regexLang.exec(file);
const langParts = match[1].split("/");
if (langParts.indexOf("en") !== -1) {
/* copy and compile a page for each enabled language from the English file */
// copy and compile a page for each enabled language from the English file
for (let i = 0; i < enabledLanguages.length; i++) {
let language = enabledLanguages[i];
/* skip conversion from english file if a file exists for this language */
// skip conversion from english file if a file exists for this language
if (
language !== "en" &&
fs.existsSync(file.replace("/en/", "/" + language + "/"))
@ -374,7 +385,7 @@ function execute() {
);
}
} else {
/* allow for rendering of other files not in pages/en folder */
// allow for rendering of other files not in pages/en folder
let language = "en";
for (let i = 0; i < langParts.length; i++) {
if (enabledLanguages.indexOf(langParts[i]) !== -1) {
@ -392,6 +403,7 @@ function execute() {
fs.removeSync(tempFile);
} else if (!fs.lstatSync(file).isDirectory()) {
// copy other non .js files
let parts = file.split("pages");
let targetFile =
CWD + "/build/" + siteConfig.projectName + "/" + parts[1];
@ -400,7 +412,7 @@ function execute() {
}
});
/* copy html files in 'en' to base level as well */
// copy html files in 'en' to base level as well
files = glob.sync(CWD + "/build/" + siteConfig.projectName + "/en/**");
files.forEach(file => {
let targetFile = file.replace("en/", "");
@ -409,7 +421,7 @@ function execute() {
}
});
/* Generate CNAME file if a custom domain is specified in siteConfig */
// Generate CNAME file if a custom domain is specified in siteConfig
if (siteConfig.cname) {
let targetFile = CWD + "/build/" + siteConfig.projectName + "/CNAME";
fs.writeFileSync(targetFile, siteConfig.cname);