mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
Enable sub-directories in docs/ (#705)
This commit is contained in:
parent
49c27b733b
commit
d04b3ca87b
10 changed files with 135 additions and 36 deletions
|
@ -15,7 +15,7 @@ const chalk = require('chalk');
|
|||
const env = require('./env.js');
|
||||
const siteConfig = require(CWD + '/siteConfig.js');
|
||||
const versionFallback = require('./versionFallback.js');
|
||||
const escapeStringRegexp = require('escape-string-regexp');
|
||||
const utils = require('./utils.js');
|
||||
|
||||
const SupportedHeaderFields = new Set([
|
||||
'id',
|
||||
|
@ -121,17 +121,10 @@ function extractMetadata(content) {
|
|||
return {metadata, rawContent: both.content};
|
||||
}
|
||||
|
||||
// process the metadata for a document found in the docs folder
|
||||
function processMetadata(file) {
|
||||
// process the metadata for a document found in either 'docs' or 'translated_docs'
|
||||
function processMetadata(file, refDir) {
|
||||
const result = extractMetadata(fs.readFileSync(file, 'utf8'));
|
||||
|
||||
let regexSubFolder = new RegExp(
|
||||
'/' + escapeStringRegexp(getDocsPath()) + '/(.*)/.*/'
|
||||
);
|
||||
|
||||
const match = regexSubFolder.exec(file);
|
||||
let language = match ? match[1] : 'en';
|
||||
|
||||
const language = utils.getLanguage(file, refDir) || 'en';
|
||||
const metadata = {};
|
||||
for (const fieldName of Object.keys(result.metadata)) {
|
||||
if (SupportedHeaderFields.has(fieldName)) {
|
||||
|
@ -142,7 +135,6 @@ function processMetadata(file) {
|
|||
}
|
||||
|
||||
const rawContent = result.rawContent;
|
||||
metadata.source = path.basename(file);
|
||||
|
||||
if (!metadata.id) {
|
||||
metadata.id = path.basename(file, path.extname(file));
|
||||
|
@ -150,6 +142,21 @@ function processMetadata(file) {
|
|||
if (metadata.id.includes('/')) {
|
||||
throw new Error('Document id cannot include "/".');
|
||||
}
|
||||
|
||||
// If a file is located in a subdirectory, prepend the subdir to it's ID
|
||||
// Example:
|
||||
// (file: 'docusaurus/docs/projectA/test.md', ID 'test', refDir: 'docs')
|
||||
// returns 'projectA/test'
|
||||
const subDir = utils.getSubDir(file, refDir);
|
||||
if (subDir) {
|
||||
metadata.id = `${subDir}/${metadata.id}`;
|
||||
}
|
||||
|
||||
// Example: `docs/projectA/test.md` source is `projectA/test.md`
|
||||
metadata.source = subDir
|
||||
? `${subDir}/${path.basename(file)}`
|
||||
: path.basename(file);
|
||||
|
||||
if (!metadata.title) {
|
||||
metadata.title = metadata.id;
|
||||
}
|
||||
|
@ -209,6 +216,7 @@ function generateMetadataDocs() {
|
|||
const defaultMetadatas = {};
|
||||
|
||||
// metadata for english files
|
||||
const docsDir = path.join(CWD, '../', getDocsPath());
|
||||
let files = glob.sync(CWD + '/../' + getDocsPath() + '/**');
|
||||
files.forEach(file => {
|
||||
let language = 'en';
|
||||
|
@ -216,7 +224,7 @@ function generateMetadataDocs() {
|
|||
const extension = path.extname(file);
|
||||
|
||||
if (extension === '.md' || extension === '.markdown') {
|
||||
const res = processMetadata(file);
|
||||
const res = processMetadata(file, docsDir);
|
||||
|
||||
if (!res) {
|
||||
return;
|
||||
|
@ -255,23 +263,17 @@ function generateMetadataDocs() {
|
|||
});
|
||||
|
||||
// metadata for non-english docs
|
||||
const regexSubFolder = /translated_docs\/(.*?)\/.*/;
|
||||
const translatedDir = path.join(CWD, 'translated_docs');
|
||||
files = glob.sync(CWD + '/translated_docs/**');
|
||||
files.forEach(file => {
|
||||
let language = 'en';
|
||||
const match = regexSubFolder.exec(file);
|
||||
if (match) {
|
||||
language = match[1];
|
||||
}
|
||||
|
||||
if (enabledLanguages.indexOf(language) === -1) {
|
||||
if (!utils.getLanguage(file, translatedDir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const extension = path.extname(file);
|
||||
|
||||
if (extension === '.md' || extension === '.markdown') {
|
||||
const res = processMetadata(file);
|
||||
const res = processMetadata(file, translatedDir);
|
||||
if (!res) {
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue