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

Support URL prefix for reference links in Markdown files, fixes #8588 #8607

Merged
merged 2 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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