fix: error rss

This commit is contained in:
KazooTTT
2024-11-26 13:08:42 +08:00
parent f943d4bcdc
commit 55dcdd7f1b

View File

@ -1,37 +1,49 @@
import { siteConfig } from '@/site-config'; import { siteConfig } from '@/site-config'
import rss from '@astrojs/rss'; import rss from '@astrojs/rss'
import type { APIContext } from 'astro'; import type { APIContext } from 'astro'
import type { CollectionEntry } from 'astro:content'; import sanitizeHtml from 'sanitize-html'
import sanitizeHtml from 'sanitize-html';
const sanitizeText = (text: string | undefined) => text?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') || ''; // Define an interface for your markdown files
interface MarkdownPost {
frontmatter: {
title?: string
date: string
description?: string
category?: string
slug: string
author?: string
}
compiledContent?: () => string
}
export async function GET(context: APIContext) { export async function GET(context: APIContext) {
const postImportResult = import.meta.glob<CollectionEntry<'post'>>('../content/post/**/*.md', { eager: true }); const postImportResult = import.meta.glob('../content/post/**/*.md', { eager: true })
const posts = Object.values(postImportResult); const posts = Object.values(postImportResult) as MarkdownPost[]
const sortedPosts = posts.sort((a, b) => new Date(b.data.date).valueOf() - new Date(a.data.date).valueOf()); const sortedPosts = posts.sort(
(a, b) => new Date(b.frontmatter.date).valueOf() - new Date(a.frontmatter.date).valueOf()
return rss({ )
title: siteConfig.title,
description: siteConfig.description, return rss({
site: context.site!, title: siteConfig.title,
items: sortedPosts.map((post) => { description: siteConfig.description,
const prefix = post.data.category?.startsWith("日记-20") ? '/dairy/' : '/blog/'; site: context.site!,
return { items: sortedPosts.map((post) => {
title: sanitizeText(post.data.title), const prefix = post.frontmatter.category?.startsWith('日记-20') ? '/dairy/' : '/blog/'
pubDate: new Date(post.data.date), return {
description: sanitizeText(post.data.description), title: post.frontmatter.title?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') ?? '',
link: `${prefix}${post.slug}`, pubDate: new Date(post.frontmatter.date),
content: sanitizeHtml(post.body, { description: post.frontmatter.description?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') ?? '',
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']), link: `${prefix}${post.frontmatter.slug}`,
allowedAttributes: { content: sanitizeHtml(post.compiledContent?.() ?? '', {
...sanitizeHtml.defaults.allowedAttributes, allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
img: ['src', 'alt'] allowedAttributes: {
}, ...sanitizeHtml.defaults.allowedAttributes,
textFilter: sanitizeText img: ['src', 'alt']
}), },
author: sanitizeText(post.data.author) textFilter: (text) => text.replace(/[\x00-\x1F\x7F-\x9F]/g, '')
}; }),
}), author: post.frontmatter.author?.replace(/[\x00-\x1F\x7F-\x9F]/g, '')
}); }
})
})
} }