chore: improve item.extra types

This commit is contained in:
Ou 2024-10-27 22:09:17 +08:00
parent 76c282f302
commit 655204a14f
10 changed files with 37 additions and 31 deletions

19
pnpm-lock.yaml generated
View File

@ -209,7 +209,7 @@ importers:
version: 0.20.5(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0))(workbox-build@7.1.1(@types/babel__core@7.20.5))(workbox-window@7.1.0) version: 0.20.5(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0))(workbox-build@7.1.1(@types/babel__core@7.20.5))(workbox-window@7.1.0)
vite-plugin-with-nitro: vite-plugin-with-nitro:
specifier: 0.0.3 specifier: 0.0.3
version: 0.0.3(better-sqlite3@11.5.0)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)) version: 0.0.3(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0))
vite-tsconfig-paths: vite-tsconfig-paths:
specifier: ^5.0.1 specifier: ^5.0.1
version: 5.0.1(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)) version: 5.0.1(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0))
@ -3751,6 +3751,9 @@ packages:
resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
idb-keyval@6.2.1:
resolution: {integrity: sha512-8Sb3veuYCyrZL+VBt9LJfZjLUPWVvqn8tG28VqYNFCo43KHcKuq+b4EiXGeuaLAQWL2YmyDgMp2aSpH9JHsEQg==}
idb@7.1.1: idb@7.1.1:
resolution: {integrity: sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==} resolution: {integrity: sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==}
@ -9870,6 +9873,9 @@ snapshots:
dependencies: dependencies:
safer-buffer: 2.1.2 safer-buffer: 2.1.2
idb-keyval@6.2.1:
optional: true
idb@7.1.1: {} idb@7.1.1: {}
ieee754@1.2.1: {} ieee754@1.2.1: {}
@ -10461,7 +10467,7 @@ snapshots:
natural-compare@1.4.0: {} natural-compare@1.4.0: {}
nitro-go@0.0.1(better-sqlite3@11.5.0)(typescript@5.6.3): nitro-go@0.0.1(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3):
dependencies: dependencies:
'@cloudflare/kv-asset-handler': 0.3.4 '@cloudflare/kv-asset-handler': 0.3.4
'@netlify/functions': 2.8.2 '@netlify/functions': 2.8.2
@ -10529,7 +10535,7 @@ snapshots:
unctx: 2.3.1 unctx: 2.3.1
unenv: 1.10.0 unenv: 1.10.0
unimport: 3.13.1(rollup@4.24.0) unimport: 3.13.1(rollup@4.24.0)
unstorage: 1.12.0(ioredis@5.4.1) unstorage: 1.12.0(idb-keyval@6.2.1)(ioredis@5.4.1)
untyped: 1.5.1 untyped: 1.5.1
unwasm: 0.3.9 unwasm: 0.3.9
transitivePeerDependencies: transitivePeerDependencies:
@ -11814,7 +11820,7 @@ snapshots:
acorn: 8.13.0 acorn: 8.13.0
webpack-virtual-modules: 0.6.2 webpack-virtual-modules: 0.6.2
unstorage@1.12.0(ioredis@5.4.1): unstorage@1.12.0(idb-keyval@6.2.1)(ioredis@5.4.1):
dependencies: dependencies:
anymatch: 3.1.3 anymatch: 3.1.3
chokidar: 3.6.0 chokidar: 3.6.0
@ -11827,6 +11833,7 @@ snapshots:
ofetch: 1.4.1 ofetch: 1.4.1
ufo: 1.5.4 ufo: 1.5.4
optionalDependencies: optionalDependencies:
idb-keyval: 6.2.1
ioredis: 5.4.1 ioredis: 5.4.1
untun@0.1.3: untun@0.1.3:
@ -11915,12 +11922,12 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
vite-plugin-with-nitro@0.0.3(better-sqlite3@11.5.0)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)): vite-plugin-with-nitro@0.0.3(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)):
dependencies: dependencies:
consola: 3.2.3 consola: 3.2.3
defu: 6.1.4 defu: 6.1.4
h3: h3-nightly@1.13.1-20241013-011028-3bf8af0 h3: h3-nightly@1.13.1-20241013-011028-3bf8af0
nitropack: nitro-go@0.0.1(better-sqlite3@11.5.0)(typescript@5.6.3) nitropack: nitro-go@0.0.1(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3)
vite: 5.4.10(@types/node@22.7.9)(terser@5.36.0) vite: 5.4.10(@types/node@22.7.9)(terser@5.36.0)
transitivePeerDependencies: transitivePeerDependencies:
- '@azure/app-configuration' - '@azure/app-configuration'

View File

@ -13,14 +13,14 @@ const quick = defineSource(async () => {
const $a = $el.find("a.item-title") const $a = $el.find("a.item-title")
const url = $a.attr("href") const url = $a.attr("href")
const title = $a.text() const title = $a.text()
const relativeDate = $el.find(".time") const relativeDate = $el.find(".time").text()
if (url && title && relativeDate) { if (url && title && relativeDate) {
news.push({ news.push({
url: `${baseURL}${url}`, url: `${baseURL}${url}`,
title, title,
id: url, id: url,
extra: { extra: {
date: parseRelativeDate(relativeDate.text()), date: parseRelativeDate(relativeDate, "Asia/Shanghai").valueOf(),
}, },
}) })
} }

View File

@ -21,7 +21,7 @@ export default defineSource(async () => {
title, title,
id: url, id: url,
extra: { extra: {
date: parseRelativeDate(relatieveTime, "Asia/Shanghai"), date: parseRelativeDate(relatieveTime, "Asia/Shanghai").valueOf(),
info, info,
}, },
}) })

View File

@ -19,12 +19,10 @@ export default defineSource(async () => {
url, url,
title, title,
id: url, id: url,
extra: { pubDate: parseRelativeDate(date, "Asia/Shanghai").valueOf(),
date: tranformToUTC(date),
},
}) })
} }
} }
}) })
return news.sort((m, n) => n.extra!.date > m.extra!.date ? 1 : -1) return news.sort((m, n) => n.pubDate! > m.pubDate! ? 1 : -1)
}) })

View File

@ -33,7 +33,7 @@ export default defineSource(async () => {
.map((k) => { .map((k) => {
const keyword = k.word_scheme ? k.word_scheme : `#${k.word}#` const keyword = k.word_scheme ? k.word_scheme : `#${k.word}#`
return { return {
id: k.num, id: k.word,
title: k.word, title: k.word,
extra: { extra: {
icon: k.icon && { icon: k.icon && {

View File

@ -24,11 +24,9 @@ export default defineSource(async () => {
url: base + url, url: base + url,
title, title,
id: url, id: url,
extra: { pubDate: parseRelativeDate(date, "Asia/Shanghai").valueOf(),
date: tranformToUTC(date),
},
}) })
} }
}) })
return news.sort((m, n) => n.extra!.date > m.extra!.date ? 1 : -1) return news.sort((m, n) => n.pubDate! > m.pubDate! ? 1 : -1)
}) })

View File

@ -24,7 +24,6 @@ export default defineSource({
const url = "https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=20&desktop=true" const url = "https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=20&desktop=true"
const res: Res = await $fetch(url) const res: Res = await $fetch(url)
return res.data return res.data
.slice(0, 30)
.map((k) => { .map((k) => {
return { return {
id: k.target.id, id: k.target.id,

View File

@ -10,14 +10,12 @@ export function defineSource<T extends X>(source: T): T {
export function defineRSSSource(url: string, option?: SourceOption): SourceGetter { export function defineRSSSource(url: string, option?: SourceOption): SourceGetter {
return async () => { return async () => {
const data = await rss2json(url) const data = await rss2json(url)
if (!data?.items.length) throw new Error("Cannot fetch data") if (!data?.items.length) throw new Error("Cannot fetch rss data")
return data.items.map(item => ({ return data.items.map(item => ({
title: item.title, title: item.title,
url: item.link, url: item.link,
id: item.link, id: item.link,
extra: { pubDate: !option?.hiddenDate ? item.created : undefined,
date: !option?.hiddenDate && item.created,
},
})) }))
} }
} }
@ -40,9 +38,7 @@ export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption,
title: item.title, title: item.title,
url: item.url, url: item.url,
id: item.id ?? item.url, id: item.id ?? item.url,
extra: { pubDate: !sourceOption?.hiddenDate ? item.date_published : undefined,
date: !sourceOption?.hiddenDate && item.date_published,
},
})) }))
} }
} }

View File

@ -89,7 +89,15 @@ export interface NewsItem {
url: string url: string
mobileUrl?: string mobileUrl?: string
pubDate?: number | string pubDate?: number | string
extra?: Record<string, any> extra?: {
hover?: string
date?: number | string
info?: false | string
icon?: false | string | {
url: string
scale: number
}
}
} }
export interface SourceResponse { export interface SourceResponse {

View File

@ -167,10 +167,10 @@ function ExtraInfo({ item }: { item: NewsItem }) {
return <>{item.extra.info}</> return <>{item.extra.info}</>
} }
if (item?.extra?.icon) { if (item?.extra?.icon) {
const { url, scale } = item.extra.icon const { url, scale } = typeof item.extra.icon === "string" ? { url: item.extra.icon, scale: undefined } : item.extra.icon
return ( return (
<img <img
src={url ?? item.extra.icon} src={url}
style={{ style={{
transform: `scale(${scale ?? 1})`, transform: `scale(${scale ?? 1})`,
}} }}
@ -181,7 +181,7 @@ function ExtraInfo({ item }: { item: NewsItem }) {
} }
} }
function NewsUpdatedTime({ date }: { date: string }) { function NewsUpdatedTime({ date }: { date: string | number }) {
const relativeTime = useRelativeTime(date) const relativeTime = useRelativeTime(date)
return <>{relativeTime}</> return <>{relativeTime}</>
} }
@ -226,7 +226,7 @@ function NewsListTimeLine({ items }: { items: NewsItem[] }) {
<span className="flex items-center gap-1 text-neutral-400/50 ml--1px"> <span className="flex items-center gap-1 text-neutral-400/50 ml--1px">
<span className="">-</span> <span className="">-</span>
<span className="text-xs text-neutral-400/80"> <span className="text-xs text-neutral-400/80">
{(item.pubDate || item.extra?.date) && <NewsUpdatedTime date={item.pubDate || item?.extra?.date} />} {(item.pubDate || item?.extra?.date) && <NewsUpdatedTime date={(item.pubDate || item?.extra?.date)!} />}
</span> </span>
<span className="text-xs text-neutral-400/80"> <span className="text-xs text-neutral-400/80">
<ExtraInfo item={item} /> <ExtraInfo item={item} />