Skip to content

Commit

Permalink
refactor again & comment
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Jun 1, 2018
1 parent a2318ec commit e1f085f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
25 changes: 18 additions & 7 deletions lib/server/readMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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));
Expand All @@ -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;
}
Expand Down Expand Up @@ -433,6 +443,7 @@ function generateMetadataBlog() {

module.exports = {
getDocsPath,
getSubDir,
readSidebar,
extractMetadata,
processMetadata,
Expand Down
24 changes: 23 additions & 1 deletion lib/server/versionFallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
21 changes: 18 additions & 3 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e1f085f

Please sign in to comment.