mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 16:17:25 +02:00
Initial API refactor
This commit is contained in:
parent
32768eea7e
commit
419e0c0ff9
8 changed files with 196 additions and 193 deletions
|
@ -22,6 +22,7 @@ function execute(port) {
|
|||
const glob = require("glob");
|
||||
const translate = require("./translate.js");
|
||||
let siteConfig = require(CWD + "/siteConfig.js");
|
||||
const ENABLE_TRANSLATION = fs.existsSync(CWD + "/languages.js");
|
||||
|
||||
/**
|
||||
* Removes a module from the cache
|
||||
|
@ -78,7 +79,6 @@ function execute(port) {
|
|||
readMetadata.generateDocsMetadata();
|
||||
purgeCache("../core/metadata.js");
|
||||
Metadata = require("../core/metadata.js");
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
|
@ -121,7 +121,7 @@ function execute(port) {
|
|||
reloadMetadata();
|
||||
|
||||
/* handle all requests for document pages */
|
||||
const app = express().get(/docs\/[\s\S]*html$/, (req, res, next) => {
|
||||
const app = express().get(/docs\/.*html$/, (req, res, next) => {
|
||||
purgeCache(CWD + "/siteConfig.js");
|
||||
siteConfig = require(CWD + "/siteConfig.js");
|
||||
|
||||
|
@ -131,23 +131,29 @@ function execute(port) {
|
|||
let links = {};
|
||||
for (let i = 0; i < Metadata.length; i++) {
|
||||
const metadata = Metadata[i];
|
||||
links[metadata.permalink] =
|
||||
"docs/" + metadata.language + "/" + metadata.source;
|
||||
if (metadata.language === "en") {
|
||||
links[metadata.permalink] = CWD + "/../docs/" + metadata.source;
|
||||
} else {
|
||||
links[metadata.permalink] =
|
||||
CWD + "/translated_docs/" + metadata.language + "/" + metadata.source;
|
||||
}
|
||||
}
|
||||
let mdToHtml = {};
|
||||
for (let i = 0; i < Metadata.length; i++) {
|
||||
const metadata = Metadata[i];
|
||||
mdToHtml["/docs/" + metadata.language + "/" + metadata.source] =
|
||||
siteConfig.baseUrl + metadata.permalink;
|
||||
if (metadata.language !== "en") {
|
||||
continue;
|
||||
}
|
||||
mdToHtml[metadata.source] = siteConfig.baseUrl + metadata.permalink;
|
||||
}
|
||||
console.log(mdToHtml);
|
||||
|
||||
let file = links[req.path.toString().replace(siteConfig.baseUrl, "")];
|
||||
file = CWD + "/../" + file;
|
||||
console.log(file);
|
||||
if (!fs.existsSync(file)) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
console.log(file);
|
||||
const result = readMetadata.processMetadata(file);
|
||||
|
||||
const metadata = result.metadata;
|
||||
|
@ -161,9 +167,17 @@ function execute(port) {
|
|||
|
||||
/* 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]);
|
||||
rawContent = rawContent.replace(
|
||||
new RegExp(key, "g"),
|
||||
mdToHtml[key].replace("/en/", "/" + language + "/")
|
||||
);
|
||||
});
|
||||
|
||||
rawContent = rawContent.replace(
|
||||
/\]\(assets\//g,
|
||||
"](" + siteConfig.baseUrl + "docs/assets/"
|
||||
);
|
||||
|
||||
purgeCache("../core/DocsLayout.js");
|
||||
const DocsLayout = require("../core/DocsLayout.js");
|
||||
const docComp = (
|
||||
|
@ -175,7 +189,9 @@ function execute(port) {
|
|||
res.send(renderToStaticMarkup(docComp));
|
||||
});
|
||||
/* handle all requests for blog pages and posts */
|
||||
app.get(/blog\/[\s\S]*html$/, (req, res) => {
|
||||
app.get(/blog\/.*html$/, (req, res) => {
|
||||
console.log(req.path);
|
||||
|
||||
purgeCache(CWD + "/siteConfig.js");
|
||||
siteConfig = require(CWD + "/siteConfig.js");
|
||||
|
||||
|
@ -229,12 +245,16 @@ function execute(port) {
|
|||
let file = parts[1];
|
||||
file = file.replace(/\.html$/, ".md");
|
||||
file = file.replace(new RegExp("/", "g"), "-");
|
||||
file = CWD + "/../blog/" + file;
|
||||
file = CWD + "/blog/" + file;
|
||||
|
||||
const result = readMetadata.extractMetadata(
|
||||
fs.readFileSync(file, { encoding: "utf8" })
|
||||
);
|
||||
const rawContent = result.rawContent;
|
||||
let rawContent = result.rawContent;
|
||||
rawContent = rawContent.replace(
|
||||
/\]\(assets\//g,
|
||||
"](" + siteConfig.baseUrl + "blog/assets/"
|
||||
);
|
||||
const metadata = Object.assign(
|
||||
{ path: req.path.toString().split("blog/")[1], content: rawContent },
|
||||
result.metadata
|
||||
|
@ -385,6 +405,14 @@ function execute(port) {
|
|||
});
|
||||
|
||||
/* serve static content first from user folder then from docusaurus */
|
||||
app.use(
|
||||
siteConfig.baseUrl + "docs/assets/",
|
||||
express.static(CWD + "/../docs/assets")
|
||||
);
|
||||
app.use(
|
||||
siteConfig.baseUrl + "blog/assets/",
|
||||
express.static(CWD + "/blog/assets")
|
||||
);
|
||||
app.use(siteConfig.baseUrl, express.static(CWD + "/static"));
|
||||
app.use(siteConfig.baseUrl, express.static(__dirname + "/../static"));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue