Skip to content
Merged
Changes from 3 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
30 changes: 25 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,20 @@ module.exports = (
}).then(() => {
const markdownAST = remark.parse(markdownNode.internal.content)

if (pathPrefix) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh one thing I just remembered — pathPrefix is always defined. It just is set to an empty string if you're not building with --prefix-paths

if (store.getState().program.prefixPaths) {

So this if isn't necessary.

Copy link
Contributor Author

@bodia-uz bodia-uz Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if pathPrefix is defined and empty, this condition will skip unneeded AST traverse

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right :-) Forgot empty strings are falsey not truthy — 🙄 @ JS

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

@nadiia nadiia Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about assuming any relative link in a markdown file should be appended with a pathPrefix? Otherwise, we can wrongfully skip adding the pathPrefix to the links starting with something similar to a pathPrefix value. For example, having a pathPrefix = "/web" and a relative link /website/details we'll get a link with no pathPrefix added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Mb its better to skip !node.url.startsWith(pathPrefix) condition.
@KyleAMathews what You think?

Copy link
Contributor Author

@bodia-uz bodia-uz Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gatsby-link does not check if link startsWith(pathPrefix), so looks like this code also should not.

https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-link/src/index.js#L20-L24

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bodia-uz yeah — don't see a reason to add that check. E.g. what if there was a path named /my-site/ but your prefix was also /my-site/. Then that path would mysteriously not be prefixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Updated.

) {
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