mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 03:32:51 +02:00
refactor: optimize rss generation logic
This commit is contained in:
parent
f34b0cb46b
commit
0888b59c5f
7 changed files with 128 additions and 155 deletions
|
@ -4,7 +4,7 @@ export const themeConfig: ThemeConfig = {
|
|||
// SITE INFORMATION >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> START
|
||||
site: {
|
||||
// site title
|
||||
title: '重新编排',
|
||||
title: 'Retypeset',
|
||||
// site subtitle
|
||||
subtitle: '再现版式之美',
|
||||
// use i18n title/subtitle from src/i18n/ui.ts instead of static ones above
|
||||
|
@ -126,6 +126,7 @@ export const themeConfig: ThemeConfig = {
|
|||
// apiflash access key
|
||||
// automatically generate website screenshots for open graph preview images
|
||||
// get your access key at: https://apiflash.com/
|
||||
// check your open graph at: https://orcascan.com/tools/open-graph-validator
|
||||
apiflashKey: '',
|
||||
},
|
||||
// SEO SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
|
||||
|
|
|
@ -2,7 +2,6 @@ import type { APIContext } from 'astro'
|
|||
import { moreLocales } from '@/config'
|
||||
import { generateRSS } from '@/utils/rss'
|
||||
|
||||
// Generate static paths for all supported languages
|
||||
export function getStaticPaths() {
|
||||
return moreLocales.map(lang => ({
|
||||
params: { lang },
|
||||
|
@ -11,14 +10,5 @@ export function getStaticPaths() {
|
|||
|
||||
export async function GET({ params }: APIContext) {
|
||||
const lang = params.lang as typeof moreLocales[number]
|
||||
|
||||
// Return 404 if language is not supported
|
||||
if (!moreLocales.includes(lang)) {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
statusText: 'Not found',
|
||||
})
|
||||
}
|
||||
|
||||
return generateRSS({ lang })
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ import type { APIContext } from 'astro'
|
|||
import { moreLocales } from '@/config'
|
||||
import { generateRSS } from '@/utils/rss'
|
||||
|
||||
// Generate static paths for all supported languages
|
||||
export function getStaticPaths() {
|
||||
return moreLocales.map(lang => ({
|
||||
params: { lang },
|
||||
|
@ -11,14 +10,5 @@ export function getStaticPaths() {
|
|||
|
||||
export async function GET({ params }: APIContext) {
|
||||
const lang = params.lang as typeof moreLocales[number]
|
||||
|
||||
// Return 404 if language is not supported
|
||||
if (!moreLocales.includes(lang)) {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
statusText: 'Not found',
|
||||
})
|
||||
}
|
||||
|
||||
return generateRSS({ lang })
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import { getCollection } from 'astro:content'
|
|||
// eslint-disable-next-line antfu/no-top-level-await
|
||||
const blogEntries = await getCollection('posts')
|
||||
|
||||
// Convert to page data objects
|
||||
// Convert blog entries into a lookup object with slug as key and title/description as value
|
||||
const pages = Object.fromEntries(
|
||||
blogEntries.map((post: CollectionEntry<'posts'>) => [
|
||||
post.slug,
|
||||
|
|
|
@ -5,7 +5,7 @@ import sanitizeHtml from 'sanitize-html'
|
|||
|
||||
const parser = new MarkdownIt()
|
||||
|
||||
type ExcerptScene = 'list' | 'meta' | 'og'
|
||||
type ExcerptScene = 'list' | 'meta' | 'og' | 'rss'
|
||||
|
||||
// Excerpt length in different scenarios
|
||||
const EXCERPT_LENGTHS: Record<ExcerptScene, {
|
||||
|
@ -24,6 +24,10 @@ const EXCERPT_LENGTHS: Record<ExcerptScene, {
|
|||
cjk: 75,
|
||||
other: 150,
|
||||
},
|
||||
rss: {
|
||||
cjk: 120,
|
||||
other: 240,
|
||||
},
|
||||
}
|
||||
|
||||
const isCJKLang = (lang: string) => ['zh', 'zh-tw', 'ja'].includes(lang)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import type { CollectionEntry } from 'astro:content'
|
||||
import themeConfig, { defaultLocale } from '@/config'
|
||||
import { defaultLocale, themeConfig } from '@/config'
|
||||
import { generateDescription } from '@/utils/description'
|
||||
import rss from '@astrojs/rss'
|
||||
import { getCollection } from 'astro:content'
|
||||
import MarkdownIt from 'markdown-it'
|
||||
|
@ -9,19 +10,6 @@ const parser = new MarkdownIt()
|
|||
const { title, description, url } = themeConfig.site
|
||||
const followConfig = themeConfig.seo?.follow
|
||||
|
||||
// Returns first 98 chars with proper truncation
|
||||
function getExcerpt(content: string): string {
|
||||
if (!content)
|
||||
return ''
|
||||
// Convert markdown to plain text by removing all HTML tags
|
||||
const plainText = sanitizeHtml(parser.render(content), {
|
||||
allowedTags: [],
|
||||
allowedAttributes: {},
|
||||
})
|
||||
const excerpt = plainText.slice(0, 98).trim()
|
||||
return excerpt.length === 98 ? `${excerpt}...` : excerpt
|
||||
}
|
||||
|
||||
interface GenerateRSSOptions {
|
||||
lang?: string
|
||||
}
|
||||
|
@ -36,34 +24,34 @@ export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
|
|||
|
||||
return rss({
|
||||
title: lang ? `${title}_${lang}` : title,
|
||||
description,
|
||||
site: url,
|
||||
description,
|
||||
customData: `
|
||||
<copyright>Copyright © ${new Date().getFullYear()} ${themeConfig.site.author}</copyright>
|
||||
<language>${lang || themeConfig.global.locale}</language>
|
||||
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
|
||||
${followConfig?.feedID && followConfig?.userID
|
||||
? `<follow_challenge>
|
||||
<feedId>${followConfig.feedID}</feedId>
|
||||
<userId>${followConfig.userID}</userId>
|
||||
</follow_challenge>`
|
||||
: ''
|
||||
}
|
||||
`.trim(),
|
||||
items: posts.map((post: CollectionEntry<'posts'>) => ({
|
||||
title: post.data.title,
|
||||
pubDate: post.data.published,
|
||||
description: post.data.description || getExcerpt(post.body),
|
||||
// Generate absolute URL with correct language prefix based on post language
|
||||
// Generate URL with language prefix and abbrlink/slug
|
||||
link: new URL(
|
||||
`${post.data.lang !== defaultLocale && post.data.lang !== '' ? `${post.data.lang}/` : ''}posts/${post.data.abbrlink || post.slug}/`,
|
||||
url,
|
||||
).toString(),
|
||||
// Convert markdown content to HTML, allowing img tags
|
||||
description: generateDescription(post, 'rss'),
|
||||
pubDate: post.data.published,
|
||||
content: post.body
|
||||
? sanitizeHtml(parser.render(post.body), {
|
||||
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
|
||||
})
|
||||
: '',
|
||||
})),
|
||||
// Add XML namespaces for language and follow challenge
|
||||
customData: `
|
||||
<language>${lang || themeConfig.global.locale}</language>
|
||||
${followConfig?.feedID && followConfig?.userID
|
||||
? `<follow_challenge>
|
||||
<feedId>${followConfig.feedID}</feedId>
|
||||
<userId>${followConfig.userID}</userId>
|
||||
</follow_challenge>`
|
||||
: ''
|
||||
}
|
||||
`.trim(),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue