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:
@@ -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 }}
|
||||
Reference in New Issue
Block a user