Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-transformer-remark): Fix transformer-remark when using assetPrefix #15518

Merged
merged 2 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,13 @@ Object {
}
`;

exports[`Links are correctly prefixed when assetPrefix is used correctly prefixes links 1`] = `
Object {
"html": "<p>This is <a href=\\"/prefix/path/to/page1\\">a link</a>.</p>
<p>This is <a href=\\"/prefix/path/to/page2\\">a reference</a></p>",
}
`;

exports[`Table of contents is generated correctly from schema correctly generates table of contents 1`] = `
Object {
"frontmatter": Object {
Expand Down
30 changes: 29 additions & 1 deletion packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,34 @@ final text`,
})

describe(`Links are correctly prefixed`, () => {
const assetPrefix = ``
const basePath = `/prefix`
const pathPrefix = assetPrefix + basePath

bootstrapTest(
`correctly prefixes links`,
`
This is [a link](/path/to/page1).

This is [a reference]

[a reference]: /path/to/page2
`,
`html`,
node => {
expect(node).toMatchSnapshot()
expect(node.html).toMatch(`<a href="/prefix/path/to/page1">`)
expect(node.html).toMatch(`<a href="/prefix/path/to/page2">`)
},
{ additionalParameters: { pathPrefix: pathPrefix, basePath: basePath } }
)
})

describe(`Links are correctly prefixed when assetPrefix is used`, () => {
const assetPrefix = `https://example.com/assets`
const basePath = `/prefix`
const pathPrefix = assetPrefix + basePath

bootstrapTest(
`correctly prefixes links`,
`
Expand All @@ -936,7 +964,7 @@ This is [a reference]
expect(node.html).toMatch(`<a href="/prefix/path/to/page1">`)
expect(node.html).toMatch(`<a href="/prefix/path/to/page2">`)
},
{ additionalParameters: { pathPrefix: `/prefix` } }
{ additionalParameters: { pathPrefix: pathPrefix, basePath: basePath } }
)
})

Expand Down
11 changes: 6 additions & 5 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const SpaceMarkdownNodeTypesSet = new Set([
module.exports = (
{
type,
basePath,
pathPrefix,
getNode,
getNodesByType,
Expand All @@ -108,7 +109,7 @@ module.exports = (
return {}
}
pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``)
pathPrefixCacheStr = pathPrefix || ``
pathPrefixCacheStr = basePath || ``

const getCache = safeGetCache({ cache, getCache: possibleGetCache })

Expand Down Expand Up @@ -203,15 +204,15 @@ module.exports = (
})
const markdownAST = remark.parse(markdownNode.internal.content)

if (pathPrefix) {
if (basePath) {
// Ensure relative links include `pathPrefix`
visit(markdownAST, [`link`, `definition`], node => {
if (
node.url &&
node.url.startsWith(`/`) &&
!node.url.startsWith(`//`)
) {
node.url = withPathPrefix(node.url, pathPrefix)
node.url = withPathPrefix(node.url, basePath)
}
})
}
Expand Down Expand Up @@ -259,7 +260,7 @@ module.exports = (
markdownNode,
getNode,
files: fileNodes,
pathPrefix,
basePath,
reporter,
cache: getCache(plugin.name),
getCache,
Expand Down Expand Up @@ -322,7 +323,7 @@ module.exports = (
return null
}
node.url = [
pathPrefix,
basePath,
_.get(markdownNode, appliedTocOptions.pathToSlugField),
node.url,
]
Expand Down