Initial API refactor

This commit is contained in:
Frank Li 2017-07-31 16:19:02 -07:00
parent 32768eea7e
commit 419e0c0ff9
8 changed files with 196 additions and 193 deletions

View file

@ -70,7 +70,7 @@ function execute() {
console.log("generate.js triggered...");
const regexSubFolder = /docs\/(.*)\/.*/;
const regexSubFolder = /translated_docs\/(.*)\/.*/;
const enabledLanguages = [];
languages.filter(lang => lang.enabled).map(lang => {
@ -82,27 +82,21 @@ function execute() {
let mdToHtml = {};
for (let i = 0; i < Metadata.length; i++) {
const metadata = Metadata[i];
mdToHtml["/docs/" + metadata.language + "/" + metadata.source] =
"/" + siteConfig.projectName + "/" + metadata.permalink;
if (metadata.language !== "en") {
continue;
}
mdToHtml[metadata.source] = siteConfig.baseUrl + metadata.permalink;
}
const DocsLayout = require("../core/DocsLayout.js");
fs.removeSync(__dirname + "/../../build");
fs.removeSync(CWD + "/build");
// create html files for all docs
// create html files for all English docs
let files = glob.sync(CWD + "/../docs/**");
files.forEach(file => {
// console.log(file);
let language = "en";
const match = regexSubFolder.exec(file);
if (match) {
language = match[1];
}
if (enabledLanguages.indexOf(language) === -1) {
return;
}
const extension = path.extname(file);
@ -120,6 +114,61 @@ function execute() {
rawContent = insertTableOfContents(rawContent);
}
/* replace any links to markdown files to their website html links */
Object.keys(mdToHtml).forEach(function(key, index) {
rawContent = rawContent.replace(
new RegExp(key, "g"),
mdToHtml[key].replace("/en/", "/" + language + "/")
);
});
const docComp = (
<DocsLayout metadata={metadata} language={language} config={siteConfig}>
{rawContent}
</DocsLayout>
);
const str = renderToStaticMarkup(docComp);
let targetFile =
CWD + "/build/" + siteConfig.projectName + "/" + metadata.permalink;
// console.log(targetFile);
writeFileAndCreateFolder(targetFile, str);
}
});
// create html files for all non-English docs
if (languages.length > 1) {
files = glob.sync(CWD + "/translated_docs/**");
files.forEach(file => {
let language = "en";
const match = regexSubFolder.exec(file);
if (match) {
language = match[1];
}
if (enabledLanguages.indexOf(language) === -1) {
return;
}
const extension = path.extname(file);
if (extension !== ".md" && extension !== ".markdown") {
return;
}
const result = readMetadata.processMetadata(file);
if (!result) {
return;
}
const metadata = result.metadata;
let rawContent = result.rawContent;
/* generate table of contents if appropriate */
if (rawContent && rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) != -1) {
rawContent = insertTableOfContents(rawContent);
}
/* replace any links to markdown files to their website html links */
Object.keys(mdToHtml).forEach(function(key, index) {
rawContent = rawContent.replace(new RegExp(key, "g"), mdToHtml[key]);
@ -131,18 +180,20 @@ function execute() {
</DocsLayout>
);
const str = renderToStaticMarkup(docComp);
let targetFile =
__dirname +
"/../../build" +
"/" +
siteConfig.projectName +
"/" +
metadata.permalink;
CWD + "/build/" + siteConfig.projectName + "/" + metadata.permalink;
// console.log(targetFile);
writeFileAndCreateFolder(targetFile, str);
}
});
});
}
/* copy docs assets if they exist */
if (fs.existsSync(CWD + "/../docs/assets")) {
fs.copySync(
CWD + "/../docs/assets",
CWD + "/build/" + siteConfig.projectName + "/docs/assets"
);
}
// create html files for all blog posts
if (fs.existsSync(__dirname + "../core/MetadataBlog.js")) {
@ -152,8 +203,13 @@ function execute() {
const MetadataBlog = require("../core/MetadataBlog.js");
const BlogPostLayout = require("../core/BlogPostLayout.js");
files = glob.sync(CWD + "/../blog/**/*.*");
files = glob.sync(CWD + "/blog/**/*.*");
files.sort().reverse().forEach(file => {
const extension = path.extname(file);
if (extension !== ".md" && extension !== ".markdown") {
return;
}
/* convert filename ot use slashes */
const filePath = path
.basename(file)
@ -185,13 +241,7 @@ function execute() {
const str = renderToStaticMarkup(blogPostComp);
let targetFile =
__dirname +
"/../../build" +
"/" +
siteConfig.projectName +
"/" +
"blog/" +
filePath;
CWD + "/build/" + siteConfig.projectName + "/blog/" + filePath;
writeFileAndCreateFolder(targetFile, str);
});
// create html files for all blog pages
@ -210,25 +260,32 @@ function execute() {
const str = renderToStaticMarkup(blogPageComp);
let targetFile =
__dirname +
"/../../build" +
"/" +
CWD +
"/build/" +
siteConfig.projectName +
"/" +
"blog" +
"/blog" +
(page > 0 ? "/page" + (page + 1) : "") +
"/index.html";
writeFileAndCreateFolder(targetFile, str);
}
/* copy blog assets if they exist */
if (fs.existsSync(CWD + "/blog/assets")) {
fs.copySync(
CWD + "/blog/assets",
CWD + "/build/" + siteConfig.projectName + "/blog/assets"
);
}
/* copy all static files from docusaurus */
files = glob.sync(__dirname + "/../static/**");
files.forEach(file => {
let targetFile = file.replace(
"/lib/static/",
"/build" + "/" + siteConfig.projectName + "/"
);
let targetFile =
CWD +
"/build/" +
siteConfig.projectName +
"/" +
file.split("/static/")[1];
if (file.match(/\.css$/)) {
let cssContent = fs.readFileSync(file);
cssContent = cssContent
@ -259,12 +316,7 @@ function execute() {
files.forEach(file => {
if (file.match(/\.css$/) && !isSeparateCss(file)) {
const mainCss =
__dirname +
"/../../build" +
"/" +
siteConfig.projectName +
"/" +
"css/main.css";
CWD + "/build/" + siteConfig.projectName + "/css/main.css";
let cssContent = fs.readFileSync(file);
cssContent = fs.readFileSync(mainCss) + "\n" + cssContent;
@ -287,12 +339,7 @@ function execute() {
} else if (!fs.lstatSync(file).isDirectory()) {
let parts = file.split("static");
let targetFile =
__dirname +
"/../../build" +
"/" +
siteConfig.projectName +
"/" +
parts[1];
CWD + "/build/" + siteConfig.projectName + "/" + parts[1];
mkdirp.sync(targetFile.replace(new RegExp("/[^/]*$"), ""));
fs.copySync(file, targetFile);
}
@ -315,7 +362,7 @@ function execute() {
const ReactComp = require(tempFile);
let targetFile =
__dirname + "/../../build/" + siteConfig.projectName + "/" + parts[1];
CWD + "/build/" + siteConfig.projectName + "/" + parts[1];
targetFile = targetFile.replace(/\.js$/, ".html");
const regexLang = /\/pages\/(.*)\//;
@ -364,21 +411,14 @@ function execute() {
} else if (!fs.lstatSync(file).isDirectory()) {
let parts = file.split("pages");
let targetFile =
__dirname +
"/../../build" +
"/" +
siteConfig.projectName +
"/" +
parts[1];
CWD + "/build/" + siteConfig.projectName + "/" + parts[1];
mkdirp.sync(targetFile.replace(new RegExp("/[^/]*$"), ""));
fs.copySync(file, targetFile);
}
});
/* copy html files in 'en' to base level as well */
files = glob.sync(
__dirname + "/../../build" + "/" + siteConfig.projectName + "/" + "en/**"
);
files = glob.sync(CWD + "/build/" + siteConfig.projectName + "/en/**");
files.forEach(file => {
let targetFile = file.replace("en/", "");
if (file.match(/\.html$/)) {