mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-17 03:56:19 +02:00
refactor: restructure project configuration and utility modules
- Move configuration and utility files to more organized locations - Update import paths across the project to reflect new file structure - Simplify content and internationalization utilities - Remove redundant configuration files and consolidate logic - Add prefetch configuration to Astro config for improved performance
This commit is contained in:
parent
a26031d490
commit
fc1daf4335
23 changed files with 380 additions and 393 deletions
|
@ -3,11 +3,11 @@ import type { ThemeConfig } from '@/types'
|
|||
export const themeConfig: ThemeConfig = {
|
||||
// SITE INFORMATION >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> START
|
||||
site: {
|
||||
title: 'Retypeset',
|
||||
subtitle: '再现版式之美',
|
||||
description: '重新编排 - 再现版式之美 | Retypeset - Rediscover the beauty of typography',
|
||||
author: 'radishzz',
|
||||
url: 'https://retypeset.radishzz.cc',
|
||||
title: 'Retypeset', // site title
|
||||
subtitle: '再现版式之美', // site subtitle
|
||||
description: 'This is Retypeset, an elegant and open-source Astro blog theme, help you rediscover the beauty of typography.', // site description for SEO
|
||||
author: 'radishzz', // author name
|
||||
url: 'https://retypeset.radishzz.cc', // site url
|
||||
favicon: '/image/logo.svg', // or https://image.example.com/logo.svg. Support only webp, svg, png
|
||||
},
|
||||
// SITE INFORMATION >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
|
||||
|
@ -60,20 +60,20 @@ export const themeConfig: ThemeConfig = {
|
|||
facebookLink: '', // facebook profile link
|
||||
// site verification
|
||||
verification: {
|
||||
google: '',
|
||||
bing: '',
|
||||
yandex: '',
|
||||
baidu: '',
|
||||
google: '', // Google Search Console: https://search.google.com/search-console
|
||||
bing: '', // Bing Webmaster Tools: https://www.bing.com/webmasters
|
||||
yandex: '', // Yandex Webmaster: https://webmaster.yandex.com
|
||||
baidu: '', // Baidu Search: https://ziyuan.baidu.com
|
||||
},
|
||||
// site analytics
|
||||
googleAnalyticsID: '',
|
||||
umamiAnalyticsID: '',
|
||||
googleAnalyticsID: '', // Google Analytics: https://analytics.google.com
|
||||
umamiAnalyticsID: '', // Umami Analytics: https://cloud.umami.is
|
||||
// follow verification
|
||||
follow: {
|
||||
feedID: '',
|
||||
userID: '',
|
||||
feedID: '', // feed ID
|
||||
userID: '', // user ID
|
||||
},
|
||||
siteScreenshot: '', // please take a screenshot of your website homepage to show on twitter card
|
||||
siteScreenshot: '', // take a screenshot of your website homepage to show on twitter card
|
||||
},
|
||||
// SEO SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
|
||||
|
||||
|
@ -96,16 +96,28 @@ export const themeConfig: ThemeConfig = {
|
|||
|
||||
// PRELOAD SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> START
|
||||
preload: {
|
||||
cdn: 'https://cdn.jsdelivr.net', // Recommended to keep default. Unless you can proxy https://cdn.jsdelivr.net/gh/radishzzz/astro-theme-retypeset/src/styles/font.css
|
||||
cdn: 'https://cdn.jsdelivr.net', // Keep the default CDN unless you can proxy https://cdn.jsdelivr.net/gh/radishzzz/astro-theme-retypeset/src/styles/font.css
|
||||
commentURL: '', // https://comment.example.com/
|
||||
imageHostURL: '', // https://image.example.com/
|
||||
// If you proxy analytics requests to the custom domain, you can fill in below
|
||||
customGoogleAnalyticsURL: '', // https://custom.example.com/
|
||||
customUmamiAnalyticsURL: '', // https://custom.example.com/
|
||||
customUmamiAnalyticsJS: '', // https://custom.example.com/custom.js
|
||||
lazyload: false,
|
||||
},
|
||||
// PRELOAD SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
|
||||
|
||||
// ABOUT PAGE CONTENT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> START
|
||||
about: {
|
||||
// Required: Content for about page in default language
|
||||
'[locale]': '关于我',
|
||||
// Optional: Content for about page in other languages. If not set, will fallback to [locale] content
|
||||
'zh-tw': '關於我',
|
||||
'ja': '私について',
|
||||
'en': 'About me',
|
||||
'es': 'Sobre mí',
|
||||
'ru': 'Обо мне',
|
||||
},
|
||||
// ABOUT PAGE CONTENT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
|
||||
}
|
||||
|
||||
export default themeConfig
|
|
@ -1,8 +1,8 @@
|
|||
---
|
||||
import Footer from '@/components/Footer.astro'
|
||||
import Header from '@/components/Header.astro'
|
||||
import LanguageSwitcher from '@/components/LanguageSwitcher.astro'
|
||||
import Navigation from '@/components/Navigation.astro'
|
||||
import LanguageSwitcher from '@/components/LangSwitch.astro'
|
||||
import Navigation from '@/components/Navbar.astro'
|
||||
import PhotoSwipe from '@/components/PhotoSwipe.astro'
|
||||
import Scrollbar from '@/components/Scrollbar.astro'
|
||||
import ThemeToggle from '@/components/ThemeToggle.astro'
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
---
|
||||
import { themeConfig } from '@/config'
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { generateLanguagePaths } from '@/utils/i18n'
|
||||
|
||||
export function getStaticPaths() {
|
||||
return themeConfig.global.moreLocale.map(lang => ({
|
||||
params: { lang },
|
||||
}))
|
||||
return generateLanguagePaths()
|
||||
}
|
||||
---
|
||||
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
---
|
||||
import { themeConfig } from '@/config'
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { getPinnedPosts, getPosts } from '@/utils/content.config'
|
||||
import { getPinnedPosts, getPosts } from '@/utils/content'
|
||||
import { generateLanguagePaths } from '@/utils/i18n'
|
||||
|
||||
export function getStaticPaths() {
|
||||
return themeConfig.global.moreLocale.map(lang => ({
|
||||
params: { lang },
|
||||
}))
|
||||
return generateLanguagePaths()
|
||||
}
|
||||
|
||||
const { lang } = Astro.params
|
||||
|
|
|
@ -1,26 +1,18 @@
|
|||
---
|
||||
import { themeConfig } from '@/config'
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { checkSlugDuplication } from '@/utils/content.config'
|
||||
import { checkSlugDuplication } from '@/utils/content'
|
||||
import { generateMultiLangPostPaths } from '@/utils/i18n'
|
||||
import { getCollection } from 'astro:content'
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const posts = await getCollection('posts')
|
||||
|
||||
|
||||
const duplicates = await checkSlugDuplication(posts)
|
||||
if (duplicates.length > 0) {
|
||||
throw new Error(`Slug conflicts found:\n${duplicates.join('\n')}`)
|
||||
}
|
||||
|
||||
return themeConfig.global.moreLocale.flatMap(lang =>
|
||||
posts.map(post => ({
|
||||
params: {
|
||||
lang,
|
||||
slug: post.data.slug || post.slug,
|
||||
},
|
||||
props: { post },
|
||||
})),
|
||||
)
|
||||
return generateMultiLangPostPaths(posts)
|
||||
}
|
||||
|
||||
const { post } = Astro.props
|
||||
|
|
|
@ -1,38 +1,8 @@
|
|||
import type { APIContext } from 'astro'
|
||||
import type { CollectionEntry } from 'astro:content'
|
||||
import themeConfig from '@/config'
|
||||
import rss from '@astrojs/rss'
|
||||
import { getCollection } from 'astro:content'
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import sanitizeHtml from 'sanitize-html'
|
||||
import { generateRSS } from '@/utils/rss'
|
||||
|
||||
const parser = new MarkdownIt()
|
||||
const { title, description, url } = themeConfig.site
|
||||
const { moreLocale } = themeConfig.global
|
||||
const followConfig = themeConfig.seo?.follow
|
||||
|
||||
// Returns first 200 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, 200).trim()
|
||||
return excerpt.length === 200 ? `${excerpt}...` : excerpt
|
||||
}
|
||||
|
||||
// Return 404 for invalid language paths
|
||||
function return404() {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
statusText: 'Not found',
|
||||
})
|
||||
}
|
||||
|
||||
// Type for supported non-default languages
|
||||
type SupportedLanguage = typeof moreLocale[number]
|
||||
|
@ -46,43 +16,12 @@ export async function GET({ params }: APIContext) {
|
|||
const lang = params.lang as SupportedLanguage
|
||||
|
||||
// Return 404 if language is not supported
|
||||
if (!moreLocale.includes(lang))
|
||||
return return404()
|
||||
if (!moreLocale.includes(lang)) {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
statusText: 'Not found',
|
||||
})
|
||||
}
|
||||
|
||||
// Get posts for specific language (including universal posts)
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
|
||||
(!data.draft && (data.lang === lang || data.lang === '')),
|
||||
)
|
||||
|
||||
return rss({
|
||||
title: `${title} (${lang})`,
|
||||
description,
|
||||
site: url,
|
||||
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 language prefix
|
||||
link: new URL(`${lang}/posts/${post.data.slug || post.slug}/`, url).toString(),
|
||||
// Convert markdown content to HTML, allowing img tags
|
||||
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}</language>
|
||||
${followConfig?.feedID && followConfig?.userID
|
||||
? `<follow_challenge>
|
||||
<feedId>${followConfig.feedID}</feedId>
|
||||
<userId>${followConfig.userID}</userId>
|
||||
</follow_challenge>`
|
||||
: ''
|
||||
}
|
||||
`.trim(),
|
||||
})
|
||||
return generateRSS({ lang })
|
||||
}
|
||||
|
|
|
@ -1,16 +1,11 @@
|
|||
---
|
||||
import { themeConfig } from '@/config'
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { getAllTags, getPostsByTag } from '@/utils/content.config'
|
||||
import { getAllTags, getPostsByTag } from '@/utils/content'
|
||||
import { generateMultiLangTagPaths } from '@/utils/i18n'
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const tags = await getAllTags()
|
||||
return themeConfig.global.moreLocale.flatMap(lang =>
|
||||
tags.map(tag => ({
|
||||
params: { lang, tags: tag },
|
||||
props: { tags: tag },
|
||||
})),
|
||||
)
|
||||
return generateMultiLangTagPaths(tags)
|
||||
}
|
||||
|
||||
const { lang } = Astro.params
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
---
|
||||
import { themeConfig } from '@/config'
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { getAllTags } from '@/utils/content.config'
|
||||
import { getAllTags } from '@/utils/content'
|
||||
import { generateLanguagePaths } from '@/utils/i18n'
|
||||
|
||||
export function getStaticPaths() {
|
||||
return themeConfig.global.moreLocale.map(lang => ({
|
||||
params: { lang },
|
||||
}))
|
||||
return generateLanguagePaths()
|
||||
}
|
||||
|
||||
const { lang } = Astro.params
|
||||
|
@ -21,3 +19,4 @@ const allTags = await getAllTags()
|
|||
</a>
|
||||
))}
|
||||
</div>
|
||||
</Layout>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { getPinnedPosts, getPosts } from '@/utils/content.config'
|
||||
import { getPinnedPosts, getPosts } from '@/utils/content'
|
||||
|
||||
const posts = await getPosts()
|
||||
const pinnedPosts = await getPinnedPosts()
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
---
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { checkSlugDuplication } from '@/utils/content.config'
|
||||
import { checkSlugDuplication } from '@/utils/content'
|
||||
import { generatePostPaths } from '@/utils/i18n'
|
||||
import { getCollection } from 'astro:content'
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const posts = await getCollection('posts')
|
||||
|
||||
|
||||
const duplicates = await checkSlugDuplication(posts)
|
||||
if (duplicates.length > 0) {
|
||||
throw new Error(`Slug conflicts found:\n${duplicates.join('\n')}`)
|
||||
}
|
||||
|
||||
return posts.map(post => ({
|
||||
params: {
|
||||
slug: post.data.slug || post.slug,
|
||||
},
|
||||
props: { post },
|
||||
}))
|
||||
return generatePostPaths(posts)
|
||||
}
|
||||
|
||||
const { post } = Astro.props
|
||||
|
|
|
@ -1,65 +1,5 @@
|
|||
import type { CollectionEntry } from 'astro:content'
|
||||
import themeConfig from '@/config'
|
||||
import rss from '@astrojs/rss'
|
||||
import { getCollection } from 'astro:content'
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import sanitizeHtml from 'sanitize-html'
|
||||
|
||||
const parser = new MarkdownIt()
|
||||
const { title, description, url } = themeConfig.site
|
||||
const { locale } = themeConfig.global
|
||||
const followConfig = themeConfig.seo?.follow
|
||||
|
||||
// Returns first 200 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, 200).trim()
|
||||
return excerpt.length === 200 ? `${excerpt}...` : excerpt
|
||||
}
|
||||
import { generateRSS } from '@/utils/rss'
|
||||
|
||||
export async function GET() {
|
||||
// Get all posts for default language (including universal posts)
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
|
||||
(!data.draft && (data.lang === locale || data.lang === '')),
|
||||
)
|
||||
|
||||
return rss({
|
||||
title,
|
||||
description,
|
||||
site: url,
|
||||
items: posts.map((post: CollectionEntry<'posts'>) => ({
|
||||
title: post.data.title,
|
||||
pubDate: post.data.published,
|
||||
description: post.data.description || getExcerpt(post.body),
|
||||
// Generate absolute URL for post
|
||||
link: new URL(`posts/${post.data.slug || post.slug}/`, url).toString(),
|
||||
// Convert markdown content to HTML, allowing img tags
|
||||
content: post.body
|
||||
? sanitizeHtml(parser.render(post.body), {
|
||||
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
|
||||
})
|
||||
: '',
|
||||
})),
|
||||
// Add XML namespaces for language and follow challenge
|
||||
customData: `
|
||||
<language>${locale}</language>
|
||||
${followConfig?.feedID && followConfig?.userID
|
||||
? `<follow_challenge>
|
||||
<feedId>${followConfig.feedID}</feedId>
|
||||
<userId>${followConfig.userID}</userId>
|
||||
</follow_challenge>`
|
||||
: ''
|
||||
}
|
||||
`.trim(),
|
||||
})
|
||||
return generateRSS()
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { getAllTags, getPostsByTag } from '@/utils/content.config'
|
||||
import { getAllTags, getPostsByTag } from '@/utils/content'
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const tags = await getAllTags()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { getAllTags } from '@/utils/content.config'
|
||||
import { getAllTags } from '@/utils/content'
|
||||
|
||||
const allTags = await getAllTags()
|
||||
---
|
||||
|
|
|
@ -10,19 +10,16 @@
|
|||
--os-handle-perpendicular-size-active: 160%;
|
||||
--os-handle-interactive-area-offset: 3px;
|
||||
}
|
||||
|
||||
.scrollbar-light {
|
||||
--os-handle-bg: #C9C4C1;
|
||||
--os-handle-bg-hover: #B8B3B0;
|
||||
--os-handle-bg-active: #B8B3B0;
|
||||
}
|
||||
|
||||
.scrollbar-dark {
|
||||
--os-handle-bg: #383838;
|
||||
--os-handle-bg-hover: #464646;
|
||||
--os-handle-bg-active: #464646;
|
||||
}
|
||||
|
||||
@media (max-width: 1023px) {
|
||||
.os-scrollbar {
|
||||
display: none !important;
|
||||
|
|
9
src/types/index.d.ts
vendored
9
src/types/index.d.ts
vendored
|
@ -1,5 +1,7 @@
|
|||
import type { langPath } from '@/utils/ui'
|
||||
|
||||
type Exclude<T, U> = T extends U ? never : T
|
||||
|
||||
export interface ThemeConfig {
|
||||
|
||||
site: {
|
||||
|
@ -84,7 +86,12 @@ export interface ThemeConfig {
|
|||
customGoogleAnalyticsURL?: string
|
||||
customUmamiAnalyticsURL?: string
|
||||
customUmamiAnalyticsJS?: string
|
||||
lazyload: boolean
|
||||
}
|
||||
|
||||
about: {
|
||||
[key in ThemeConfig['global']['locale']]: string
|
||||
} & {
|
||||
[key in ThemeConfig['global']['moreLocale'][number]]?: string
|
||||
}
|
||||
}
|
||||
|
||||
|
|
38
src/utils/i18n.ts
Normal file
38
src/utils/i18n.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import type { CollectionEntry } from 'astro:content'
|
||||
import { themeConfig } from '@/config'
|
||||
|
||||
export function generateLanguagePaths() {
|
||||
return themeConfig.global.moreLocale.map(lang => ({
|
||||
params: { lang },
|
||||
}))
|
||||
}
|
||||
|
||||
export function generatePostPaths(posts: CollectionEntry<'posts'>[]) {
|
||||
return posts.map(post => ({
|
||||
params: {
|
||||
slug: post.data.slug || post.slug,
|
||||
},
|
||||
props: { post },
|
||||
}))
|
||||
}
|
||||
|
||||
export function generateMultiLangPostPaths(posts: CollectionEntry<'posts'>[]) {
|
||||
return themeConfig.global.moreLocale.flatMap(lang =>
|
||||
posts.map(post => ({
|
||||
params: {
|
||||
lang,
|
||||
slug: post.data.slug || post.slug,
|
||||
},
|
||||
props: { post },
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
export function generateMultiLangTagPaths(tags: string[]) {
|
||||
return themeConfig.global.moreLocale.flatMap(lang =>
|
||||
tags.map(tag => ({
|
||||
params: { lang, tags: tag },
|
||||
props: { tags: tag },
|
||||
})),
|
||||
)
|
||||
}
|
71
src/utils/rss.ts
Normal file
71
src/utils/rss.ts
Normal file
|
@ -0,0 +1,71 @@
|
|||
import type { CollectionEntry } from 'astro:content'
|
||||
import themeConfig from '@/config'
|
||||
import rss from '@astrojs/rss'
|
||||
import { getCollection } from 'astro:content'
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import sanitizeHtml from 'sanitize-html'
|
||||
|
||||
const parser = new MarkdownIt()
|
||||
const { title, description, url } = themeConfig.site
|
||||
const followConfig = themeConfig.seo?.follow
|
||||
|
||||
// Returns first 200 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, 200).trim()
|
||||
return excerpt.length === 200 ? `${excerpt}...` : excerpt
|
||||
}
|
||||
|
||||
interface GenerateRSSOptions {
|
||||
lang?: string
|
||||
}
|
||||
|
||||
export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
|
||||
// Get posts for specific language (including universal posts)
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
|
||||
(!data.draft && (data.lang === lang || data.lang === '')),
|
||||
)
|
||||
|
||||
return rss({
|
||||
title: lang ? `${title} (${lang})` : title,
|
||||
description,
|
||||
site: url,
|
||||
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 optional language prefix
|
||||
link: new URL(
|
||||
`${lang ? `${lang}/` : ''}posts/${post.data.slug || post.slug}/`,
|
||||
url,
|
||||
).toString(),
|
||||
// Convert markdown content to HTML, allowing img tags
|
||||
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