Skip to content

Commit

Permalink
[gatsby-transformer-remark] Add pathPrefix to relative links (#3823)
Browse files Browse the repository at this point in the history
* [gatsby-transformer-remark] Add `pathPrefix` to relative links
Ensure markdown relative links include `pathPrefix`.
Fixes #3316

* [gatsby-transformer-remark] add minor comment to `withPathPrefix`

* [gatsby-transformer-remark] add `pathPrefix` to `cacheKey` functions

* [gatsby-transformer-remark] remove `node.url.startsWith(pathPrefix)` condition for `withPathPrefix` logic
  • Loading branch information
bodia-uz authored and KyleAMathews committed Feb 7, 2018
1 parent 4268f48 commit ee5aa89
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,31 @@ const stripPosition = require(`unist-util-remove-position`)
const hastReparseRaw = require(`hast-util-raw`)

let pluginsCacheStr = ``
let pathPrefixCacheStr = ``
const astCacheKey = node =>
`transformer-remark-markdown-ast-${
node.internal.contentDigest
}-${pluginsCacheStr}`
}-${pluginsCacheStr}-${pathPrefixCacheStr}`
const htmlCacheKey = node =>
`transformer-remark-markdown-html-${
node.internal.contentDigest
}-${pluginsCacheStr}`
}-${pluginsCacheStr}-${pathPrefixCacheStr}`
const htmlAstCacheKey = node =>
`transformer-remark-markdown-html-ast-${
node.internal.contentDigest
}-${pluginsCacheStr}`
}-${pluginsCacheStr}-${pathPrefixCacheStr}`
const headingsCacheKey = node =>
`transformer-remark-markdown-headings-${
node.internal.contentDigest
}-${pluginsCacheStr}`
}-${pluginsCacheStr}-${pathPrefixCacheStr}`
const tableOfContentsCacheKey = node =>
`transformer-remark-markdown-toc-${
node.internal.contentDigest
}-${pluginsCacheStr}`
}-${pluginsCacheStr}-${pathPrefixCacheStr}`

// ensure only one `/` in new url
const withPathPrefix = (url, pathPrefix) =>
(pathPrefix + url).replace(/\/\//, `/`)

module.exports = (
{ type, store, pathPrefix, getNode, cache },
Expand All @@ -55,6 +60,7 @@ module.exports = (
}

pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``)
pathPrefixCacheStr = pathPrefix || ``

return new Promise((resolve, reject) => {
// Setup Remark.
Expand Down Expand Up @@ -107,6 +113,19 @@ module.exports = (
}).then(() => {
const markdownAST = remark.parse(markdownNode.internal.content)

if (pathPrefix) {
// Ensure relative links include `pathPrefix`
visit(markdownAST, `link`, node => {
if (
node.url &&
node.url.startsWith(`/`) &&
!node.url.startsWith(`//`)
) {
node.url = withPathPrefix(node.url, pathPrefix)
}
})
}

// source => parse (can order parsing for dependencies) => typegen
//
// source plugins identify nodes, provide id, initial parse, know
Expand Down

0 comments on commit ee5aa89

Please sign in to comment.