Skip to content
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

Merged
merged 10 commits into from
Dec 19, 2017

Conversation

richardzcode
Copy link
Contributor

Motivation

This is to fix #305

The root cause of the problem is using 'en' as default language regardless of translation enabled or not.

However 'en' and no translation are two different cases. The fix has two major parts:

  1. Components. Basically use empty language property as an indication of no translation.
  2. Template generation. Basically handle the case of no translation.
    For readability I've created a new file env.js which encapsulate simple translation and versioning logic. So other places can just call env.translation.enabled to check.

I try to keep the changes minimal but sill has to modify quite a few files.

Test Plan

I've tested run server and generate locally.

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Dec 19, 2017
@JoelMarcey
Copy link
Contributor

@richardzcode Hi! Yep the build failed because prettier issues thus far.

Did you run npm run prettier on your pull request? Here is the changes that Prettier is recommending.

https://gist.github.com/JoelMarcey/215ea77e546496ab5758bcc86fe4152c

@richardzcode
Copy link
Contributor Author

My fault. I did run prettier but did not check what prettier changed... Let me try again.

@JoelMarcey
Copy link
Contributor

....and tests passed :)

@JoelMarcey JoelMarcey self-requested a review December 19, 2017 03:45
@richardzcode
Copy link
Contributor Author

😁

Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richardzcode I really like this pull request. I understand what you are trying to do here, and I think it makes sense.

I have some feedback/questions.

One key thing is that I think you need to rebase to the latest in master because some of the code I see in your PR is stuff that you are bringing back that has been removed in other PRs.

I want to think about this some more, and maybe let some of the other core team have a look as well. I may try to run this on a couple of our other sites too to make sure we are not breaking anyone horribly.

But I am impressed. <3 👍

@@ -81,8 +68,7 @@ function readSidebar() {

// split markdown header
function splitHeader(content) {
// New line characters need to handle all operating systems.
const lines = content.split(/\r?\n/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to keep the content.split(/\r?\n/); so we can continue to support, Windows, right? This was just added in #304 by @neilsutcliffe

? this.props.language + '/versions.html'
: 'versions.html');
return (
<div className="fixedHeaderContainer">
<div className="headerWrapper wrapper">
<header>
<a href={this.props.baseUrl}>
{siteConfig.headerIcon && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to remove the siteConfig.headerIcon check? It was just added in #312.

@@ -149,28 +132,19 @@ function processMetadata(file) {
metadata.title = metadata.id;
}

if (languages.length === 1 && !siteConfig.useEnglishUrl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, siteConfig.useEnglishUrl is a config option (that is not documented, unfortunately - but I will document it if we agree), that allows one to override the default of /docs/doc.html with something like /docs/en/doc.hmtl.

Are you thinking we shouldn't allow this override any longer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my mistake. should keep this override. should we use this override, or just have a languages.js with only 'en' enabled. It is a separate question.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal opinion is that we document and use the override to keep a clean separation between sites that have translations and sites that do not. If you have a language.js file, then translations will be enabled. If you want the /docs/en format, but no translations, you can use this override.

Seem reasonable?

@@ -194,6 +170,8 @@ function processMetadata(file) {

// process metadata for all docs and save into core/metadata.js
function generateMetadataDocs() {
console.log('Generating Metadata for Docs....');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may not be fully rebased to the latest master. I know this was removed on purpose because the logs were too noisy in #291

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@@ -345,6 +322,13 @@ function generateMetadataBlog() {
const metadatas = [];

let files = glob.sync(CWD + '/blog/**/*.*');
if (!files || files.length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. This was also removed in #291

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@@ -122,6 +120,8 @@ function execute(port) {

/****************************************************************************/

console.log('server.js triggered...');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here too: #291

const siteConfig = require(CWD + '/siteConfig.js');
const translate = require('./translate.js');
const versionFallback = require('./versionFallback.js');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming this is removed because it is unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this one is not used.

@@ -23,15 +24,12 @@ function execute(port) {
const glob = require('glob');
const chalk = require('chalk');
const translate = require('./translate.js');
const versionFallback = require('./versionFallback');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming this is removed because it is unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is unused.

@@ -523,6 +518,7 @@ function execute(port) {
});

app.listen(port);
console.log('listening on port: ' + port);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here too: #291

@JoelMarcey
Copy link
Contributor

@ericnakagawa @ericvicenti @hramos - maybe have a quick look at this too to make sure I am not missing anything key here. I like the PR.

@richardzcode
Copy link
Contributor Author

Thank you @JoelMarcey for the thorough review! Not sure what went wrong with my rebase. Now corrected. I'll be extra careful next time.

Copy link
Contributor

@hramos hramos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I am going to go with this. Thanks so much for this -- this definitely wasn't an easy task.

@JoelMarcey JoelMarcey merged commit 7dc6c6c into facebook:master Dec 19, 2017
JoelMarcey added a commit that referenced this pull request Dec 19, 2017
@JoelMarcey
Copy link
Contributor

I had to revert this actually -- I just noticed a link in docusaurus.io that was broken. Let me check out why.

@JoelMarcey
Copy link
Contributor

@richardzcode Hi. Sorry I had to revert this.

However, I cherry-picked your pull request locally and am running some tests on it. I am finding some things, which I will comment inline.

const join = path.join;

const languages_js = join(CWD, 'lanauages.js');
const translation_js = join(CWD, 'translation.js');
Copy link
Contributor

@JoelMarcey JoelMarcey Dec 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be const translation_js = './translation.js; since this file is in the same directory as env.js, not as part of the current process' working directory.


const join = path.join;

const languages_js = join(CWD, 'lanauages.js');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was spelled wrong :)

languages.js

}
const langPart = env.translation.enabled ? this.props.language + '-' : '';
const versionPart =
env.translation.enabled && this.props.version !== 'next'
Copy link
Contributor

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, not env.translation.enabled.

@@ -443,21 +425,25 @@ function execute() {
str
);
}

// write to base level
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking there is something funny going on in this section.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I am not 100% sure yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@richardzcode
Copy link
Contributor Author

Sorry about the mistakes. Let me run some more tests. Should I continue fix on this PR? Or it should be handled separately?

@JoelMarcey
Copy link
Contributor

No apologies necessary :) I really appreciate the work here. I think we are close.

Since I merged this one already, it's hard to bring back. Maybe a new PR with the fixes in it would be best. Is that ok?

@JoelMarcey
Copy link
Contributor

@richardzcode When I try to build docusaurus locally using npm run build within it's website directory, after those small fixes I let you know about, I get the following error:

/Users/joelm/dev/Docusaurus-primary/lib/core/nav/HeaderNav.js:175
            throw new Error("It looks like you've enabled language support, but haven't provided translated files. The document with id: '" + id + "' doesn't exist.");
            ^

Error: It looks like you've enabled language support, but haven't provided translated files. The document with id: '-installation' doesn't exist.

And that is because at some point this.props.language becomes undefined.

@richardzcode
Copy link
Contributor Author

sure. Working on it.

@richardzcode
Copy link
Contributor Author

richardzcode commented Dec 19, 2017

I think I know what's going on in HeaderNav.js.

This line:

const langPart = env.translation.enabled ? this.props.language + '-' : '';

Because we are generating default pages with language=''. When translation enabled, langPart would become '-doc1' etc. which does not exist in Metadata.

But I am confused why my local test doesn't have the problem...

@JoelMarcey
Copy link
Contributor

@richardzcode Are you testing on your own personal site? Or are you testing on the Docusaurus site itself?

@richardzcode
Copy link
Contributor Author

#322

@richardzcode
Copy link
Contributor Author

@JoelMarcey Testing on my own site. You are right. I should test on Docusaurus site.

JoelMarcey added a commit to JoelMarcey/Docusaurus that referenced this pull request Dec 27, 2017
JoelMarcey added a commit that referenced this pull request Dec 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken links in example index.js because of language
4 participants