mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 11:41:17 +02:00
feat: enhance internationalization support with dynamic routing
This commit is contained in:
parent
d6c98880d3
commit
8c19d26cfd
12 changed files with 199 additions and 87 deletions
|
@ -43,6 +43,9 @@ export default defineConfig({
|
||||||
codes,
|
codes,
|
||||||
})),
|
})),
|
||||||
defaultLocale: locale,
|
defaultLocale: locale,
|
||||||
|
routing: {
|
||||||
|
prefixDefaultLocale: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
integrations: [
|
integrations: [
|
||||||
partytown({
|
partytown({
|
||||||
|
|
38
pnpm-lock.yaml
generated
38
pnpm-lock.yaml
generated
|
@ -2736,8 +2736,8 @@ packages:
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
eslint: ^8.57.0 || ^9.0.0
|
eslint: ^8.57.0 || ^9.0.0
|
||||||
|
|
||||||
eslint-plugin-jsdoc@50.6.1:
|
eslint-plugin-jsdoc@50.6.2:
|
||||||
resolution: {integrity: sha512-UWyaYi6iURdSfdVVqvfOs2vdCVz0J40O/z/HTsv2sFjdjmdlUI/qlKLOTmwbPQ2tAfQnE5F9vqx+B+poF71DBQ==}
|
resolution: {integrity: sha512-n7GNZ4czMAAbDg7DsDA7PvHo1IPIUwAXYmxTx6j/hTlXbt5V0x5q/kGkiJ7s4wA9SpB/yaiK8jF7CO237lOLew==}
|
||||||
engines: {node: '>=18'}
|
engines: {node: '>=18'}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
eslint: ^7.0.0 || ^8.0.0 || ^9.0.0
|
eslint: ^7.0.0 || ^8.0.0 || ^9.0.0
|
||||||
|
@ -2930,8 +2930,8 @@ packages:
|
||||||
fastq@1.18.0:
|
fastq@1.18.0:
|
||||||
resolution: {integrity: sha512-QKHXPW0hD8g4UET03SdOdunzSouc9N4AuHdsX8XNcTsuz+yYFILVNIX4l9yHABMhiEI9Db0JTTIpu0wB+Y1QQw==}
|
resolution: {integrity: sha512-QKHXPW0hD8g4UET03SdOdunzSouc9N4AuHdsX8XNcTsuz+yYFILVNIX4l9yHABMhiEI9Db0JTTIpu0wB+Y1QQw==}
|
||||||
|
|
||||||
fdir@6.4.2:
|
fdir@6.4.3:
|
||||||
resolution: {integrity: sha512-KnhMXsKSPZlAhp7+IjUkRZKPb4fUyccpDrdFXbi4QL1qkmFh9kVY09Yox+n4MaOb3lHZ1Tv829C3oaaXoMYPDQ==}
|
resolution: {integrity: sha512-PMXmW2y1hDDfTSRc9gaXIuCCRpuoz3Kaz8cUelp3smouvfT632ozg2vrT6lJsHKKOF59YLbOGfAWGUcKEfRMQw==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
picomatch: ^3 || ^4
|
picomatch: ^3 || ^4
|
||||||
peerDependenciesMeta:
|
peerDependenciesMeta:
|
||||||
|
@ -3545,8 +3545,8 @@ packages:
|
||||||
jsonfile@6.1.0:
|
jsonfile@6.1.0:
|
||||||
resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==}
|
resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==}
|
||||||
|
|
||||||
katex@0.16.20:
|
katex@0.16.21:
|
||||||
resolution: {integrity: sha512-jjuLaMGD/7P8jUTpdKhA9IoqnH+yMFB3sdAFtq5QdAqeP2PjiSbnC3EaguKPNtv6dXXanHxp1ckwvF4a86LBig==}
|
resolution: {integrity: sha512-XvqR7FgOHtWupfMiigNzmh+MgUVmDGU2kXZm899ZkPfcuoPuFxyHmXsgATDpFZDAXCI8tvinaVcDo8PIIJSo4A==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
keyv@4.5.4:
|
keyv@4.5.4:
|
||||||
|
@ -4222,8 +4222,8 @@ packages:
|
||||||
pathe@1.1.2:
|
pathe@1.1.2:
|
||||||
resolution: {integrity: sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==}
|
resolution: {integrity: sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==}
|
||||||
|
|
||||||
pathe@2.0.1:
|
pathe@2.0.2:
|
||||||
resolution: {integrity: sha512-6jpjMpOth5S9ITVu5clZ7NOgHNsv5vRQdheL9ztp2vZmM6fRbLvyua1tiBIL4lk8SAe3ARzeXEly6siXCjDHDw==}
|
resolution: {integrity: sha512-15Ztpk+nov8DR524R4BF7uEuzESgzUEAV4Ah7CUMNGXdE5ELuvxElxGXndBl32vMSsWa1jpNf22Z+Er3sKwq+w==}
|
||||||
|
|
||||||
perfect-debounce@1.0.0:
|
perfect-debounce@1.0.0:
|
||||||
resolution: {integrity: sha512-xCy9V055GLEqoFaHoC1SoLIaLmWctgCUaBaWxDZ7/Zx4CTyX7cJQLJOok/orfjZAh9kEYpjJa4d0KcJmCbctZA==}
|
resolution: {integrity: sha512-xCy9V055GLEqoFaHoC1SoLIaLmWctgCUaBaWxDZ7/Zx4CTyX7cJQLJOok/orfjZAh9kEYpjJa4d0KcJmCbctZA==}
|
||||||
|
@ -5755,7 +5755,7 @@ snapshots:
|
||||||
eslint-plugin-antfu: 2.7.0(eslint@9.18.0(jiti@2.4.2))
|
eslint-plugin-antfu: 2.7.0(eslint@9.18.0(jiti@2.4.2))
|
||||||
eslint-plugin-command: 2.1.0(eslint@9.18.0(jiti@2.4.2))
|
eslint-plugin-command: 2.1.0(eslint@9.18.0(jiti@2.4.2))
|
||||||
eslint-plugin-import-x: 4.6.1(eslint@9.18.0(jiti@2.4.2))(typescript@5.7.3)
|
eslint-plugin-import-x: 4.6.1(eslint@9.18.0(jiti@2.4.2))(typescript@5.7.3)
|
||||||
eslint-plugin-jsdoc: 50.6.1(eslint@9.18.0(jiti@2.4.2))
|
eslint-plugin-jsdoc: 50.6.2(eslint@9.18.0(jiti@2.4.2))
|
||||||
eslint-plugin-jsonc: 2.18.2(eslint@9.18.0(jiti@2.4.2))
|
eslint-plugin-jsonc: 2.18.2(eslint@9.18.0(jiti@2.4.2))
|
||||||
eslint-plugin-n: 17.15.1(eslint@9.18.0(jiti@2.4.2))
|
eslint-plugin-n: 17.15.1(eslint@9.18.0(jiti@2.4.2))
|
||||||
eslint-plugin-no-only-tests: 3.3.0
|
eslint-plugin-no-only-tests: 3.3.0
|
||||||
|
@ -8838,7 +8838,7 @@ snapshots:
|
||||||
|
|
||||||
eslint-flat-config-utils@1.0.0:
|
eslint-flat-config-utils@1.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
pathe: 2.0.1
|
pathe: 2.0.2
|
||||||
|
|
||||||
eslint-import-resolver-node@0.3.9:
|
eslint-import-resolver-node@0.3.9:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
@ -8910,7 +8910,7 @@ snapshots:
|
||||||
- supports-color
|
- supports-color
|
||||||
- typescript
|
- typescript
|
||||||
|
|
||||||
eslint-plugin-jsdoc@50.6.1(eslint@9.18.0(jiti@2.4.2)):
|
eslint-plugin-jsdoc@50.6.2(eslint@9.18.0(jiti@2.4.2)):
|
||||||
dependencies:
|
dependencies:
|
||||||
'@es-joy/jsdoccomment': 0.49.0
|
'@es-joy/jsdoccomment': 0.49.0
|
||||||
are-docs-informative: 0.0.2
|
are-docs-informative: 0.0.2
|
||||||
|
@ -9204,7 +9204,7 @@ snapshots:
|
||||||
dependencies:
|
dependencies:
|
||||||
reusify: 1.0.4
|
reusify: 1.0.4
|
||||||
|
|
||||||
fdir@6.4.2(picomatch@4.0.2):
|
fdir@6.4.3(picomatch@4.0.2):
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
picomatch: 4.0.2
|
picomatch: 4.0.2
|
||||||
|
|
||||||
|
@ -9898,7 +9898,7 @@ snapshots:
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
graceful-fs: 4.2.11
|
graceful-fs: 4.2.11
|
||||||
|
|
||||||
katex@0.16.20:
|
katex@0.16.21:
|
||||||
dependencies:
|
dependencies:
|
||||||
commander: 8.3.0
|
commander: 8.3.0
|
||||||
|
|
||||||
|
@ -10446,7 +10446,7 @@ snapshots:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/katex': 0.16.7
|
'@types/katex': 0.16.7
|
||||||
devlop: 1.1.0
|
devlop: 1.1.0
|
||||||
katex: 0.16.20
|
katex: 0.16.21
|
||||||
micromark-factory-space: 2.0.1
|
micromark-factory-space: 2.0.1
|
||||||
micromark-util-character: 2.1.1
|
micromark-util-character: 2.1.1
|
||||||
micromark-util-symbol: 2.0.1
|
micromark-util-symbol: 2.0.1
|
||||||
|
@ -10669,7 +10669,7 @@ snapshots:
|
||||||
mlly@1.7.4:
|
mlly@1.7.4:
|
||||||
dependencies:
|
dependencies:
|
||||||
acorn: 8.14.0
|
acorn: 8.14.0
|
||||||
pathe: 2.0.1
|
pathe: 2.0.2
|
||||||
pkg-types: 1.3.1
|
pkg-types: 1.3.1
|
||||||
ufo: 1.5.4
|
ufo: 1.5.4
|
||||||
|
|
||||||
|
@ -10910,7 +10910,7 @@ snapshots:
|
||||||
|
|
||||||
pathe@1.1.2: {}
|
pathe@1.1.2: {}
|
||||||
|
|
||||||
pathe@2.0.1: {}
|
pathe@2.0.2: {}
|
||||||
|
|
||||||
perfect-debounce@1.0.0: {}
|
perfect-debounce@1.0.0: {}
|
||||||
|
|
||||||
|
@ -10936,7 +10936,7 @@ snapshots:
|
||||||
dependencies:
|
dependencies:
|
||||||
confbox: 0.1.8
|
confbox: 0.1.8
|
||||||
mlly: 1.7.4
|
mlly: 1.7.4
|
||||||
pathe: 2.0.1
|
pathe: 2.0.2
|
||||||
|
|
||||||
pluralize@8.0.0: {}
|
pluralize@8.0.0: {}
|
||||||
|
|
||||||
|
@ -11347,7 +11347,7 @@ snapshots:
|
||||||
'@types/katex': 0.16.7
|
'@types/katex': 0.16.7
|
||||||
hast-util-from-html-isomorphic: 2.0.0
|
hast-util-from-html-isomorphic: 2.0.0
|
||||||
hast-util-to-text: 4.0.2
|
hast-util-to-text: 4.0.2
|
||||||
katex: 0.16.20
|
katex: 0.16.21
|
||||||
unist-util-visit-parents: 6.0.1
|
unist-util-visit-parents: 6.0.1
|
||||||
vfile: 6.0.3
|
vfile: 6.0.3
|
||||||
|
|
||||||
|
@ -12026,7 +12026,7 @@ snapshots:
|
||||||
|
|
||||||
tinyglobby@0.2.10:
|
tinyglobby@0.2.10:
|
||||||
dependencies:
|
dependencies:
|
||||||
fdir: 6.4.2(picomatch@4.0.2)
|
fdir: 6.4.3(picomatch@4.0.2)
|
||||||
picomatch: 4.0.2
|
picomatch: 4.0.2
|
||||||
|
|
||||||
to-regex-range@5.0.1:
|
to-regex-range@5.0.1:
|
||||||
|
|
|
@ -33,7 +33,6 @@ export const themeConfig: ThemeConfig = {
|
||||||
locale: 'zh', // zh, zh-tw, ja, en, es, ru, default locale setting
|
locale: 'zh', // zh, zh-tw, ja, en, es, ru, default locale setting
|
||||||
moreLocale: ['zh-tw', 'ja', 'en', 'es', 'ru'], // ['zh-tw', 'ja', 'en', 'es', 'ru'], not fill in the default locale code again
|
moreLocale: ['zh-tw', 'ja', 'en', 'es', 'ru'], // ['zh-tw', 'ja', 'en', 'es', 'ru'], not fill in the default locale code again
|
||||||
font: 'sans', // sans, serif, choose the font style for posts
|
font: 'sans', // sans, serif, choose the font style for posts
|
||||||
rss: true, // true, false, whether to enable RSS
|
|
||||||
toc: true, // true, false, whether to enable table of contents in posts
|
toc: true, // true, false, whether to enable table of contents in posts
|
||||||
},
|
},
|
||||||
// GLOBAL SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
|
// GLOBAL SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
---
|
---
|
||||||
|
import { themeConfig } from '@/config'
|
||||||
import Layout from '@/layouts/Layout.astro'
|
import Layout from '@/layouts/Layout.astro'
|
||||||
|
|
||||||
|
export function getStaticPaths() {
|
||||||
|
return themeConfig.global.moreLocale.map(lang => ({
|
||||||
|
params: { lang },
|
||||||
|
}))
|
||||||
|
}
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout>
|
<Layout>
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
---
|
---
|
||||||
|
import { themeConfig } from '@/config'
|
||||||
import Layout from '@/layouts/Layout.astro'
|
import Layout from '@/layouts/Layout.astro'
|
||||||
import { getPinnedPosts, getPosts } from '@/utils/content.config'
|
import { getPinnedPosts, getPosts } from '@/utils/content.config'
|
||||||
|
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return [
|
return themeConfig.global.moreLocale.map(lang => ({
|
||||||
{ params: { lang: 'zh' } },
|
params: { lang },
|
||||||
{ params: { lang: 'en' } },
|
}))
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { lang } = Astro.params
|
const { lang } = Astro.params
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
---
|
---
|
||||||
|
import { themeConfig } from '@/config'
|
||||||
import Layout from '@/layouts/Layout.astro'
|
import Layout from '@/layouts/Layout.astro'
|
||||||
import { getCollection } from 'astro:content'
|
import { getCollection } from 'astro:content'
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
const posts = await getCollection('posts')
|
const posts = await getCollection('posts')
|
||||||
return posts.map(post => ({
|
return themeConfig.global.moreLocale.flatMap(lang =>
|
||||||
params: { slug: post.slug },
|
posts.map(post => ({
|
||||||
props: { post },
|
params: { lang, slug: post.slug },
|
||||||
}))
|
props: { post },
|
||||||
|
})),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { post } = Astro.props
|
const { post } = Astro.props
|
||||||
|
|
86
src/pages/[lang]/rss.xml.ts
Normal file
86
src/pages/[lang]/rss.xml.ts
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
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'
|
||||||
|
|
||||||
|
const parser = new MarkdownIt()
|
||||||
|
const { title, description, url } = themeConfig.site
|
||||||
|
const { locale: defaultLocale, moreLocale } = themeConfig.global
|
||||||
|
const followConfig = themeConfig.seo?.follow
|
||||||
|
|
||||||
|
// Extract first 100 chars from content as description
|
||||||
|
function getExcerpt(content: string): string {
|
||||||
|
const plainText = sanitizeHtml(
|
||||||
|
parser.render(content),
|
||||||
|
{
|
||||||
|
allowedTags: [],
|
||||||
|
allowedAttributes: {},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return `${plainText.slice(0, 100).trim()}...`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return 404 response for invalid language routes
|
||||||
|
function return404() {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 404,
|
||||||
|
statusText: 'Not found',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add getStaticPaths for dynamic routes
|
||||||
|
export function getStaticPaths() {
|
||||||
|
return moreLocale.map(lang => ({ params: { lang } }))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate RSS feed for non-default languages
|
||||||
|
export async function GET({ params }: APIContext) {
|
||||||
|
const lang = params.lang as string
|
||||||
|
|
||||||
|
// Only generate RSS for valid non-default languages
|
||||||
|
if (!moreLocale.includes(lang)) {
|
||||||
|
return return404()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get posts for specific language (include 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,
|
||||||
|
stylesheet: '/rss/styles.xsl',
|
||||||
|
// Map posts to RSS items with language-specific URLs
|
||||||
|
items: posts.map(post => ({
|
||||||
|
title: post.data.title,
|
||||||
|
pubDate: post.data.published,
|
||||||
|
description: post.data.description || getExcerpt(post.body),
|
||||||
|
link: `/${lang}/posts/${post.slug}/`,
|
||||||
|
content: sanitizeHtml(
|
||||||
|
parser.render(post.body),
|
||||||
|
{
|
||||||
|
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
})),
|
||||||
|
// Add language and follow challenge info
|
||||||
|
customData: `
|
||||||
|
<language>${lang}</language>
|
||||||
|
${followConfig?.feedID && followConfig?.userID
|
||||||
|
? `<follow_challenge>
|
||||||
|
<feedId>${followConfig.feedID}</feedId>
|
||||||
|
<userId>${followConfig.userID}</userId>
|
||||||
|
</follow_challenge>`
|
||||||
|
: ''
|
||||||
|
}
|
||||||
|
`.trim(),
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,15 +1,19 @@
|
||||||
---
|
---
|
||||||
|
import { themeConfig } from '@/config'
|
||||||
import Layout from '@/layouts/Layout.astro'
|
import Layout from '@/layouts/Layout.astro'
|
||||||
import { getAllTags, getPostsByTag } from '@/utils/content.config'
|
import { getAllTags, getPostsByTag } from '@/utils/content.config'
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
const tags = await getAllTags()
|
const tags = await getAllTags()
|
||||||
return tags.map(tag => ({
|
return themeConfig.global.moreLocale.flatMap(lang =>
|
||||||
params: { tags: tag },
|
tags.map(tag => ({
|
||||||
props: { tags: tag },
|
params: { lang, tags: tag },
|
||||||
}))
|
props: { tags: tag },
|
||||||
|
})),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { lang } = Astro.params
|
||||||
const { tags } = Astro.props
|
const { tags } = Astro.props
|
||||||
const posts = await getPostsByTag(tags)
|
const posts = await getPostsByTag(tags)
|
||||||
const allTags = await getAllTags()
|
const allTags = await getAllTags()
|
||||||
|
@ -18,7 +22,7 @@ const allTags = await getAllTags()
|
||||||
<Layout>
|
<Layout>
|
||||||
<div>
|
<div>
|
||||||
{allTags.map(tag => (
|
{allTags.map(tag => (
|
||||||
<a href={`/tags/${tag}/`}>
|
<a href={`/${lang}/tags/${tag}/`}>
|
||||||
{tag}
|
{tag}
|
||||||
</a>
|
</a>
|
||||||
))}
|
))}
|
||||||
|
@ -28,7 +32,7 @@ const allTags = await getAllTags()
|
||||||
<ul>
|
<ul>
|
||||||
{posts.map(post => (
|
{posts.map(post => (
|
||||||
<li>
|
<li>
|
||||||
<a href={`/posts/${post.slug}/`}>{post.data.title}</a>
|
<a href={`/${lang}/posts/${post.slug}/`}>{post.data.title}</a>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -1,14 +1,22 @@
|
||||||
---
|
---
|
||||||
|
import { themeConfig } from '@/config'
|
||||||
import Layout from '@/layouts/Layout.astro'
|
import Layout from '@/layouts/Layout.astro'
|
||||||
import { getAllTags } from '@/utils/content.config'
|
import { getAllTags } from '@/utils/content.config'
|
||||||
|
|
||||||
|
export function getStaticPaths() {
|
||||||
|
return themeConfig.global.moreLocale.map(lang => ({
|
||||||
|
params: { lang },
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
const { lang } = Astro.params
|
||||||
const allTags = await getAllTags()
|
const allTags = await getAllTags()
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout>
|
<Layout>
|
||||||
<div>
|
<div>
|
||||||
{allTags.map(tag => (
|
{allTags.map(tag => (
|
||||||
<a href={`/tags/${tag}/`}>
|
<a href={`/${lang}/tags/${tag}/`}>
|
||||||
{tag}
|
{tag}
|
||||||
</a>
|
</a>
|
||||||
))}
|
))}
|
||||||
|
|
|
@ -24,11 +24,13 @@ function getExcerpt(content: string): string {
|
||||||
return `${plainText.slice(0, 100).trim()}...`
|
return `${plainText.slice(0, 100).trim()}...`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate RSS feed
|
// Generate RSS feed for default language
|
||||||
export async function GET(_context: APIContext) {
|
export async function GET() {
|
||||||
|
// Only handle posts for default language
|
||||||
const posts = await getCollection(
|
const posts = await getCollection(
|
||||||
'posts',
|
'posts',
|
||||||
({ data }: { data: CollectionEntry<'posts'>['data'] }) => !data.draft,
|
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
|
||||||
|
(!data.draft && (data.lang === locale || data.lang === '')),
|
||||||
)
|
)
|
||||||
|
|
||||||
return rss({
|
return rss({
|
||||||
|
@ -36,6 +38,7 @@ export async function GET(_context: APIContext) {
|
||||||
description,
|
description,
|
||||||
site: url,
|
site: url,
|
||||||
stylesheet: '/rss/styles.xsl',
|
stylesheet: '/rss/styles.xsl',
|
||||||
|
// Map posts to RSS items
|
||||||
items: posts.map((post: CollectionEntry<'posts'>) => ({
|
items: posts.map((post: CollectionEntry<'posts'>) => ({
|
||||||
title: post.data.title,
|
title: post.data.title,
|
||||||
pubDate: post.data.published,
|
pubDate: post.data.published,
|
||||||
|
@ -48,13 +51,14 @@ export async function GET(_context: APIContext) {
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
})),
|
})),
|
||||||
|
// Add language and follow challenge info
|
||||||
customData: `
|
customData: `
|
||||||
<language>${locale}</language>
|
<language>${locale}</language>
|
||||||
${followConfig?.feedID && followConfig?.userID
|
${followConfig?.feedID && followConfig?.userID
|
||||||
? `<follow_challenge>
|
? `<follow_challenge>
|
||||||
<feedId>${followConfig.feedID}</feedId>
|
<feedId>${followConfig.feedID}</feedId>
|
||||||
<userId>${followConfig.userID}</userId>
|
<userId>${followConfig.userID}</userId>
|
||||||
</follow_challenge>`
|
</follow_challenge>`
|
||||||
: ''
|
: ''
|
||||||
}
|
}
|
||||||
`.trim(),
|
`.trim(),
|
||||||
|
|
1
src/types/index.d.ts
vendored
1
src/types/index.d.ts
vendored
|
@ -29,7 +29,6 @@ export interface ThemeConfig {
|
||||||
locale: typeof langPath[number]
|
locale: typeof langPath[number]
|
||||||
moreLocale: typeof langPath[number][]
|
moreLocale: typeof langPath[number][]
|
||||||
font: string
|
font: string
|
||||||
rss: boolean
|
|
||||||
toc: boolean
|
toc: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,25 @@
|
||||||
import type { CollectionEntry } from 'astro:content'
|
import type { CollectionEntry } from 'astro:content'
|
||||||
|
import themeConfig from '@/config'
|
||||||
|
import { langPath } from '@/utils/ui'
|
||||||
import { getCollection } from 'astro:content'
|
import { getCollection } from 'astro:content'
|
||||||
|
|
||||||
export type Post = CollectionEntry<'posts'>
|
export type Post = CollectionEntry<'posts'>
|
||||||
export type PostData = Post['data']
|
export type PostData = Post['data']
|
||||||
export type PostsGroupByYear = Map<number, Post[]>
|
export type PostsGroupByYear = Map<number, Post[]>
|
||||||
|
|
||||||
// Get all posts and sort by publish date
|
// Get all posts except drafts (include pinned)
|
||||||
export async function getPosts() {
|
export async function getPosts(lang?: string) {
|
||||||
|
const defaultLocale = themeConfig.global.locale
|
||||||
|
const currentLang = lang || defaultLocale
|
||||||
|
|
||||||
const posts = await getCollection(
|
const posts = await getCollection(
|
||||||
'posts',
|
'posts',
|
||||||
({ data }: Post) => {
|
({ data }: Post) => {
|
||||||
const shouldInclude = (import.meta.env.DEV || !data.draft) && !data.pin
|
const shouldInclude = import.meta.env.DEV || !data.draft
|
||||||
return shouldInclude
|
if (!langPath.includes(currentLang)) {
|
||||||
|
return shouldInclude && data.lang === ''
|
||||||
|
}
|
||||||
|
return shouldInclude && (data.lang === currentLang || data.lang === '')
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,27 +28,23 @@ export async function getPosts() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get all posts except drafts (not pinned)
|
||||||
|
export async function getRegularPosts(lang?: string) {
|
||||||
|
const posts = await getPosts(lang)
|
||||||
|
return posts.filter(post => !post.data.pin)
|
||||||
|
}
|
||||||
|
|
||||||
// Get pinned posts
|
// Get pinned posts
|
||||||
export async function getPinnedPosts() {
|
export async function getPinnedPosts(lang?: string) {
|
||||||
const posts = await getCollection(
|
const posts = await getPosts(lang)
|
||||||
'posts',
|
return posts.filter(post => post.data.pin)
|
||||||
({ data }: Post) => {
|
|
||||||
const shouldInclude = (import.meta.env.DEV || !data.draft) && data.pin
|
|
||||||
return shouldInclude
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
return posts.sort((a: Post, b: Post) =>
|
|
||||||
b.data.published.valueOf() - a.data.published.valueOf(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group posts by year and sort
|
// Get posts group by year (not pinned)
|
||||||
export async function getPostsByYear(): Promise<PostsGroupByYear> {
|
export async function getPostsByYear(lang?: string): Promise<PostsGroupByYear> {
|
||||||
const posts = await getPosts()
|
const posts = await getRegularPosts(lang)
|
||||||
const yearMap = new Map<number, Post[]>()
|
const yearMap = new Map<number, Post[]>()
|
||||||
|
|
||||||
// Group by year
|
|
||||||
posts.forEach((post: Post) => {
|
posts.forEach((post: Post) => {
|
||||||
const year = post.data.published.getFullYear()
|
const year = post.data.published.getFullYear()
|
||||||
if (!yearMap.has(year)) {
|
if (!yearMap.has(year)) {
|
||||||
|
@ -49,7 +53,6 @@ export async function getPostsByYear(): Promise<PostsGroupByYear> {
|
||||||
yearMap.get(year)?.push(post)
|
yearMap.get(year)?.push(post)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Sort posts within each year
|
|
||||||
yearMap.forEach((yearPosts) => {
|
yearMap.forEach((yearPosts) => {
|
||||||
yearPosts.sort((a: Post, b: Post) => {
|
yearPosts.sort((a: Post, b: Post) => {
|
||||||
const aDate = a.data.published
|
const aDate = a.data.published
|
||||||
|
@ -66,9 +69,23 @@ export async function getPostsByYear(): Promise<PostsGroupByYear> {
|
||||||
return new Map([...yearMap.entries()].sort((a, b) => b[0] - a[0]))
|
return new Map([...yearMap.entries()].sort((a, b) => b[0] - a[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group posts by tags
|
// Get all tags
|
||||||
export async function sortPostsByTags() {
|
export async function getAllTags(lang?: string) {
|
||||||
const posts = await getPosts()
|
const posts = await getPosts(lang)
|
||||||
|
const tags = new Set<string>()
|
||||||
|
|
||||||
|
posts.forEach((post: Post) => {
|
||||||
|
post.data.tags?.forEach((tag: string) =>
|
||||||
|
tags.add(tag),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
return Array.from(tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get posts group by each tag
|
||||||
|
export async function getPostsGroupByTags(lang?: string) {
|
||||||
|
const posts = await getRegularPosts(lang)
|
||||||
const tagMap = new Map<string, Post[]>()
|
const tagMap = new Map<string, Post[]>()
|
||||||
|
|
||||||
posts.forEach((post: Post) => {
|
posts.forEach((post: Post) => {
|
||||||
|
@ -85,28 +102,10 @@ export async function sortPostsByTags() {
|
||||||
return tagMap
|
return tagMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get posts by specific tag
|
// Get all posts by one tag
|
||||||
export async function getPostsByTag(tag: string) {
|
export async function getPostsByTag(tag: string, lang?: string) {
|
||||||
const posts = await getPosts()
|
const posts = await getRegularPosts(lang)
|
||||||
|
|
||||||
return posts.filter((post: Post) =>
|
return posts.filter((post: Post) =>
|
||||||
post.data.tags?.includes(tag),
|
post.data.tags?.includes(tag),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all tags list
|
|
||||||
export async function getAllTags() {
|
|
||||||
const posts = await getCollection(
|
|
||||||
'posts',
|
|
||||||
({ data }: Post) => import.meta.env.DEV || !data.draft,
|
|
||||||
)
|
|
||||||
const tags = new Set<string>()
|
|
||||||
|
|
||||||
posts.forEach((post: Post) => {
|
|
||||||
post.data.tags?.forEach((tag: string) =>
|
|
||||||
tags.add(tag),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
return Array.from(tags)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue