-
Notifications
You must be signed in to change notification settings - Fork 394
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
Fix images in the RSS feed #1245
Changes from all commits
3ec0c30
1af8716
64bb732
20943ff
d2ba7eb
1e6bac4
7fd9e8b
a10217c
dbae74c
d9ecb1f
fd1dcf9
b1bb845
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 |
---|---|---|
@@ -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;' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. qq- does it makes/do we respect our custom image sizing logic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just changes the wrapper's internal image to have |
||
} | ||
} | ||
] | ||
}) | ||
return tree | ||
} | ||
|
||
function makeFeedHtml(htmlAst, siteUrl) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. really annoying that gastby plugin feed does not handle this ... kinda feels like all of this is a requirement for that plugin to be useful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree to a certain extent, especially for our use case, but RSS feeds are very often used for something like podcasts where the encoded content is more of a summary than the whole blog (ads are a lot harder in an RSS reader, after all), and the encoded content is kept simple. This isn't to say the default and suggested queries of |
||
// 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 |
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.
let's put a link here gatsbyjs/gatsby#14133 to remove this when it's fixed upstream?
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.
Sounds good to me!