-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make root-relative links absolute in feed. This runs the feed's htmlAst (from the remark transformer) through a processor that changes all root-relative links (/example) into absolute links (https://dvc.org/example), which is what many non-browser consumers of HTML expect. * Finish getting things working with both processors. This commit combines the root-relative rewriting with now-working image unwrapping to deliver working images to the RSS feed! The processor definition is also moved into the feed scope to take advantage of the feed query to get the root URL. * Move the feed plugin back to its original space. Since moving it out of gatsby-config won't be as simple as I thought, I decided to leave it out of the scope of this branch. * Extract feed processor from gatsby-config to its own file Also changed a bit around implementation: 1. Replaced `rehype-urls`, which depended on older rehype libs, with a local implementation of the same thing for root-to-absolute linking. 2. The new feed util leans on the existing rehype-to-html function, and now the new processor `.use`s less `unified` plugins as it only does AST processing. * Change unwrapped image styling in a way that respects original max-width * Revert package.json to its pre-branch form. * Use remark image wrapper class from its exported constants * Clean formatting of blog feed query * Change plugin definitions to all have "resolve" first.
- Loading branch information
1 parent
b8cd773
commit 2ea63aa
Showing
2 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { imageWrapperClass } = require('gatsby-remark-images/constants') | ||
const unified = require('unified') | ||
const { convertHastToHtml } = require('./convertHast.js') | ||
const { select, selectAll } = require('hast-util-select') | ||
|
||
const rootToAbsolute = siteUrl => tree => { | ||
selectAll('a', tree).forEach(node => { | ||
if (node.properties.href.startsWith('/')) { | ||
node.properties.href = siteUrl + node.properties.href | ||
} | ||
}) | ||
selectAll('img', tree).forEach(node => { | ||
if (node.properties.src.startsWith('/')) { | ||
node.properties.src = siteUrl + node.properties.src | ||
} | ||
}) | ||
return tree | ||
} | ||
|
||
const unwrapImages = () => tree => { | ||
selectAll(`.${imageWrapperClass}`, tree).forEach(node => { | ||
// Set the fallback image as the wrapper's only child, and then | ||
// give that image the wrapper's original style. | ||
const fallbackImg = select('img', node) | ||
node.children = [ | ||
{ | ||
...fallbackImg, | ||
properties: { | ||
...fallbackImg.properties, | ||
style: 'max-width: 100%; margin: auto;' | ||
} | ||
} | ||
] | ||
}) | ||
return tree | ||
} | ||
|
||
function makeFeedHtml(htmlAst, siteUrl) { | ||
// We add the rootToAbsolute processor before usage because it depends on siteUrl. | ||
return convertHastToHtml( | ||
unified() | ||
/* | ||
All images processed by Gatsby to be responsive are "unwrapped" into | ||
their fallback 'img' nodes, as RSS doesn't work with the tricks that | ||
true HTML does. | ||
*/ | ||
.use(unwrapImages) | ||
.use(rootToAbsolute, siteUrl) | ||
.runSync(htmlAst) | ||
) | ||
} | ||
|
||
module.exports = makeFeedHtml |