From dc0c1390c469c6f7fb956f55ecd81ba39165414b Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Wed, 8 Nov 2017 20:16:05 -0800 Subject: [PATCH] [WIP] Allow custom subpath for docs within the "docs" folder e.g., The default is `docs/*.md` This allows `docs/somedir/*.md` or `docs/somedir/anotherdir/*.md` Notes: - All URLs are still /docs/*.html (i.e. the subpath does not get preserved in the link). - Files in `translated_docs`, if any, will still only be one level - This should not affect translations or versioning --- lib/server/generate.js | 6 +++--- lib/server/readMetadata.js | 20 ++++++++++++++++---- lib/server/server.js | 4 ++-- lib/version.js | 2 +- lib/write-translations.js | 4 ++-- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/server/generate.js b/lib/server/generate.js index b919651f26f9..262af559795e 100644 --- a/lib/server/generate.js +++ b/lib/server/generate.js @@ -129,7 +129,7 @@ function execute() { } } else { if (metadata.language === "en") { - file = CWD + "/../docs/" + metadata.source; + file = CWD + "/../" + readMetadata.getDocsPath() + "/" + metadata.source; } else { file = CWD + "/translated_docs/" + metadata.language + "/" + metadata.source; @@ -219,9 +219,9 @@ function execute() { }); // copy docs assets if they exist - if (fs.existsSync(CWD + "/../docs/assets")) { + if (fs.existsSync(CWD + "/../" + readMetadata.getDocsPath() + "/assets")) { fs.copySync( - CWD + "/../docs/assets", + CWD + "/../" + readMetadata.getDocsPath() + "/assets", CWD + "/build/" + siteConfig.projectName + "/docs/assets" ); } diff --git a/lib/server/readMetadata.js b/lib/server/readMetadata.js index 2d7421e2614a..a24780c670fa 100644 --- a/lib/server/readMetadata.js +++ b/lib/server/readMetadata.js @@ -16,6 +16,8 @@ const versionFallback = require("./versionFallback.js"); const ENABLE_VERSIONING = fs.existsSync(CWD + "/versions.json"); + + let languages; if (fs.existsSync(CWD + "/languages.js")) { languages = require(CWD + "/languages.js"); @@ -29,6 +31,15 @@ if (fs.existsSync(CWD + "/languages.js")) { ]; } +// Can add additional path information to the docs folder +// e.g., docs/whereDocsReallyExist +// All .md docs still (currently) must be in one flat directory hierarchy. +// e.g., docs/whereDocsReallyExist/*.md (all .md files in this dir) +function getDocsPath() { + return siteConfig.docsAdditionalPath + ? "docs" + siteConfig.docsAdditionalPath + : "docs"; +} // returns map from id to object containing sidebar ordering info function readSidebar() { let allSidebars; @@ -114,7 +125,7 @@ function extractMetadata(content) { function processMetadata(file) { const result = extractMetadata(fs.readFileSync(file, "utf8")); - const regexSubFolder = /docs\/(.*)\/.*/; + let regexSubFolder = new RegExp("/" + getDocsPath() + "\/(.*)\/.*/"); let language = "en"; const match = regexSubFolder.exec(file); @@ -190,7 +201,7 @@ function generateMetadataDocs() { console.error(e); process.exit(1); } - + const regexSubFolder = /translated_docs\/(.*)\/.*/; const enabledLanguages = []; @@ -202,7 +213,7 @@ function generateMetadataDocs() { const defaultMetadatas = {}; // metadata for english files - let files = glob.sync(CWD + "/../docs/**"); + let files = glob.sync(CWD + "/../" + getDocsPath() + "/**"); files.forEach(file => { let language = "en"; @@ -210,7 +221,7 @@ function generateMetadataDocs() { if (extension === ".md" || extension === ".markdown") { const res = processMetadata(file); - + if (!res) { return; } @@ -389,6 +400,7 @@ function generateMetadataBlog() { } module.exports = { + getDocsPath, readSidebar, extractMetadata, processMetadata, diff --git a/lib/server/server.js b/lib/server/server.js index 5585e47f6357..684a4e14e21c 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -177,7 +177,7 @@ function execute(port) { } } else { if (metadata.language === "en") { - file = CWD + "/../docs/" + metadata.source; + file = CWD + "/../" + readMetadata.getDocsPath() + "/" + metadata.source; } else { file = CWD + "/translated_docs/" + metadata.language + "/" + metadata.source; @@ -485,7 +485,7 @@ function execute(port) { // serve static assets from these locations app.use( siteConfig.baseUrl + "docs/assets/", - express.static(CWD + "/../docs/assets") + express.static(CWD + "/../" + readMetadata.getDocsPath() + "/assets") ); app.use( siteConfig.baseUrl + "blog/assets/", diff --git a/lib/version.js b/lib/version.js index 429aac192878..b1e003da9273 100755 --- a/lib/version.js +++ b/lib/version.js @@ -65,7 +65,7 @@ const versionFolder = CWD + "/versioned_docs/version-" + version; mkdirp.sync(versionFolder); // copy necessary files to new version, changing some of its metadata to reflect the versioning -let files = glob.sync(CWD + "/../docs/*"); +let files = glob.sync(CWD + "/../" + readMetadata.getDocsPath() + "/*"); files.forEach(file => { const ext = path.extname(file); if (ext !== ".md" && ext !== ".markdown") { diff --git a/lib/write-translations.js b/lib/write-translations.js index 117501ff02de..b973c74a88c8 100755 --- a/lib/write-translations.js +++ b/lib/write-translations.js @@ -48,7 +48,7 @@ function execute() { }; // look through markdown headers of docs for titles and categories to translate - let files = glob.sync(CWD + "/../docs/**"); + let files = glob.sync(CWD + "/../" + readMetadata.getDocsPath() + "/**"); files.forEach(file => { const extension = path.extname(file); if (extension === ".md" || extension === ".markdown") { @@ -91,7 +91,7 @@ function execute() { files.forEach(file => { if (!file.endsWith("-sidebars.json")) { if (file.endsWith("-sidebar.json")) { - console.warn(`Skipping ${file}. Make sure your sidebar filenames follow this format: 'version-VERSION-sidebars.json'.`); + console.warn(`Skipping ${file}. Make sure your sidebar filenames follow this format: 'version-VERSION-sidebars.json'.`); } return; }