From cf6cadb6861c6ae7ed6892ea053517e9c6d83ef5 Mon Sep 17 00:00:00 2001 From: Carlos Company Date: Wed, 11 Jun 2025 15:39:20 +0200 Subject: [PATCH] fix: fixing rss by cleaning invalid characters (#492) * fix: fixing rss by cleaning invalid characters * style: format code for consistency in rss.xml.ts --------- Co-authored-by: L4Ph <4ranci0ne@gmail.com> --- src/pages/rss.xml.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index a3ae9c49..372ed2b3 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -7,6 +7,14 @@ import sanitizeHtml from "sanitize-html"; const parser = new MarkdownIt(); +function stripInvalidXmlChars(str: string): string { + return str.replace( + // biome-ignore lint/suspicious/noControlCharactersInRegex: https://www.w3.org/TR/xml/#charsets + /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFDD0-\uFDEF\uFFFE\uFFFF]/g, + "", + ); +} + export async function GET(context: APIContext) { const blog = await getSortedPosts(); @@ -17,13 +25,13 @@ export async function GET(context: APIContext) { items: blog.map((post) => { const content = typeof post.body === "string" ? post.body : String(post.body || ""); - + const cleanedContent = stripInvalidXmlChars(content); return { title: post.data.title, pubDate: post.data.published, description: post.data.description || "", link: `/posts/${post.slug}/`, - content: sanitizeHtml(parser.render(content), { + content: sanitizeHtml(parser.render(cleanedContent), { allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]), }), };