Skip to content

Commit

Permalink
address code review & fix typo from prevs refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Jun 5, 2018
1 parent c519ea5 commit fcd0180
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
29 changes: 17 additions & 12 deletions lib/core/DocsLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ const path = require('path');

// component used to generate whole webpage for docs, including sidebar/header/footer
class DocsLayout extends React.Component {
getRelativeURL = (from, to) => {
return (
path
.relative(from, to)
.replace('\\', '/')
.replace(/^\.\.\//, '') + '.html'
);
};

render() {
const metadata = this.props.metadata;
const content = this.props.children;
Expand Down Expand Up @@ -55,12 +64,10 @@ class DocsLayout extends React.Component {
{metadata.previous_id && (
<a
className="docs-prev button"
href={
path
.relative(metadata.localized_id, metadata.previous_id)
.replace('\\', '/')
.replace(/^\.\.\//, '') + '.html'
}>
href={this.getRelativeURL(
metadata.localized_id,
metadata.previous_id
)}>
{' '}
{i18n
? translation[this.props.metadata.language][
Expand All @@ -76,12 +83,10 @@ class DocsLayout extends React.Component {
{metadata.next_id && (
<a
className="docs-next button"
href={
path
.relative(metadata.localized_id, metadata.next_id)
.replace('\\', '/')
.replace(/^\.\.\//, '') + '.html'
}>
href={this.getRelativeURL(
metadata.localized_id,
metadata.next_id
)}>
{i18n
? translation[this.props.metadata.language][
'localized-strings'
Expand Down
7 changes: 4 additions & 3 deletions lib/server/readMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ function processMetadata(file, refDir) {
// returns 'projectA/test'
const subDir = utils.getSubDir(file, refDir);
if (subDir) {
metadata.id = subDir + '/' + metadata.id;
metadata.id = `${subDir}/${metadata.id}`;
}

// Example: `docs/projectA/test.md` source is `projectA/test.md`
metadata.source = subDir
? subDir + '/' + path.basename(file)
? `${subDir}/${path.basename(file)}`
: path.basename(file);

if (!metadata.title) {
Expand Down Expand Up @@ -216,14 +216,15 @@ 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';

const extension = path.extname(file);

if (extension === '.md' || extension === '.markdown') {
const res = processMetadata(file, path.join(CWD, '../', getDocsPath()));
const res = processMetadata(file, docsDir);

if (!res) {
return;
Expand Down
14 changes: 6 additions & 8 deletions lib/server/versionFallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,16 @@ function processVersionMetadata(file, version, useVersion, language) {
path.join(CWD, 'versioned_docs', `version-${useVersion}`)
);
if (subDir) {
metadata.original_id = subDir + '/' + metadata.original_id;
metadata.original_id = `${subDir}/${metadata.original_id}`;
metadata.id = metadata.id.replace(
'version-' + useVersion + '-',
'version-' + useVersion + '-' + subDir + '/'
`version-${useVersion}-`,
`version-${useVersion}-${subDir}/`
);
}

metadata.source =
'version-' +
useVersion +
'/' +
(subDir ? subDir + '/' + path.basename(file) : path.basename(file));
metadata.source = subDir
? `version-${useVersion}/${subDir}/${path.basename(file)}`
: `version-${useVersion}/${path.basename(file)}`;

const latestVersion = versions[0];

Expand Down
12 changes: 6 additions & 6 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const path = require('path');
const mkdirp = require('mkdirp');
const chalk = require('chalk');
const readMetadata = require('./server/readMetadata.js');
const utils = require('./server/utils.js');
const versionFallback = require('./server/versionFallback.js');
const env = require('./server/env.js');

Expand Down Expand Up @@ -108,12 +109,11 @@ files.forEach(file => {
metadata.original_id = metadata.id;
metadata.id = 'version-' + version + '-' + metadata.id;

let filePath = path.basename(file);
const subDir = readMetadata.getSubDir(file);
if (subDir) {
filePath = subDir + '/' + filePath;
}
const targetFile = versionFolder + '/' + filePath;
const docsDir = path.join(CWD, '../', readMetadata.getDocsPath());
const subDir = utils.getSubDir(file, docsDir);
const targetFile = subDir
? `${versionFolder}/${subDir}/${path.basename(file)}`
: `${versionFolder}/${path.basename(file)}`;

writeFileAndCreateFolder(
targetFile,
Expand Down
2 changes: 1 addition & 1 deletion lib/write-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ function execute() {
};

// look through markdown headers of docs for titles and categories to translate
const docsDir = path.join(CWD, '../', readMetadata.getDocsPath());
let files = glob.sync(CWD + '/../' + readMetadata.getDocsPath() + '/**');
files.forEach(file => {
const extension = path.extname(file);
if (extension === '.md' || extension === '.markdown') {
let res;
try {
const docsDir = path.join(CWD, '../', readMetadata.getDocsPath());
res = readMetadata.processMetadata(file, docsDir);
} catch (e) {
console.error(e);
Expand Down

0 comments on commit fcd0180

Please sign in to comment.