mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-24 03:58:49 +02:00
Add comments
This commit is contained in:
parent
df0f95e36f
commit
43ef3c8142
22 changed files with 114 additions and 62 deletions
|
@ -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);
|
||||
|
|
|
@ -23,6 +23,8 @@ if (fs.existsSync(CWD + "/languages.js")) {
|
|||
}
|
||||
];
|
||||
}
|
||||
|
||||
// returns data broken up into categories for a sidebar
|
||||
function readCategories(sidebar) {
|
||||
const enabledLanguages = [];
|
||||
languages.filter(lang => lang.enabled).map(lang => {
|
||||
|
|
|
@ -30,6 +30,7 @@ if (fs.existsSync(CWD + "/languages.js")) {
|
|||
];
|
||||
}
|
||||
|
||||
// returns map from id to object containing sidebar ordering info
|
||||
function readSidebar() {
|
||||
let allSidebars;
|
||||
if (fs.existsSync(CWD + "/sidebars.json")) {
|
||||
|
@ -69,6 +70,7 @@ function readSidebar() {
|
|||
return order;
|
||||
}
|
||||
|
||||
// split markdown header
|
||||
function splitHeader(content) {
|
||||
const lines = content.split("\n");
|
||||
let i = 1;
|
||||
|
@ -173,6 +175,7 @@ function processMetadata(file) {
|
|||
return { metadata, rawContent: rawContent };
|
||||
}
|
||||
|
||||
// process metadata for all docs and save into core/metadata.js
|
||||
function generateDocsMetadata() {
|
||||
const order = readSidebar();
|
||||
|
||||
|
@ -185,7 +188,7 @@ function generateDocsMetadata() {
|
|||
|
||||
const metadatas = {};
|
||||
|
||||
/* metadata for english files */
|
||||
// metadata for english files
|
||||
let files = glob.sync(CWD + "/../docs/**");
|
||||
files.forEach(file => {
|
||||
let language = "en";
|
||||
|
@ -202,7 +205,7 @@ function generateDocsMetadata() {
|
|||
}
|
||||
});
|
||||
|
||||
/* metadata for non-english docs */
|
||||
// metadata for non-english docs
|
||||
files = glob.sync(CWD + "/translated_docs/**");
|
||||
files.forEach(file => {
|
||||
let language = "en";
|
||||
|
@ -227,6 +230,7 @@ function generateDocsMetadata() {
|
|||
}
|
||||
});
|
||||
|
||||
// metadata for versioned docs
|
||||
const versionData = versionFallback.docData();
|
||||
versionData.forEach(metadata => {
|
||||
const id = metadata.localized_id;
|
||||
|
@ -262,6 +266,7 @@ function generateDocsMetadata() {
|
|||
);
|
||||
}
|
||||
|
||||
// process metadata for blog posts and save into core/MetadataBlog.js
|
||||
function generateBlogMetadata() {
|
||||
const metadatas = [];
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -1,3 +1,14 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
// translation object contains all translations for each string in 18n/en.json
|
||||
|
||||
const CWD = process.cwd();
|
||||
const fs = require("fs");
|
||||
const glob = require("glob");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue