Browse Source

Dynamically append path alias (aaa/bbb.html = aaa/bbb = aaa/bbb/) int… (#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
main
Richie 7 years ago
committed by Sophie Alpert
parent
commit
e52e6c6d5a
  1. 2
      content/docs/reference-pure-render-mixin.md
  2. 17
      gatsby/onCreateNode.js

2
content/docs/reference-pure-render-mixin.md

@ -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

17
gatsby/onCreateNode.js

@ -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;
@ -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;
}
};
Loading…
Cancel
Save