optimize: rss generation

This commit is contained in:
radishzzz 2025-03-14 23:34:10 +00:00
parent 54902da6dd
commit ff59dc1a7c
6 changed files with 16 additions and 23 deletions

View file

@ -86,6 +86,7 @@
"noreferrer",
"Noto",
"oklch",
"opengraph",
"overlayscrollbars",
"pagefind",
"partytown",

View file

@ -22,7 +22,7 @@ const initMetaTheme = mode === 'dark' ? darkMode : lightMode
const pageTitle = postTitle ? `${postTitle} | ${title}` : `${title} - ${subtitle}`
const pageDescription = postDescription || description
// TODO: Change openGraph image fallback url
const pageImage = postSlug ? `${url}/og/${postSlug}.png` : 'https://placehold.co/1200x630'
const pageImage = postSlug ? `${url}/opengraph/${postSlug}.png` : 'https://placehold.co/1200x630'
---
<head>
<!-- Basic info -->

View file

@ -1,20 +1,16 @@
import type { APIContext } from 'astro'
import themeConfig from '@/config'
import { getMultiLangRoutes } from '@/i18n/route'
import { moreLocales } from '@/i18n/config'
import { generateRSS } from '@/utils/rss'
const { moreLocales } = themeConfig.global
// Type for supported non-default languages
type SupportedLanguage = typeof moreLocales[number]
// Generate static paths for all supported languages
export function getStaticPaths() {
return getMultiLangRoutes()
return moreLocales.map(lang => ({
params: { lang },
}))
}
export async function GET({ params }: APIContext) {
const lang = params.lang as SupportedLanguage
const lang = params.lang as typeof moreLocales[number]
// Return 404 if language is not supported
if (!moreLocales.includes(lang)) {

View file

@ -1,20 +1,16 @@
import type { APIContext } from 'astro'
import themeConfig from '@/config'
import { getMultiLangRoutes } from '@/i18n/route'
import { moreLocales } from '@/i18n/config'
import { generateRSS } from '@/utils/rss'
const { moreLocales } = themeConfig.global
// Type for supported non-default languages
type SupportedLanguage = typeof moreLocales[number]
// Generate static paths for all supported languages
export function getStaticPaths() {
return getMultiLangRoutes()
return moreLocales.map(lang => ({
params: { lang },
}))
}
export async function GET({ params }: APIContext) {
const lang = params.lang as SupportedLanguage
const lang = params.lang as typeof moreLocales[number]
// Return 404 if language is not supported
if (!moreLocales.includes(lang)) {

View file

@ -18,7 +18,7 @@ const pages = Object.fromEntries(
// Configure Open Graph image generation route
export const { getStaticPaths, GET } = OGImageRoute({
param: 'route',
param: 'image',
pages,
getImageOptions: (_path, page) => ({
title: page.title,

View file

@ -1,5 +1,6 @@
import type { CollectionEntry } from 'astro:content'
import themeConfig from '@/config'
import { defaultLocale } from '@/i18n/config'
import rss from '@astrojs/rss'
import { getCollection } from 'astro:content'
import MarkdownIt from 'markdown-it'
@ -8,7 +9,6 @@ import sanitizeHtml from 'sanitize-html'
const parser = new MarkdownIt()
const { title, description, url } = themeConfig.site
const followConfig = themeConfig.seo?.follow
const defaultLang = themeConfig.global.locale
// Returns first 50 chars with proper truncation
function getExcerpt(content: string): string {
@ -32,7 +32,7 @@ export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
const posts = await getCollection(
'posts',
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
(!data.draft && (data.lang === lang || data.lang === '' || (lang === undefined && data.lang === defaultLang))),
(!data.draft && (data.lang === lang || data.lang === '' || (lang === undefined && data.lang === defaultLocale))),
)
return rss({
@ -45,7 +45,7 @@ export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
description: post.data.description || getExcerpt(post.body),
// Generate absolute URL with correct language prefix based on post language
link: new URL(
`${post.data.lang !== defaultLang && post.data.lang !== '' ? `${post.data.lang}/` : ''}posts/${post.data.abbrlink || post.slug}/`,
`${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