-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[gatsby-transformer-remark] Add pathPrefix to relative links
#3823
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
Changes from 3 commits
98ad1aa
a5afa53
fd70a9a
665a475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 }, | ||
|
|
@@ -55,6 +60,7 @@ module.exports = ( | |
| } | ||
|
|
||
| pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``) | ||
| pathPrefixCacheStr = pathPrefix || `` | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| // Setup Remark. | ||
|
|
@@ -107,6 +113,20 @@ 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.startsWith(pathPrefix) | ||
|
||
| ) { | ||
| node.url = withPathPrefix(node.url, pathPrefix) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // source => parse (can order parsing for dependencies) => typegen | ||
| // | ||
| // source plugins identify nodes, provide id, initial parse, know | ||
|
|
||
There was a problem hiding this comment.
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 —
pathPrefixis always defined. It just is set to an empty string if you're not building with--prefix-pathsgatsby/packages/gatsby/src/utils/api-runner-node.js
Line 61 in 41ff67d
So this if isn't necessary.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if
pathPrefixis defined and empty, this condition will skip unneeded AST traverseThere was a problem hiding this comment.
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