fix: revert zfeed to feed, resolves #535

This commit is contained in:
YunYouJun 2025-07-12 18:36:15 +08:00
parent 6b116400f7
commit 89b1ce5c05
3 changed files with 47 additions and 30 deletions

View File

@ -1,13 +1,14 @@
import type { Author, Feed, Item } from 'zfeed'
import type { Author, FeedOptions, Item } from 'feed'
import type { ResolvedValaxyOptions } from '../../options'
import { readFile } from 'node:fs/promises'
import { dirname, join, resolve } from 'node:path'
import { ensurePrefix } from '@antfu/utils'
import { consola } from 'consola'
import { colors } from 'consola/utils'
import dayjs from 'dayjs'
import fg from 'fast-glob'
import { Feed } from 'feed'
import fs from 'fs-extra'
import matter from 'gray-matter'
@ -15,7 +16,6 @@ import MarkdownIt from 'markdown-it'
import ora from 'ora'
import { getBorderCharacters, table } from 'table'
import { createFeed, generateAtom1, generateJson1, generateRss2 } from 'zfeed'
import { matterOptions } from '../../plugins/markdown/transform/matter'
import { isExternal } from '../../utils'
import { getCreatedTime, getUpdatedTime } from '../../utils/date'
@ -52,21 +52,23 @@ export async function build(options: ResolvedValaxyOptions) {
}
const ccVersion = (siteConfig.license?.type === 'zero') ? '1.0' : '4.0'
const feedNameMap = {
const feedNameMap: Record<string, string> = {
atom: siteConfig.feed?.name ? `${siteConfig.feed?.name}.atom` : 'atom.xml',
json: `${siteConfig.feed?.name || 'feed'}.json`,
rss: `${siteConfig.feed?.name || 'feed'}.xml`,
}
const feedOptions: Feed = {
const feedLinks: FeedOptions['feedLinks'] = {}
Object.keys(feedNameMap).forEach((key) => {
feedLinks[key] = `${siteUrl}${feedNameMap[key]}`
})
const feedOptions: FeedOptions = {
title: siteConfig.title || 'Valaxy Blog',
description: siteConfig.description,
id: siteUrl || 'valaxy',
link: siteUrl,
copyright: `CC ${siteConfig.license?.type?.toUpperCase()} ${ccVersion} ${new Date().getFullYear()} © ${siteConfig.author?.name}`,
feed: Object.fromEntries(
Object.entries(feedNameMap).map(([key, value]) => [key, `${siteUrl}${value}`]),
),
feedLinks,
}
const DOMAIN = siteConfig.url.slice(0, -1)
@ -171,28 +173,27 @@ export async function getPosts(params: {
posts.push({
title: data.title,
updatedAt: new Date(data.date),
publishedAt: new Date(data.updated || data.date),
...data,
date: new Date(data.date),
published: new Date(data.updated || data.date),
content: html + tip,
author,
author: [author],
id: data.id || link,
link,
})
}
// sort by updated
posts.sort((a, b) => +(b.publishedAt || b.updatedAt) - +(a.publishedAt || a.updatedAt))
posts.sort((a, b) => +(b.published || b.date) - +(a.published || a.date))
return posts
}
/**
* write feed to local
*/
export async function writeFeed(feedOptions: Feed, posts: Item[], options: ResolvedValaxyOptions, feedNameMap: Record<string, string>) {
const feed = createFeed({
...feedOptions,
items: posts,
})
export async function writeFeed(feedOptions: FeedOptions, posts: Item[], options: ResolvedValaxyOptions, feedNameMap: Record<string, string>) {
const feed = new Feed(feedOptions)
posts.forEach(item => feed.addItem(item))
await fs.ensureDir(dirname(`./dist/${feedNameMap.atom}`))
const path = resolve(options.userRoot, './dist')
@ -212,11 +213,11 @@ export async function writeFeed(feedOptions: Feed, posts: Item[], options: Resol
let data = ''
const distFeedPath = `${path}/${feedNameMap[type]}`
if (type === 'rss')
data = generateRss2(feed)
data = feed.rss2()
else if (type === 'atom')
data = generateAtom1(feed)
data = feed.atom1()
else if (type === 'json')
data = generateJson1(feed)
data = feed.json1()
await fs.writeFile(distFeedPath, data, 'utf-8')
consola.debug(`[${colors.cyan(type)}] dist: ${colors.dim(distFeedPath)}`)

View File

@ -87,6 +87,7 @@
"ejs": "^3.1.10",
"escape-html": "^1.0.3",
"fast-glob": "^3.3.3",
"feed": "^5.1.0",
"floating-vue": "^5.2.2",
"fs-extra": "^11.3.0",
"fuse.js": "^7.1.0",
@ -139,8 +140,7 @@
"vue": "catalog:frontend",
"vue-i18n": "catalog:build",
"vue-router": "^4.5.1",
"yargs": "^18.0.0",
"zfeed": "^0.2.3"
"yargs": "^18.0.0"
},
"devDependencies": {
"@mdit-vue/plugin-component": "^2.1.4",

View File

@ -494,6 +494,9 @@ importers:
fast-glob:
specifier: ^3.3.3
version: 3.3.3
feed:
specifier: ^5.1.0
version: 5.1.0
floating-vue:
specifier: ^5.2.2
version: 5.2.2(@nuxt/kit@3.17.6)(vue@3.5.17(typescript@5.8.3))
@ -653,9 +656,6 @@ importers:
yargs:
specifier: ^18.0.0
version: 18.0.0
zfeed:
specifier: ^0.2.3
version: 0.2.3
devDependencies:
'@mdit-vue/plugin-component':
specifier: ^2.1.4
@ -4282,6 +4282,10 @@ packages:
picomatch:
optional: true
feed@5.1.0:
resolution: {integrity: sha512-qGNhgYygnefSkAHHrNHqC7p3R8J0/xQDS/cYUud8er/qD9EFGWyCdUDfULHTJQN1d3H3WprzVwMc9MfB4J50Wg==}
engines: {node: '>=20', pnpm: '>=10'}
figures@6.1.0:
resolution: {integrity: sha512-d+l3qxjSesT4V7v2fh+QnmFnUWv9lSpjarhShNTgBOfA0ttejbQUAlHLitbjkoRiDulW0OPoQPYIGhIC8ohejg==}
engines: {node: '>=18'}
@ -6439,6 +6443,9 @@ packages:
engines: {node: '>=14.0.0'}
hasBin: true
sax@1.4.1:
resolution: {integrity: sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==}
saxes@6.0.0:
resolution: {integrity: sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==}
engines: {node: '>=v12.22.7'}
@ -7588,6 +7595,10 @@ packages:
utf-8-validate:
optional: true
xml-js@1.6.11:
resolution: {integrity: sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==}
hasBin: true
xml-name-validator@4.0.0:
resolution: {integrity: sha512-ICP2e+jsHvAj2E2lIHxa5tjXRlKDJo4IdvPvCXbXQGdzSfmSpNVyIKMvoZHjDY9DP0zV17iI85o90vRFXNccRw==}
engines: {node: '>=12'}
@ -7653,9 +7664,6 @@ packages:
resolution: {integrity: sha512-GQHQqAopRhwU8Kt1DDM8NjibDXHC8eoh1erhGAJPEyveY9qqVeXvVikNKrDz69sHowPMorbPUrH/mx8c50eiBQ==}
engines: {node: '>=18'}
zfeed@0.2.3:
resolution: {integrity: sha512-NapL+DN8rp0wKZTotRZFxEVSVlB9LmhC1RJLaupjlPJxYfyjlFKgKgHDpxTZLPBo+TdiMXc2kU5zYW6DKb0TgA==}
zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
@ -11664,6 +11672,10 @@ snapshots:
optionalDependencies:
picomatch: 4.0.2
feed@5.1.0:
dependencies:
xml-js: 1.6.11
figures@6.1.0:
dependencies:
is-unicode-supported: 2.1.0
@ -14047,6 +14059,8 @@ snapshots:
optionalDependencies:
'@parcel/watcher': 2.5.1
sax@1.4.1: {}
saxes@6.0.0:
dependencies:
xmlchars: 2.2.0
@ -15568,6 +15582,10 @@ snapshots:
ws@8.18.3: {}
xml-js@1.6.11:
dependencies:
sax: 1.4.1
xml-name-validator@4.0.0: {}
xml-name-validator@5.0.0: {}
@ -15635,8 +15653,6 @@ snapshots:
yoctocolors@2.1.1: {}
zfeed@0.2.3: {}
zwitch@2.0.4: {}
zx@8.7.0: {}