Skip to content

Commit c1c85b9

Browse files
gatsby-theme-blog path generation fixes (#16611)
1 parent e3e26f9 commit c1c85b9

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

themes/gatsby-theme-blog-core/gatsby-node.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const mkdirp = require(`mkdirp`)
44
const crypto = require(`crypto`)
55
const Debug = require(`debug`)
66
const { createFilePath } = require(`gatsby-source-filesystem`)
7+
const { urlResolve } = require(`gatsby-core-utils`)
78

89
const debug = Debug(`gatsby-theme-blog-core`)
910
const withDefaults = require(`./utils/default-options`)
@@ -97,7 +98,7 @@ exports.onCreateNode = async (
9798
themeOptions
9899
) => {
99100
const { createNode, createParentChildLink } = actions
100-
const { contentPath } = withDefaults(themeOptions)
101+
const { contentPath, basePath } = withDefaults(themeOptions)
101102

102103
// Make sure it's an MDX node
103104
if (node.internal.type !== `Mdx`) {
@@ -109,12 +110,25 @@ exports.onCreateNode = async (
109110
const source = fileNode.sourceInstanceName
110111

111112
if (node.internal.type === `Mdx` && source === contentPath) {
112-
const slug = createFilePath({
113-
node: fileNode,
114-
getNode,
115-
basePath: contentPath,
116-
})
117-
113+
let slug
114+
if (node.frontmatter.slug) {
115+
if (path.isAbsolute(node.frontmatter.slug)) {
116+
// absolute paths take precedence
117+
slug = node.frontmatter.slug
118+
} else {
119+
// otherwise a relative slug gets turned into a sub path
120+
slug = urlResolve(basePath, node.frontmatter.slug)
121+
}
122+
} else {
123+
// otherwise use the filepath function from gatsby-source-filesystem
124+
const filePath = createFilePath({
125+
node: fileNode,
126+
getNode,
127+
basePath: contentPath,
128+
})
129+
130+
slug = urlResolve(basePath, filePath)
131+
}
118132
const fieldData = {
119133
title: node.frontmatter.title,
120134
tags: node.frontmatter.tags || [],

0 commit comments

Comments
 (0)