mirror of
https://github.com/KazooTTT/kazoottt-blog.git
synced 2025-06-22 10:11:30 +08:00
fix: show full content in rss
This commit is contained in:
@ -1,33 +1,37 @@
|
||||
import { siteConfig } from '@/site-config'
|
||||
import { getAllSortedPosts } from '@/utils'
|
||||
import rss from '@astrojs/rss'
|
||||
import type { APIContext } from 'astro'
|
||||
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';
|
||||
|
||||
const sanitizeText = (text: string | undefined) => text?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') || '';
|
||||
|
||||
export async function GET(context: APIContext) {
|
||||
const blog = await getAllSortedPosts()
|
||||
return rss({
|
||||
// `<title>` field in output xml
|
||||
title: siteConfig.title,
|
||||
// `<description>` field in output xml
|
||||
description: siteConfig.description,
|
||||
// Pull in your project "site" from the endpoint context
|
||||
// https://docs.astro.build/en/reference/api-reference/#contextsite
|
||||
site: context.site!,
|
||||
// Array of `<item>`s in output xml
|
||||
// See "Generating items" section for examples using content collections and glob imports
|
||||
items: blog.map((post) => ({
|
||||
title: post.data.title,
|
||||
pubDate: post.data.date,
|
||||
description: post.data.description ?? '',
|
||||
// Compute RSS link from post `slug`
|
||||
// This example assumes all posts are rendered as `/blog/[slug]` routes
|
||||
link: `/blog/${post.slug}/`
|
||||
})),
|
||||
// Add custom XML elements
|
||||
customData: `
|
||||
<follow_challenge>
|
||||
<feedId>83074007039123456</feedId>
|
||||
<userId>62156866798228480</userId>
|
||||
</follow_challenge>`
|
||||
})
|
||||
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)
|
||||
};
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { SiteConfig } from '@/types'
|
||||
import type { AstroExpressiveCodeOptions } from 'astro-expressive-code'
|
||||
import type { SiteConfig } from '@/types';
|
||||
import type { AstroExpressiveCodeOptions } from 'astro-expressive-code';
|
||||
|
||||
export const siteConfig: SiteConfig = {
|
||||
// Used as both a meta property (src/components/BaseHead.astro L:31 + L:49) & the generated satori png (src/pages/og-image/[slug].png.ts)
|
||||
|
@ -1,17 +1,13 @@
|
||||
export { cn } from './tailwind'
|
||||
export {
|
||||
getAllPosts,
|
||||
getAllSortedPosts,
|
||||
sortMDByDate,
|
||||
getUniqueTags,
|
||||
getUniqueTagsWithCount,
|
||||
getAllCategories,
|
||||
getUniqueCategories,
|
||||
getUniqueCategoriesWithCount,
|
||||
getallDiaries,
|
||||
getallDiariesSorted
|
||||
} from './post'
|
||||
export { getFormattedDate } from './date'
|
||||
export { elementHasClass, rootInDarkMode, toggleClass } from './domElement'
|
||||
export { generateToc } from './generateToc'
|
||||
export type { TocItem } from './generateToc'
|
||||
export { elementHasClass, toggleClass, rootInDarkMode } from './domElement'
|
||||
export {
|
||||
getAllCategories, getAllPosts,
|
||||
getAllSortedPosts, getUniqueCategories,
|
||||
getUniqueCategoriesWithCount, getUniqueTags,
|
||||
getUniqueTagsWithCount, getAllDiaries as getallDiaries,
|
||||
getAllDiariesSorted as getallDiariesSorted, sortMDByDate
|
||||
} from './post'
|
||||
export { cn } from './tailwind'
|
||||
|
||||
|
@ -2,27 +2,27 @@ import type { CollectionEntry } from 'astro:content'
|
||||
import { getCollection } from 'astro:content'
|
||||
|
||||
/** Note: this function filters out draft posts based on the environment */
|
||||
export async function getAllPosts() {
|
||||
return await getCollection('post', ({ data }) => {
|
||||
export async function getAllPosts(): Promise<CollectionEntry<'post'>[]> {
|
||||
return await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
|
||||
return !data.draft && !data.category?.startsWith('日记-20')
|
||||
})
|
||||
}
|
||||
|
||||
export async function getAllSortedPosts() {
|
||||
export async function getAllSortedPosts(): Promise<CollectionEntry<'post'>[]> {
|
||||
return sortMDByDate(await getAllPosts())
|
||||
}
|
||||
|
||||
export const getallDiaries = async () => {
|
||||
return await getCollection('post', ({ data }) => {
|
||||
export const getAllDiaries = async (): Promise<CollectionEntry<'post'>[]> => {
|
||||
return await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
|
||||
return !data.draft && data.category?.startsWith('日记-20')
|
||||
})
|
||||
}
|
||||
|
||||
export const getallDiariesSorted = async () => {
|
||||
return sortMDByDate(await getallDiaries())
|
||||
export const getAllDiariesSorted = async (): Promise<CollectionEntry<'post'>[]> => {
|
||||
return sortMDByDate(await getAllDiaries())
|
||||
}
|
||||
|
||||
export function sortMDByDate(posts: Array<CollectionEntry<'post'>>) {
|
||||
export function sortMDByDate(posts: Array<CollectionEntry<'post'>>): CollectionEntry<'post'>[] {
|
||||
return posts.sort((a, b) => {
|
||||
const aDate = new Date(a.data.date).valueOf()
|
||||
const bDate = new Date(b.data.date).valueOf()
|
||||
@ -31,12 +31,12 @@ export function sortMDByDate(posts: Array<CollectionEntry<'post'>>) {
|
||||
}
|
||||
|
||||
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
|
||||
export function getAllTags(posts: Array<CollectionEntry<'post'>>) {
|
||||
export function getAllTags(posts: Array<CollectionEntry<'post'>>): string[] {
|
||||
return posts.flatMap((post) => [...(post.data?.tags ?? [])])
|
||||
}
|
||||
|
||||
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
|
||||
export function getUniqueTags(posts: Array<CollectionEntry<'post'>>) {
|
||||
export function getUniqueTags(posts: Array<CollectionEntry<'post'>>): string[] {
|
||||
return [...new Set(getAllTags(posts))]
|
||||
}
|
||||
|
||||
@ -58,7 +58,7 @@ export function getAllCategories(posts: Array<CollectionEntry<'post'>>): string[
|
||||
}
|
||||
|
||||
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
|
||||
export function getUniqueCategories(posts: Array<CollectionEntry<'post'>>) {
|
||||
export function getUniqueCategories(posts: Array<CollectionEntry<'post'>>): string[] {
|
||||
return [...new Set(getAllCategories(posts))]
|
||||
}
|
||||
|
||||
@ -74,7 +74,7 @@ export function getUniqueCategoriesWithCount(
|
||||
].sort((a, b) => b[1] - a[1])
|
||||
}
|
||||
|
||||
export function getIdToSlugMap(posts: Array<CollectionEntry<'post'>>) {
|
||||
export function getIdToSlugMap(posts: Array<CollectionEntry<'post'>>): Record<string, string> {
|
||||
return posts.reduce(
|
||||
(acc, post) => {
|
||||
acc[post.id.split('.md')[0]] = post.slug
|
||||
|
Reference in New Issue
Block a user