Skip to content

Commit

Permalink
[WIP] Allow custom subpath for docs within the "docs" folder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JoelMarcey committed Nov 9, 2017
1 parent 9f32a28 commit dc0c139
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
);
}
Expand Down
20 changes: 16 additions & 4 deletions lib/server/readMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -190,7 +201,7 @@ function generateMetadataDocs() {
console.error(e);
process.exit(1);
}

const regexSubFolder = /translated_docs\/(.*)\/.*/;

const enabledLanguages = [];
Expand All @@ -202,15 +213,15 @@ 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";

const extension = path.extname(file);

if (extension === ".md" || extension === ".markdown") {
const res = processMetadata(file);

if (!res) {
return;
}
Expand Down Expand Up @@ -389,6 +400,7 @@ function generateMetadataBlog() {
}

module.exports = {
getDocsPath,
readSidebar,
extractMetadata,
processMetadata,
Expand Down
4 changes: 2 additions & 2 deletions lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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/",
Expand Down
2 changes: 1 addition & 1 deletion lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
4 changes: 2 additions & 2 deletions lib/write-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit dc0c139

Please sign in to comment.