mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 11:41:17 +02:00
optimize: rss generation
This commit is contained in:
parent
54902da6dd
commit
ff59dc1a7c
6 changed files with 16 additions and 23 deletions
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
|
@ -86,6 +86,7 @@
|
||||||
"noreferrer",
|
"noreferrer",
|
||||||
"Noto",
|
"Noto",
|
||||||
"oklch",
|
"oklch",
|
||||||
|
"opengraph",
|
||||||
"overlayscrollbars",
|
"overlayscrollbars",
|
||||||
"pagefind",
|
"pagefind",
|
||||||
"partytown",
|
"partytown",
|
||||||
|
|
|
@ -22,7 +22,7 @@ const initMetaTheme = mode === 'dark' ? darkMode : lightMode
|
||||||
const pageTitle = postTitle ? `${postTitle} | ${title}` : `${title} - ${subtitle}`
|
const pageTitle = postTitle ? `${postTitle} | ${title}` : `${title} - ${subtitle}`
|
||||||
const pageDescription = postDescription || description
|
const pageDescription = postDescription || description
|
||||||
// TODO: Change openGraph image fallback url
|
// 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>
|
<head>
|
||||||
<!-- Basic info -->
|
<!-- Basic info -->
|
||||||
|
|
|
@ -1,20 +1,16 @@
|
||||||
import type { APIContext } from 'astro'
|
import type { APIContext } from 'astro'
|
||||||
import themeConfig from '@/config'
|
import { moreLocales } from '@/i18n/config'
|
||||||
import { getMultiLangRoutes } from '@/i18n/route'
|
|
||||||
import { generateRSS } from '@/utils/rss'
|
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
|
// Generate static paths for all supported languages
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return getMultiLangRoutes()
|
return moreLocales.map(lang => ({
|
||||||
|
params: { lang },
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET({ params }: APIContext) {
|
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
|
// Return 404 if language is not supported
|
||||||
if (!moreLocales.includes(lang)) {
|
if (!moreLocales.includes(lang)) {
|
||||||
|
|
|
@ -1,20 +1,16 @@
|
||||||
import type { APIContext } from 'astro'
|
import type { APIContext } from 'astro'
|
||||||
import themeConfig from '@/config'
|
import { moreLocales } from '@/i18n/config'
|
||||||
import { getMultiLangRoutes } from '@/i18n/route'
|
|
||||||
import { generateRSS } from '@/utils/rss'
|
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
|
// Generate static paths for all supported languages
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return getMultiLangRoutes()
|
return moreLocales.map(lang => ({
|
||||||
|
params: { lang },
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET({ params }: APIContext) {
|
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
|
// Return 404 if language is not supported
|
||||||
if (!moreLocales.includes(lang)) {
|
if (!moreLocales.includes(lang)) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ const pages = Object.fromEntries(
|
||||||
|
|
||||||
// Configure Open Graph image generation route
|
// Configure Open Graph image generation route
|
||||||
export const { getStaticPaths, GET } = OGImageRoute({
|
export const { getStaticPaths, GET } = OGImageRoute({
|
||||||
param: 'route',
|
param: 'image',
|
||||||
pages,
|
pages,
|
||||||
getImageOptions: (_path, page) => ({
|
getImageOptions: (_path, page) => ({
|
||||||
title: page.title,
|
title: page.title,
|
|
@ -1,5 +1,6 @@
|
||||||
import type { CollectionEntry } from 'astro:content'
|
import type { CollectionEntry } from 'astro:content'
|
||||||
import themeConfig from '@/config'
|
import themeConfig from '@/config'
|
||||||
|
import { defaultLocale } from '@/i18n/config'
|
||||||
import rss from '@astrojs/rss'
|
import rss from '@astrojs/rss'
|
||||||
import { getCollection } from 'astro:content'
|
import { getCollection } from 'astro:content'
|
||||||
import MarkdownIt from 'markdown-it'
|
import MarkdownIt from 'markdown-it'
|
||||||
|
@ -8,7 +9,6 @@ import sanitizeHtml from 'sanitize-html'
|
||||||
const parser = new MarkdownIt()
|
const parser = new MarkdownIt()
|
||||||
const { title, description, url } = themeConfig.site
|
const { title, description, url } = themeConfig.site
|
||||||
const followConfig = themeConfig.seo?.follow
|
const followConfig = themeConfig.seo?.follow
|
||||||
const defaultLang = themeConfig.global.locale
|
|
||||||
|
|
||||||
// Returns first 50 chars with proper truncation
|
// Returns first 50 chars with proper truncation
|
||||||
function getExcerpt(content: string): string {
|
function getExcerpt(content: string): string {
|
||||||
|
@ -32,7 +32,7 @@ export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
|
||||||
const posts = await getCollection(
|
const posts = await getCollection(
|
||||||
'posts',
|
'posts',
|
||||||
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
|
({ 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({
|
return rss({
|
||||||
|
@ -45,7 +45,7 @@ export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
|
||||||
description: post.data.description || getExcerpt(post.body),
|
description: post.data.description || getExcerpt(post.body),
|
||||||
// Generate absolute URL with correct language prefix based on post language
|
// Generate absolute URL with correct language prefix based on post language
|
||||||
link: new URL(
|
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,
|
url,
|
||||||
).toString(),
|
).toString(),
|
||||||
// Convert markdown content to HTML, allowing img tags
|
// Convert markdown content to HTML, allowing img tags
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue