From e1f085fdbb1ffddb5cead08bb450d98f6c9f6b4d Mon Sep 17 00:00:00 2001 From: endiliey Date: Fri, 1 Jun 2018 18:57:32 +0800 Subject: [PATCH] refactor again & comment --- lib/server/readMetadata.js | 25 ++++++++++++++++++------- lib/server/versionFallback.js | 24 +++++++++++++++++++++++- lib/version.js | 21 ++++++++++++++++++--- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/lib/server/readMetadata.js b/lib/server/readMetadata.js index f6cd8aad8d81e..588768040cd96 100644 --- a/lib/server/readMetadata.js +++ b/lib/server/readMetadata.js @@ -121,6 +121,15 @@ function extractMetadata(content) { return {metadata, rawContent: both.content}; } +// Return the subdirectory path of a doc file +// Example: `docs/projectA/test.md` subDir is `projectA` +function getSubDir(file) { + const subDir = path.dirname( + path.relative(path.join(CWD, '../', getDocsPath()), file) + ); + return subDir !== '.' ? subDir : null; +} + // process the metadata for a document found in the docs folder function processMetadata(file) { const result = extractMetadata(fs.readFileSync(file, 'utf8')); @@ -153,7 +162,6 @@ function processMetadata(file) { } const rawContent = result.rawContent; - metadata.source = path.basename(file); if (!metadata.id) { metadata.id = path.basename(file, path.extname(file)); @@ -164,14 +172,16 @@ function processMetadata(file) { // If a doc file is located in a subdirectory, prepend the subdir to it's ID // Example: `docs/projectA/test.md` with 'test' ID is changed to `projectA/test` - const curDir = path.dirname( - path.relative(path.join(CWD, '../', getDocsPath()), file) - ); - if (curDir !== '.') { - metadata.id = curDir + '/' + metadata.id; - metadata.source = curDir + '/' + metadata.source; + const subDir = getSubDir(file); + 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; } @@ -433,6 +443,7 @@ function generateMetadataBlog() { module.exports = { getDocsPath, + getSubDir, readSidebar, extractMetadata, processMetadata, diff --git a/lib/server/versionFallback.js b/lib/server/versionFallback.js index 099fcb12388db..00d19bd8d5f52 100644 --- a/lib/server/versionFallback.js +++ b/lib/server/versionFallback.js @@ -190,11 +190,33 @@ function diffLatestDoc(file, id) { ); } +// Return the subdirectory path of a versioned_doc file +// Example: `versioned_docs/version-1.0.11/projectA/test.md` subDir is `projectA` +function getSubDir(file, version) { + const subDir = path.dirname( + path.relative(path.join(CWD, 'versioned_docs', `version-${version}`), file) + ); + return subDir !== '.' ? subDir : null; +} + // return metadata for a versioned file given the file, its version (requested), // the version of the file to be used, and its language function processVersionMetadata(file, version, useVersion, language) { const metadata = extractMetadata(fs.readFileSync(file, 'utf8')).metadata; - metadata.source = 'version-' + useVersion + '/' + path.basename(file); + + // If a versioned doc file is located in a subdirectory, do extra work + const subDir = getSubDir(file, useVersion); + if (subDir) { + metadata.original_id = subDir + '/' + metadata.original_id; + metadata.id = metadata.id.replace( + 'version-' + version + '-', + 'version-' + version + '-' + subDir + '/' + ); + metadata.source = + 'version-' + useVersion + '/' + subDir + '/' + path.basename(file); + } else { + metadata.source = 'version-' + useVersion + '/' + path.basename(file); + } const latestVersion = versions[0]; diff --git a/lib/version.js b/lib/version.js index 875db756bb502..90a8e48aa6b54 100755 --- a/lib/version.js +++ b/lib/version.js @@ -70,12 +70,18 @@ function makeHeader(metadata) { return header; } +function writeFileAndCreateFolder(file, content, encoding) { + mkdirp.sync(path.dirname(file)); + + fs.writeFileSync(file, content, encoding); +} + 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 + '/../' + readMetadata.getDocsPath() + '/*'); +let files = glob.sync(CWD + '/../' + readMetadata.getDocsPath() + '/**'); files.forEach(file => { const ext = path.extname(file); if (ext !== '.md' && ext !== '.markdown') { @@ -106,9 +112,18 @@ files.forEach(file => { metadata.original_id = metadata.id; metadata.id = 'version-' + version + '-' + metadata.id; - const targetFile = versionFolder + '/' + path.basename(file); + let filePath = path.basename(file); + const subDir = readMetadata.getSubDir(file); + if (subDir) { + filePath = subDir + '/' + filePath; + } + const targetFile = versionFolder + '/' + filePath; - fs.writeFileSync(targetFile, makeHeader(metadata) + rawContent, 'utf8'); + writeFileAndCreateFolder( + targetFile, + makeHeader(metadata) + rawContent, + 'utf8' + ); }); // copy sidebar if necessary