Skip to content

Commit

Permalink
[ci] Add build step to check docs for broken links
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknovitski committed Jan 18, 2019
1 parent 6ebf329 commit d8c4910
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ jobs:
- yarn:
command: export
working_directory: docs
- run:
working_directory: docs
command: nix run expo.python --command yarn run export-server
background: true
- run:
working_directory: docs
command: yarn run test-links:all http://localhost:8000
- deploy:
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
Expand Down
3 changes: 3 additions & 0 deletions bin/wget
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /usr/bin/env bash
# This file was generated by nix-binstubs
exec nix run -f "$(dirname "$0")/../nix" wget --command wget "$@"
2 changes: 1 addition & 1 deletion docs/components/DocumentationHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class DocumentationHeader extends React.PureComponent {
<header className={STYLES_NAV}>
<div className={STYLES_LEFT}>
<div className={STYLES_LOGO_CONTAINER}>
<Link prefetch href="/versions">
<Link prefetch href="/versions/">
<a className={STYLES_LOGO}>
<BrandLogo />
</a>
Expand Down
10 changes: 6 additions & 4 deletions docs/components/DocumentationPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ export default class DocumentationPage extends React.Component {

_handleSetVersion = version => {
this._version = version;
let newPath = '/versions/' + version;

if (version === 'latest') {
Router.push('/versions/' + LATEST_VERSION + '/', '/versions/' + version + '/');
} else {
Router.push('/versions/' + version + '/', '/versions/' + version + '/');
// TODO: Find what's stripping trailing slashes from these
if (version.startsWith('v')) {
newPath += '/'
}

Router.push(newPath + '/');
};

_handleShowMenu = () => {
Expand Down
5 changes: 5 additions & 0 deletions docs/components/DocumentationSidebarLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const STYLES_DEFAULT = css`
`;

export default class DocumentationSidebarLink extends React.Component {
componentDidMount() {
// Consistent link behavior across dev server and static export
global.__NEXT_DATA__.nextExport = true
}

isSelected() {
if (!this.props.url) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion docs/mdjs/generate-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const generateNavLinks = (path_, arr) => {
let items = fs.readdirSync(path_);

let processUrl = path => {
let newPath = path.replace(/^versions/, '/versions').replace(/.md$/, '/');
let newPath = path.replace(/^versions/, '/versions').replace(/.md$/, '');
return newPath;
};

Expand Down
10 changes: 6 additions & 4 deletions docs/mdjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ export default redirect('/versions/latest/');

console.timeEnd('Compiling *.md files to *.js');

// symlink versions/latest to versions/v(latest version)
// copy versions/v(latest version) to versions/latest
// (next only half-handles symlinks)
const LATEST_VERSION = 'v' + require('../package.json').version;
vLatest = path.join(DESTINATION_PATH_PREFIX, LATEST_VERSION);
latest = path.join(DESTINATION_PATH_PREFIX, "latest");
const vLatest = path.join(DESTINATION_PATH_PREFIX, LATEST_VERSION + "/");
const latest = path.join(DESTINATION_PATH_PREFIX, "latest/");
console.log(vLatest, latest);
fs.removeSync(latest)
fs.ensureSymlink(vLatest, latest)
fs.copySync(vLatest, latest)

// Watch for changes in directory
if (process.argv.length < 3) {
Expand Down
13 changes: 4 additions & 9 deletions docs/next.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const LATEST_VERSION = 'v' + require('./package.json').version;
const { join } = require('path');
const { copyFileSync } = require('fs-extra');

module.exports = {
async exportPathMap(defaultPathMap, {dev}) {
async exportPathMap(defaultPathMap, {dev, dir, outDir}) {
if (dev) {
return defaultPathMap
}
copyFileSync(join(dir, 'robots.txt'), join(outDir, 'robots.txt'))
return Object.assign(
...Object.entries(defaultPathMap).map(([pathname, page]) => {
if (pathname.match(/\/v[1-9][^\/]*$/)) {
Expand All @@ -14,13 +16,6 @@ module.exports = {

result = { [pathname]: page };

// For every path which doesn't end in ".html" (except "/"), create a matching copy with ".html" added
// (We have many internal links of this sort)
if (! pathname.match(/\.html$/) && pathname.match(/[a-z]$/)) {
result[pathname + ".html"] = page;
}


return result;
})
);
Expand Down
4 changes: 3 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"mdjs-prod": "node ./mdjs --no-watch",
"fix-markdown": "node ./transition/fix-markdown.js",
"clean": "rm -r docs pages/versions generated/navigation-data.json static/images/generated",
"import-react-native-docs": "node ./scripts/import-react-native-docs.js"
"import-react-native-docs": "node ./scripts/import-react-native-docs.js",
"test-links": "wget --page-requisites --spider --random-wait --recursive",
"test-links:all": "scripts/test-links-all-versions.sh"
},
"dependencies": {
"babel-plugin-module-resolver": "3.1.1",
Expand Down
Empty file added docs/robots.txt
Empty file.
14 changes: 14 additions & 0 deletions docs/scripts/import-react-native-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ let mainAsync = async () => {
'(https://facebook.github.io/react-native/docs/more-resources.html)'
);

l.replace(
/\[`Toolbar` widget\]\([^\)]+\)/,
'[`Toolbar` widget](https://developer.android.com/reference/android/support/v7/widget/Toolbar.html)'
);
l.replace(
/\[navigator\.geolocation\]\([^\)]+\)/g,
'[navigator.geolocation](../../sdk/location/)'
);

l.replace(
/\[CameraRoll\]([^\)]+\)/g,
'[CameraRoll](https://facebook.github.io/react-native/docs/cameraroll.html)'
);

// `](./foo` -> `](foo`
l = l.replace(
/\]\(\.\/([^\):]+)/g,
Expand Down
15 changes: 15 additions & 0 deletions docs/scripts/test-links-all-versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail

root=$1
docsDir="$(dirname "$0")/.."

pushd "$docsDir"

# TODO: add hidden links between version pages and delete this script

for v in versions/latest $(ls -d versions/v*); do
yarn run test-links "$root/$v"
done

popd
17 changes: 7 additions & 10 deletions docs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const app = next({ dev });
const port = 3000;
const handle = app.getRequestHandler();

const LATEST_VERSION = 'v' + require('./package.json').version;
const { WORKFLOW, DISTRIBUTION, EXPOKIT } = require('./transition/sections');
const CATEGORY_ALIASES = [
{ path: 'workflow', files: WORKFLOW },
Expand Down Expand Up @@ -50,26 +49,24 @@ app.prepare().then(() => {
}

// NOTE(jim): Mutations have to line up with FS paths provided by mdjs.
server.get('/versions/:version/:category/:post', (req, res) => {
server.get('/versions/:version/:category/:post/', (req, res) => {
const { query } = parse(req.url, true);
let { version, category, post } = req.params;

post = stripTrailingSlashAndExtensions(post);

if (version === 'latest') {
version = LATEST_VERSION;
}

category = mutateCategoryWithRedirectAlias(category, post);

const updatedPath = `/versions/${version}/${category}/${post}`;
req.originalPath = updatedPath;
app.render(req, res, updatedPath, query);
});

server.get('/', (req, res) => {
const { pathname, query } = parse(req.url, true);
app.render(req, res, pathname, query)
})

server.get('*', (req, res) => {
const { pathname, query } = parse(req.url, true);
app.render(req, res, pathname, query);
app.render(req, res, pathname.slice(0,-1), query);
});

server.listen(port, err => {
Expand Down
1 change: 1 addition & 0 deletions update-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bin/nix-binstubs \
git-lfs \
nodePackages.gulp-cli \
nodejs-10_x \
wget \
xcpretty \
yarn

Expand Down

0 comments on commit d8c4910

Please sign in to comment.