Adding to Allowed Tags in Sanitize-Html
Part of the Astro series
Because I include full body text in my RSS feed, I use an html sanitizer called sanitize-html to sanitize, escape, and encode everything in the item body. One thing I didn’t realize until today is that by default, it strips out img tags. I knew that images were missing from my posts when viewed in RSS readers, but I thought this was due to me using relative URLs for them and not including the full URL including domain name. This may actually still matter, but it turns out my images never got that far, because sanitize-html was removing them whenever my RSS feed was rebuilt.
The good news is, there’s an easy way around this because sanitize-html provides an easy way to add tags to those allowed, and their documentation even includes the img tag example:
Here’s the entirety of my original Astro RSS template before I realized this:
Fixing it is as easy as modifying the content section like this:
In my case I run all this through yet another custom function called globalImageUrls, which just takes relative URLs from the post body and converts them to absolute urls including the domain:
That function is in a utility file called StringFormat.js and looks look this:
Anyway, if you use sanitize-html be aware that this is its list of allowed tags by default: