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

@ -101,7 +101,7 @@ function execute(port) {
reloadMetadata();
/* handle all requests for document pages */
// handle all requests for document pages
const app = express().get(/docs\/.*html$/, (req, res, next) => {
removeFromCache(CWD + "/siteConfig.js");
siteConfig = require(CWD + "/siteConfig.js");
@ -110,13 +110,15 @@ function execute(port) {
reloadMetadata();
// links is a map from a permalink to an id
// links is a map from a permalink to an id for each document
let links = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
links[metadata.permalink] = id;
});
// 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];
@ -140,6 +142,7 @@ function execute(port) {
}
const language = metadata.language;
// determine what file to use according to its id
let file;
if (metadata.original_id) {
if (ENABLE_TRANSLATION && metadata.language !== "en") {
@ -165,7 +168,7 @@ function execute(port) {
let rawContent = readMetadata.extractMetadata(fs.readFileSync(file, "utf8"))
.rawContent;
/* generate table of contents if appropriate */
// generate table of contents if appropriate
if (rawContent && rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) !== -1) {
rawContent = insertTableOfContents(rawContent);
}
@ -177,7 +180,7 @@ function execute(port) {
)[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 + "/");
@ -193,6 +196,7 @@ function execute(port) {
);
});
// replace any relative links to static assets to absolute links
rawContent = rawContent.replace(
/\]\(assets\//g,
"](" + siteConfig.baseUrl + "docs/assets/"
@ -209,7 +213,7 @@ function execute(port) {
res.send(renderToStaticMarkup(docComp));
});
/* handle all requests for blog pages and posts */
// handle all requests for blog pages and posts
app.get(/blog\/.*html$/, (req, res) => {
removeFromCache(CWD + "/siteConfig.js");
siteConfig = require(CWD + "/siteConfig.js");
@ -220,11 +224,11 @@ function execute(port) {
readMetadata.generateBlogMetadata();
const MetadataBlog = require("../core/MetadataBlog.js");
/* generate all of the blog pages */
// generate all of the blog pages
removeFromCache("../core/BlogPageLayout.js");
const BlogPageLayout = require("../core/BlogPageLayout.js");
const blogPages = {};
/* make blog pages with 10 posts per page */
// make blog pages with 10 posts per page
const perPage = 10;
for (
let page = 0;
@ -296,12 +300,12 @@ function execute(port) {
}
});
/* handle all other main pages */
// handle all other main pages
app.get("*.html", (req, res, next) => {
removeFromCache(CWD + "/siteConfig.js");
siteConfig = require(CWD + "/siteConfig.js");
/* look for user provided html file first */
// look for user provided html file first
let htmlFile = req.path.toString().replace(siteConfig.baseUrl, "");
htmlFile = CWD + "/pages/" + htmlFile;
if (
@ -317,7 +321,7 @@ function execute(port) {
return;
}
/* look for user provided react file either in specified path or in path for english files */
// look for user provided react file either in specified path or in path for english files
let file = req.path.toString().replace(/\.html$/, ".js");
file = file.replace(siteConfig.baseUrl, "");
let userFile = CWD + "/pages/" + file;
@ -340,8 +344,8 @@ function execute(port) {
englishFile = englishFile.replace("/" + language + "/", "/en/");
}
/* check for: a file for the page, an english file for page with unspecified language,
english file for the page */
// check for: a file for the page, an english file for page with unspecified language,
// english file for the page
if (
fs.existsSync(userFile) ||
fs.existsSync(
@ -352,7 +356,7 @@ function execute(port) {
) ||
fs.existsSync((userFile = englishFile))
) {
/* copy into docusaurus so require paths work */
// copy into docusaurus so require paths work
let parts = userFile.split("pages/");
let tempFile = __dirname + "/../pages/" + parts[1];
tempFile = tempFile.replace(
@ -362,7 +366,7 @@ function execute(port) {
mkdirp.sync(tempFile.replace(new RegExp("/[^/]*$"), ""));
fs.copySync(userFile, tempFile);
/* render into a string */
// render into a string
removeFromCache(tempFile);
const ReactComp = require(tempFile);
removeFromCache("../core/Site.js");
@ -383,7 +387,7 @@ function execute(port) {
}
});
/* generate the main.css file by concatenating user provided css to the end */
// generate the main.css file by concatenating user provided css to the end
app.get(/main\.css$/, (req, res) => {
const mainCssPath =
__dirname +
@ -422,6 +426,7 @@ function execute(port) {
res.send(cssContent);
});
// serve static assets from these locations
app.use(
siteConfig.baseUrl + "docs/assets/",
express.static(CWD + "/../docs/assets")
@ -433,6 +438,8 @@ function execute(port) {
app.use(siteConfig.baseUrl, express.static(CWD + "/static"));
app.use(siteConfig.baseUrl, express.static(__dirname + "/../static"));
// "redirect" requests to pages ending with "/" or no extension so that
// request to "...blog" returns same result as "...blog/index.html"
app.get(/\/[^\.]*\/?$/, (req, res) => {
if (req.path.toString().endsWith("/")) {
request.get(