Skip to content

Commit

Permalink
Dynamically append path alias (aaa/bbb.html = aaa/bbb = aaa/bbb/) int… (
Browse files Browse the repository at this point in the history
reactjs#783)

* Dynamically append path alias (aaa/bbb.html = aaa/bbb = aaa/bbb/) into redirects during gatsby onCreateNode callback API to avoid 404

* Consider more cases:
(1) markdown without redirects
(2) duplicated permalink for /docs/pure-render-mixin.html, rename the unused one

* Test permalink with ending `.html` rather then containing them
  • Loading branch information
whitedogg13 authored and sophiebits committed Jul 13, 2018
1 parent a1655f4 commit e52e6c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion content/docs/reference-pure-render-mixin.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: pure-render-mixin
title: PureRenderMixin
layout: docs
category: Reference
permalink: docs/pure-render-mixin.html
permalink: docs/pure-render-mixin-old.html
---

> Note
Expand Down
19 changes: 17 additions & 2 deletions gatsby/onCreateNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
// Parse date information out of blog post filename.
const BLOG_POST_FILENAME_REGEX = /([0-9]+)\-([0-9]+)\-([0-9]+)\-(.+)\.md$/;

function buildRedirectString(permalink, redirect_from) {
if (!permalink || !permalink.endsWith('.html')) {
return redirect_from ? JSON.stringify(redirect_from) : '';
}

let basePath = permalink.slice(0, -'.html'.length);
let redirects = [basePath, basePath + '/'];
if (Array.isArray(redirect_from)) {
redirects = redirects.concat(redirect_from);
}

return JSON.stringify(redirects);
}

// Add custom fields to MarkdownRemark nodes.
module.exports = exports.onCreateNode = ({node, boundActionCreators, getNode}) => {
const {createNodeField} = boundActionCreators;
Expand Down Expand Up @@ -67,8 +81,9 @@ module.exports = exports.onCreateNode = ({node, boundActionCreators, getNode}) =
createNodeField({
node,
name: 'redirect',
value: redirect_from ? JSON.stringify(redirect_from) : '',
value: buildRedirectString(permalink, redirect_from),
});

return;
}
};
};

0 comments on commit e52e6c6

Please sign in to comment.