Skip to content

Commit

Permalink
fix: support path-prefix for reference links in Markdown files (#8607)
Browse files Browse the repository at this point in the history
* Support URL prefix for reference links in markdown files, fixes #8588

* Add test to gatsby-transformer-remark to ensure link prefixing works
  • Loading branch information
i-like-robots authored and pieh committed Oct 2, 2018
1 parent f556349 commit fb43fda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ Object {
}
`;

exports[`Links are correctly prefixed 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: 26 additions & 4 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
const extendNodeType = require(`../extend-node-type`)

// given a set of nodes and a query, return the result of the query
async function queryResult(nodes, fragment, { types = [] } = {}) {
async function queryResult(nodes, fragment, { types = [] } = {}, additionalParameters) {
const inferredFields = inferObjectStructureFromNodes({
nodes,
types: [...types],
Expand All @@ -24,6 +24,7 @@ async function queryResult(nodes, fragment, { types = [] } = {}) {
set: () => null,
},
getNodes: () => [],
...additionalParameters,
},
{
plugins: [],
Expand Down Expand Up @@ -89,7 +90,8 @@ const bootstrapTest = (label, content, query, test, additionalParameters = {}) =
query,
{
types: [{ name: `MarkdownRemark` }],
}
},
additionalParameters
).then(result => {
try {
test(result.data.listNode[0])
Expand Down Expand Up @@ -348,7 +350,27 @@ final text
frontmatter {
title
}`,
(node) => {
expect(node).toMatchSnapshot()
})
})

describe(`Links are correctly prefixed`, () => {
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">`)
},
{ pathPrefix: `/prefix` }
)
})
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module.exports = (

if (pathPrefix) {
// Ensure relative links include `pathPrefix`
visit(markdownAST, `link`, node => {
visit(markdownAST, [`link`, `definition`], node => {
if (
node.url &&
node.url.startsWith(`/`) &&
Expand Down

0 comments on commit fb43fda

Please # to comment.