fix: 修改rss生成逻辑,修改目录生成逻辑

This commit is contained in:
KazooTTT
2024-11-30 16:43:18 +08:00
parent 09cd161c84
commit 89911d9e63
8 changed files with 5008 additions and 6165 deletions

View File

@ -54,6 +54,7 @@
"devDependencies": {
"@tailwindcss/aspect-ratio": "^0.4.2",
"@tailwindcss/typography": "^0.5.10",
"@types/markdown-it": "^14.1.2",
"@types/sanitize-html": "^2.13.0",
"@typescript-eslint/parser": "^7.1.1",
"dotenv": "^16.4.5",

10839
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,16 @@ const path = require('path')
const contentDir = path.join(__dirname, '../src/content/post')
const USE_FULL_PATH = true
function toCamelCase(str) {
return str
.split(' ')
.map((word, index) => {
if (index === 0) return word.toLowerCase()
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
})
.join('')
}
function processDirectory(dir) {
fs.readdirSync(dir).forEach((item) => {
const itemPath = path.join(dir, item)
@ -25,11 +35,13 @@ function getCategoryFromPath(filePath) {
if (pathParts.length > 1) {
if (USE_FULL_PATH) {
// Join all directory parts except the filename
return pathParts.slice(0, -1).join('-')
// Join all directory parts except the filename and convert to camelCase if contains spaces
const category = pathParts.slice(0, -1).join('-')
return category.includes(' ') ? toCamelCase(category) : category
} else {
// Just use the first directory after "post"
return pathParts[0]
// Just use the first directory after "post" and convert to camelCase if contains spaces
const category = pathParts[0]
return category.includes(' ') ? toCamelCase(category) : category
}
}

View File

@ -25,7 +25,7 @@ NotionID-notionnext: 80f19b4c-d207-45a0-bbbb-39641a9dc330
link-notionnext: >-
https://kazoottt.notion.site/Possible-Causes-and-Solutions-for-Focusee-Switching-System-Audio-to-Speaker-Playback-forcibly-80f19b4cd20745a0bbbb39641a9dc330
rinId: 39
category: english writing
category: englishWriting
---
# Possible Causes and Solutions for Focusee Switching System Audio to Speaker Playback Forcibly

View File

@ -12,7 +12,7 @@ tags:
- Refund
finished: true
published: true
category: english writing
category: englishWriting
slug: focusee-macos-review-en
description: An analysis of Focusee's shortcomings on macOS, including severe color discrepancies, slow export speed, and sound card configuration conflicts, along with the author's refund experience.
NotionID-notionnext: c692f30c-bcbc-48fd-9739-19e23a3e1e40

View File

@ -18,7 +18,7 @@ description: >-
NotionID-notionnext: 96e4d436-6fd9-4fec-865c-ac2d80b06be0
link-notionnext: 'https://kazoottt.notion.site/open-graph-intro-96e4d4366fd94fec865cac2d80b06be0'
rinId: 17
category: english writing
category: englishWriting
---
``

View File

@ -2,27 +2,12 @@ import { siteConfig } from '@/site-config'
import rss from '@astrojs/rss'
import type { APIContext } from 'astro'
import sanitizeHtml from 'sanitize-html'
import MarkdownIt from 'markdown-it';
import { getSortedAllPostsAndDiaries } from 'src/utils/post';
// Define an interface for your markdown files
interface MarkdownPost {
frontmatter: {
title?: string
date: string
description?: string
category?: string
slug: string
author?: string
}
compiledContent?: () => string
}
const parser = new MarkdownIt();
export async function GET(context: APIContext) {
const postImportResult = import.meta.glob('../content/post/**/*.md', { eager: true })
const posts = Object.values(postImportResult) as MarkdownPost[]
const sortedPosts = posts.sort(
(a, b) => new Date(b.frontmatter.date).valueOf() - new Date(a.frontmatter.date).valueOf()
)
const blog = await getSortedAllPostsAndDiaries()
return rss({
title: siteConfig.title,
description: siteConfig.description,
@ -31,22 +16,20 @@ export async function GET(context: APIContext) {
<feedId>75113012474671104</feedId>
<userId>62156866798228480</userId>
</follow_challenge>`,
items: sortedPosts.map((post) => {
const prefix = post.frontmatter.category?.startsWith('日记-20') ? '/diary/' : '/blog/'
items: blog.map((post) => {
const prefix = post?.data.category?.startsWith('日记-20') ? '/diary/' : '/blog/'
return {
title: post.frontmatter.title?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') ?? '',
pubDate: new Date(post.frontmatter.date),
description: post.frontmatter.description?.replace(/[\x00-\x1F\x7F-\x9F]/g, '') ?? '',
link: `${prefix}${post.frontmatter.slug}`,
content: sanitizeHtml(post.compiledContent?.() ?? '', {
title: post.data.title,
pubDate: new Date(post.data.date),
description: post.data.description ?? '',
link: `${prefix}${post.slug}`,
content: sanitizeHtml(parser.render(post.body), {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
allowedAttributes: {
...sanitizeHtml.defaults.allowedAttributes,
img: ['src', 'alt']
},
textFilter: (text) => text.replace(/[\x00-\x1F\x7F-\x9F]/g, '')
textFilter: function (text) {
return text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFFF0-\uFFFF]/g, '')
}
}),
author: post.frontmatter.author?.replace(/[\x00-\x1F\x7F-\x9F]/g, '')
author: siteConfig.author
}
})
})

View File

@ -22,6 +22,14 @@ export const getAllDiariesSorted = async (): Promise<CollectionEntry<'post'>[]>
return sortMDByDate(await getAllDiaries())
}
export const getSortedAllPostsAndDiaries = async (): Promise<CollectionEntry<'post'>[]> => {
const posts = await getCollection('post', ({ data }: { data: CollectionEntry<'post'> }) => {
return !data.draft
})
return sortMDByDate(posts)
}
export function sortMDByDate(posts: Array<CollectionEntry<'post'>>): CollectionEntry<'post'>[] {
return posts.sort((a, b) => {
const aDate = new Date(a.data.date).valueOf()