refactor: replace art template with jsx (#20777)

* refactor: replace art template with jsx

* fix: tsx files

* fix: renderToString import

* fix: hono html raw import

* feat: remove art
This commit is contained in:
DIYgod
2025-12-29 12:27:32 +08:00
committed by GitHub
parent 2cef8cf1cc
commit cf4e772de8
1441 changed files with 17689 additions and 14825 deletions
@@ -1,16 +1,34 @@
import path from 'node:path';
import type { Cheerio, CheerioAPI } from 'cheerio';
import { load } from 'cheerio';
import type { Element } from 'domhandler';
import type { Context } from 'hono';
import { raw } from 'hono/html';
import { renderToString } from 'hono/jsx/dom/server';
import type { Data, DataItem, Route } from '@/types';
import { ViewType } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
type DescriptionImage = {
src?: string;
alt?: string;
};
const renderDescription = ({ images, description }: { images?: DescriptionImage[]; description?: string }) =>
renderToString(
<>
{images?.map((image) =>
image?.src ? (
<figure>
<img src={image.src} alt={image.alt ?? undefined} />
</figure>
) : null
)}
{description ? <>{raw(description)}</> : null}
</>
);
export const handler = async (ctx: Context): Promise<Data> => {
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '50', 10);
@@ -72,7 +90,7 @@ export const handler = async (ctx: Context): Promise<Data> => {
const title: string = $$('article h1').text();
const image: string | undefined = $$('meta[property="og:image"]').attr('content') ?? item.image;
const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), {
const description: string | undefined = renderDescription({
images: image
? [
{
@@ -81,7 +99,7 @@ export const handler = async (ctx: Context): Promise<Data> => {
},
]
: undefined,
description: $$('div.prose').html(),
description: $$('div.prose').html() ?? undefined,
});
const pubDateStr: string | undefined = $$('meta[property="article:published_time"]').attr('content');
const authorEls: Element[] = $$('meta[property="article:author"]').toArray();
@@ -1,17 +0,0 @@
{{ if images }}
{{ each images image }}
{{ if image?.src }}
<figure>
<img
{{ if image.alt }}
alt="{{ image.alt }}"
{{ /if }}
src="{{ image.src }}">
</figure>
{{ /if }}
{{ /each }}
{{ /if }}
{{ if description }}
{{@ description }}
{{ /if }}