This might be a bit unusual case, but we migrated a lot of content from Wordpress (html) to Contentful (markdown). All relative urls in markdown look like /:locale/blog/.... Because of some other issues, we had to switch from building all languages at once, into 6 separate builds and we set pathPrefix for each build (/en/blog, /de/blog etc).
Now, because gatsby-transformer-remark adds this prefix, all our relative urls look like /:locale/blog/:locale/blog/.... I don't want to update urls in our content, because we got almost 4000 pages and also because we hope to switch back to 1 build without pathPrefix after Gatsby v2 is released.
Thus it would be great, if there was an option to tell gatsby-transformer-remark to skip adding pathPrefix to relative urls. I could probably write a plugin that removes it from all relative links, but maybe such option would be useful for others as well.
I'll switch to 1.7.30 for now (I don't really want to maintain a fork of gatsby-transformer-remark), but if anyone thinks it's a good idea, I can prepare a PR.
{
resolve: 'gatsby-transformer-remark',
options: {
usePathPrefix: false, // defaults to true
plugins: [],
},
},
This might be a bit unusual case
hah, nice link :)
This sounds like a perfect feature to implement as a plugin, particularly as it's a workaround for some Gatsby v1 issues. If it's something that people find very useful, that would be the time to consider merging it into gatsby-transformer-remark.
I'll switch to
1.7.30
What's 1.7.30?
In version 1.7.31 of gatsby-transformer-remark a new feature was added to gatsby-transformer-remark that adds pathPrefix to all relative urls in markdown - https://github.com/gatsbyjs/gatsby/commit/ee5aa897d60e024a25108b69b4fca943768d3063#diff-488d99b036db541ee4a25a6991027b5dR116, so 1.7.30 is the last version without this feature ;)
I'm not sure it makes sense to add a plugin that removes pathPrefix from relative urls after they are added by gatsby-transformer-remark, it would make more sense if it was an option - addPathPrefixToRelativeUrls: true/false or sth like this.
As @m-allanson this would be good candidate for general plugin that could replace links - either with string search and replace or more flexible "transform" function. I don't particularly like the idea of adding usePathPrefix config if this could be done in plugin.
Turns out it wasn't that hard:
const visit = require('unist-util-visit');
module.exports = ({ markdownAST, pathPrefix }) => {
visit(markdownAST, 'link', (node) => {
if (node.url.startsWith(pathPrefix)) {
node.url = node.url.replace(pathPrefix, '');
}
});
return markdownAST;
};
It's not generic at all and probably doesn't handle references (which we don't have), but works for my case.