mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 16:47:26 +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
46
lib/server/utils.js
Normal file
46
lib/server/utils.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
const path = require('path');
|
||||
const escapeStringRegexp = require('escape-string-regexp');
|
||||
const env = require('./env.js');
|
||||
|
||||
// Return the subdirectory path from a reference directory
|
||||
// Example:
|
||||
// (file: 'docs/projectA/test.md', refDir: 'subDir')
|
||||
// returns 'projectA'
|
||||
function getSubDir(file, refDir) {
|
||||
let subDir = path.dirname(path.relative(refDir, file));
|
||||
subDir = subDir.replace('\\', '/');
|
||||
return subDir !== '.' ? subDir : null;
|
||||
}
|
||||
|
||||
// Get the corresponding enabled language locale of a file.
|
||||
// Example:
|
||||
// (file: '/website/translated_docs/ko/projectA/test.md', refDir: 'website/translated_docs')
|
||||
// returns 'ko'
|
||||
function getLanguage(file, refDir) {
|
||||
let regexSubFolder = new RegExp(
|
||||
'/' + escapeStringRegexp(path.basename(refDir)) + '/(.*)/.*/'
|
||||
);
|
||||
const match = regexSubFolder.exec(file);
|
||||
|
||||
// Avoid misinterpreting subdirectory as language
|
||||
if (match && env.translation.enabled) {
|
||||
const enabledLanguages = env.translation
|
||||
.enabledLanguages()
|
||||
.map(language => language.tag);
|
||||
if (enabledLanguages.indexOf(match[1]) !== -1) {
|
||||
return match[1];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSubDir,
|
||||
getLanguage,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue