Skip to content

Commit

Permalink
Teach website sidebar to prefer en-US
Browse files Browse the repository at this point in the history
Summary:
When I reran the website build toolchain for the 0.9.1 release I ended up updating the `metadata.js` file:

88c41e9

This in turn meant that we had multiple documents with the same id; eg:

- thinking-in-graphql

But different permalinks:

- docs/thinking-in-graphql.zh-CN.html
- docs/thinking-in-graphql.html

When I committed and pushed the generated documentation, ie:

eef148d

I saw that our sidebar table of contents was now using the localized version of the document; ie:

> 深入理解 GraphQL

instead of:

> Thinking in GraphQL

The solution for now is to always prefer non-localized resources in the sidebar, but in the long term we'll want the localized version of each page to prefer localized links in the sidebar, if they exist. This may be tricky to implement, however, because we don't control the gh-pages environment and so can't necessarily implement the logical `Accept-Language` solution (although perhaps GitHub will... maybe they already do?). For now just getting the fix out. The updated, published documentation went out in:

52b908d

Aside: this commit takes care to preserve ordering, so that the "find first element which doesn't have any previous" logic can rely on it. But this is not well-defined behavior because (I think) the order in which we're visiting the source files (as returned by `glob.sync(MD_DIR + '**/*.*')` in `website/server/convert.js`) is actually filesystem-dependent. On HFS+ (macOS) this happens to be lexicographically ordered, but I know of other filesystems where it is dependent on things like inode metadata layout and such.

Closes #1238

Pulled By: wincent

fbshipit-source-id: dd46ba2ba2b9972afd8a720a31407c8d3f29343c
  • Loading branch information
wincent authored and Facebook Github Bot 3 committed Jun 24, 2016
1 parent 912433e commit 865194a
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions website/core/DocsSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,47 @@
'use strict';

const Metadata = require('Metadata');
const React = require('React');

/**
* We're not really set up to support translations well at the moment (ie.
* multiple documents with the same `id` but different `permalink`s of the
* form "foo.html" and "foo.zh-CN.html"), so we make sure we always prefer
* the "canonical" (non-localized) version.
*/
function shouldOverwritePreviousWithCanonical(previous, maybeCanonical) {
let match = previous.permalink.match(/^(.+)\.[a-z]+-[a-z]+\.html$/i);
if (match) {
// `previous` is a localized file.
const previousBase = match[1];
match = maybeCanonical.permalink.match(/^(.+)\.html/);
if (match && match[1] === previousBase) {
// Found canonical "foo.html"; should overwrite "foo.zh-CN.html".
return true;
}
}
return false;
}

const DocsSidebar = React.createClass({
getCategories: function() {
const metadatas = Metadata.files.filter(function(metadata) {
return metadata.layout === 'docs';
});
// Skip over non-docs and non-en_US entries.
const metadatas = Array.from(
Metadata.files.reduce(function(acc, metadata) {
if (metadata.layout === 'docs') {
const previous = acc.get(metadata.id);
if (
!previous ||
shouldOverwritePreviousWithCanonical(previous, metadata)
) {
acc.delete(metadata.id);
acc.set(metadata.id, metadata);
}
}
return acc;
}, new Map()
).values()
);

// Build a hashmap of article_id -> metadata
const articles = {};
Expand Down Expand Up @@ -58,8 +93,8 @@ const DocsSidebar = React.createClass({
currentCategory && categories.push(currentCategory);
currentCategory = {
name: metadata.category,
links: []
}
links: [],
};
}
currentCategory.links.push(metadata);
metadata = articles[metadata.next];
Expand Down Expand Up @@ -97,7 +132,7 @@ const DocsSidebar = React.createClass({
</div>
)}
</div>;
}
},
});

module.exports = DocsSidebar;

0 comments on commit 865194a

Please sign in to comment.