-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing Issue 305 - broken links because of language #316
Changes from all commits
b20e1af
dbb19a1
4e8cece
499aff0
1cdffe2
04a0e26
90818c5
4e48aa3
2a6ca41
8a6ca46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 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 CWD = process.cwd(); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
const join = path.join; | ||
|
||
const languages_js = join(CWD, 'lanauages.js'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was spelled wrong :)
|
||
const translation_js = join(CWD, 'translation.js'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be |
||
const versions_json = join(CWD, 'versions.json'); | ||
|
||
class Translation { | ||
constructor() { | ||
this.enabled = false; | ||
this.languages = [ | ||
{ | ||
enabled: true, | ||
name: 'English', | ||
tag: 'en', | ||
}, | ||
]; | ||
|
||
this._load(); | ||
} | ||
|
||
enabledLanguages() { | ||
return this.languages.filter(lang => lang.enabled); | ||
} | ||
|
||
_load() { | ||
if (fs.existsSync(languages_js)) { | ||
this.enabled = true; | ||
this.languages = require(language_js); | ||
this.translation = require(translation_js); | ||
} | ||
} | ||
} | ||
|
||
class Versioning { | ||
constructor() { | ||
this.enabled = false; | ||
this.latestVersion = null; | ||
this.versions = []; | ||
|
||
this._load(); | ||
} | ||
|
||
_load() { | ||
if (fs.existsSync(versions_json)) { | ||
this.enabled = true; | ||
this.versions = JSON.parse(fs.readFileSync(versions_json, 'utf8')); | ||
this.latestVersion = this.versions[0]; | ||
} | ||
} | ||
} | ||
|
||
const env = { | ||
translation: new Translation(), | ||
versioning: new Versioning(), | ||
}; | ||
|
||
module.exports = env; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,30 +20,15 @@ function execute() { | |
const glob = require('glob'); | ||
const chalk = require('chalk'); | ||
const Site = require('../core/Site.js'); | ||
const env = require('./env.js'); | ||
const siteConfig = require(CWD + '/siteConfig.js'); | ||
const translate = require('./translate.js'); | ||
const versionFallback = require('./versionFallback.js'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am assuming this is removed because it is unused. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes this one is not used. |
||
|
||
const feed = require('./feed.js'); | ||
const sitemap = require('./sitemap.js'); | ||
|
||
const join = path.join; | ||
|
||
const ENABLE_TRANSLATION = fs.existsSync(join(CWD, 'languages.js')); | ||
const ENABLE_VERSIONING = fs.existsSync(join(CWD, 'versions.json')); | ||
|
||
let languages; | ||
if (ENABLE_TRANSLATION) { | ||
languages = require(CWD + '/languages.js'); | ||
} else { | ||
languages = [ | ||
{ | ||
enabled: true, | ||
name: 'English', | ||
tag: 'en', | ||
}, | ||
]; | ||
} | ||
// create the folder path for a file if it does not exist, then write the file | ||
function writeFileAndCreateFolder(file, content) { | ||
mkdirp.sync(file.replace(new RegExp('/[^/]*$'), '')); | ||
|
@@ -87,10 +72,9 @@ function execute() { | |
console.log('generate.js triggered...'); | ||
|
||
// array of tags of enabled languages | ||
const enabledLanguages = []; | ||
languages.filter(lang => lang.enabled).map(lang => { | ||
enabledLanguages.push(lang.tag); | ||
}); | ||
const enabledLanguages = env.translation | ||
.enabledLanguages() | ||
.map(lang => lang.tag); | ||
|
||
readMetadata.generateMetadataDocs(); | ||
const Metadata = require('../core/metadata.js'); | ||
|
@@ -134,7 +118,7 @@ function execute() { | |
// determine what file to use according to its id | ||
let file; | ||
if (metadata.original_id) { | ||
if (ENABLE_TRANSLATION && metadata.language !== 'en') { | ||
if (env.translation.enabled && metadata.language !== 'en') { | ||
file = join(CWD, 'translated_docs', metadata.language, metadata.source); | ||
} else { | ||
file = join(CWD, 'versioned_docs', metadata.source); | ||
|
@@ -161,12 +145,7 @@ function execute() { | |
rawContent = insertTableOfContents(rawContent); | ||
} | ||
|
||
let latestVersion; | ||
if (ENABLE_VERSIONING) { | ||
latestVersion = JSON.parse( | ||
fs.readFileSync(join(CWD, 'versions.json'), 'utf8') | ||
)[0]; | ||
} | ||
let latestVersion = env.versioning.latestVersion; | ||
|
||
// replace any links to markdown files to their website html links | ||
Object.keys(mdToHtml).forEach(function(key, index) { | ||
|
@@ -207,7 +186,10 @@ function execute() { | |
writeFileAndCreateFolder(targetFile, str); | ||
|
||
// generate english page redirects when languages are enabled | ||
if (ENABLE_TRANSLATION && metadata.permalink.indexOf('docs/en') !== -1) { | ||
if ( | ||
env.translation.enabled && | ||
metadata.permalink.indexOf('docs/en') !== -1 | ||
) { | ||
const redirectComp = ( | ||
<Redirect | ||
metadata={metadata} | ||
|
@@ -443,21 +425,25 @@ function execute() { | |
str | ||
); | ||
} | ||
|
||
// write to base level | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am thinking there is something funny going on in this section. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I am not 100% sure yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a change I made in order to generate default pages from English pages. Original code was to copy English pages to one level above. However those pages has {language} property as 'en' which causes links wrong. So I made the change to generate instead. Couldn't think of a problem it would cause, but let me run some tests first. |
||
let language = ''; | ||
const str = renderToStaticMarkup( | ||
<Site language={language} config={siteConfig}> | ||
<ReactComp language={language} /> | ||
</Site> | ||
); | ||
writeFileAndCreateFolder(targetFile.replace('/en/', '/'), str); | ||
} else { | ||
// allow for rendering of other files not in pages/en folder | ||
let language = 'en'; | ||
for (let i = 0; i < langParts.length; i++) { | ||
if (enabledLanguages.indexOf(langParts[i]) !== -1) { | ||
language = langParts[i]; | ||
} | ||
} | ||
let language = ''; | ||
translate.setLanguage(language); | ||
const str = renderToStaticMarkup( | ||
<Site language={language} config={siteConfig}> | ||
<ReactComp language={language} /> | ||
</Site> | ||
); | ||
writeFileAndCreateFolder(targetFile, str); | ||
writeFileAndCreateFolder(targetFile.replace('/en/', '/'), str); | ||
} | ||
fs.removeSync(tempFile); | ||
} else if (!fs.lstatSync(file).isDirectory()) { | ||
|
@@ -469,15 +455,6 @@ function execute() { | |
} | ||
}); | ||
|
||
// copy html files in 'en' to base level as well | ||
files = glob.sync(join(buildDir, 'en', '**')); | ||
files.forEach(file => { | ||
let targetFile = file.replace(join(buildDir, 'en'), join(buildDir)); | ||
if (file.match(/\.html$/)) { | ||
fs.copySync(file, targetFile); | ||
} | ||
}); | ||
|
||
// Generate CNAME file if a custom domain is specified in siteConfig | ||
if (siteConfig.cname) { | ||
let targetFile = join(buildDir, 'CNAME'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
env.versioning.enabled
, notenv.translation.enabled
.