From cf160faf96e631db13a1524a64eafeb9ce74eba7 Mon Sep 17 00:00:00 2001 From: Jonas Braathen Date: Sun, 27 Jan 2019 11:39:55 +0100 Subject: [PATCH] Extract relative permalinks from Wordpress posts for use as gatsby paths --- gatsby-node.js | 14 +++++++++++++- src/components/PostList.js | 8 +++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 2eb2ecd..9b22cb5 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -60,6 +60,9 @@ exports.createPages = ({ actions, graphql }) => { id slug status + fields { + link + } } } } @@ -86,7 +89,7 @@ exports.createPages = ({ actions, graphql }) => { _.each(posts, ({ node: post }) => { // Create the Gatsby page for this WordPress post createPage({ - path: `/${post.slug}/`, + path: post.fields.link, component: postTemplate, context: { id: post.id, @@ -219,4 +222,13 @@ exports.onCreateNode = ({ node, actions, getNode }) => { value, }) } + + if (node.internal.type === 'wordpress__POST') { + const link = new URL(node.link).pathname + createNodeField({ + node, + name: `link`, + value: link, + }) + } } diff --git a/src/components/PostList.js b/src/components/PostList.js index df44df2..c77b682 100644 --- a/src/components/PostList.js +++ b/src/components/PostList.js @@ -19,7 +19,7 @@ export default class IndexPage extends React.Component { key={post.id} >

- + {post.title} • @@ -36,7 +36,7 @@ export default class IndexPage extends React.Component { __html: post.excerpt.replace(/