mirror of
https://github.com/KazooTTT/kazoottt-blog.git
synced 2025-06-16 23:41:21 +08:00
fix: error rss
This commit is contained in:
@ -1,37 +1,49 @@
|
||||
import { siteConfig } from '@/site-config';
|
||||
import rss from '@astrojs/rss';
|
||||
import type { APIContext } from 'astro';
|
||||
import type { CollectionEntry } from 'astro:content';
|
||||
import sanitizeHtml from 'sanitize-html';
|
||||
import { siteConfig } from '@/site-config'
|
||||
import rss from '@astrojs/rss'
|
||||
import type { APIContext } from 'astro'
|
||||
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) {
|
||||
const postImportResult = import.meta.glob<CollectionEntry<'post'>>('../content/post/**/*.md', { eager: true });
|
||||
const posts = Object.values(postImportResult);
|
||||
const sortedPosts = posts.sort((a, b) => new Date(b.data.date).valueOf() - new Date(a.data.date).valueOf());
|
||||
|
||||
return rss({
|
||||
title: siteConfig.title,
|
||||
description: siteConfig.description,
|
||||
site: context.site!,
|
||||
items: sortedPosts.map((post) => {
|
||||
const prefix = post.data.category?.startsWith("日记-20") ? '/dairy/' : '/blog/';
|
||||
return {
|
||||
title: sanitizeText(post.data.title),
|
||||
pubDate: new Date(post.data.date),
|
||||
description: sanitizeText(post.data.description),
|
||||
link: `${prefix}${post.slug}`,
|
||||
content: sanitizeHtml(post.body, {
|
||||
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
|
||||
allowedAttributes: {
|
||||
...sanitizeHtml.defaults.allowedAttributes,
|
||||
img: ['src', 'alt']
|
||||
},
|
||||
textFilter: sanitizeText
|
||||
}),
|
||||
author: sanitizeText(post.data.author)
|
||||
};
|
||||
}),
|
||||
});
|
||||
const postImportResult = import.meta.glob('../content/post/**/*.md', { eager: true })
|
||||
const posts = Object.values(postImportResult) as MarkdownPost[]
|
||||
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,
|
||||
site: context.site!,
|
||||
items: sortedPosts.map((post) => {
|
||||
const prefix = post.frontmatter.category?.startsWith('日记-20') ? '/dairy/' : '/blog/'
|
||||
return {
|
||||
title: post.frontmatter.title?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') ?? '',
|
||||
pubDate: new Date(post.frontmatter.date),
|
||||
description: post.frontmatter.description?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') ?? '',
|
||||
link: `${prefix}${post.frontmatter.slug}`,
|
||||
content: sanitizeHtml(post.compiledContent?.() ?? '', {
|
||||
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
|
||||
allowedAttributes: {
|
||||
...sanitizeHtml.defaults.allowedAttributes,
|
||||
img: ['src', 'alt']
|
||||
},
|
||||
textFilter: (text) => text.replace(/[\x00-\x1F\x7F-\x9F]/g, '')
|
||||
}),
|
||||
author: post.frontmatter.author?.replace(/[\x00-\x1F\x7F-\x9F]/g, '')
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user