Skip to content

Commit cf4e772

Browse files
authored
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
1 parent 2cef8cf commit cf4e772

File tree

1,827 files changed

+56591
-53914
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,827 files changed

+56591
-53914
lines changed

.github/labeler.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'Route':
77
- changed-files:
8-
- any-glob-to-any-file: ['lib/routes/**/*.ts']
8+
- any-glob-to-any-file: ['lib/routes/**/*.ts', 'lib/routes/**/*.tsx']
99

1010
core enhancement:
1111
- changed-files:

.github/workflows/build-assets.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
- master
88
paths:
99
- 'lib/**/*.ts'
10+
- 'lib/**/*.tsx'
1011

1112
jobs:
1213
build:

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
'if (process.env.BUILD_ROUTES_MODE) {
4848
modules = directoryImport({
4949
targetDirectoryPath: path.join(__dirname, "./routes"),
50-
importPattern: /\.ts$/,
50+
importPattern: /\.tsx?$/,
5151
}) as typeof modules;
5252
} else if (config.isPackage)'
5353
'';

lib/middleware/templates/iframe.art

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/registry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'node:path';
22

33
import { serveStatic } from '@hono/node-server/serve-static';
4-
import { directoryImport } from 'directory-import';
54
import type { Handler } from 'hono';
65
import { Hono } from 'hono';
76
import { routePath } from 'hono/route';
@@ -12,6 +11,7 @@ import index from '@/routes/index';
1211
import metrics from '@/routes/metrics';
1312
import robotstxt from '@/routes/robots.txt';
1413
import type { APIRoute, Namespace, Route } from '@/types';
14+
import { directoryImport } from '@/utils/directory-import';
1515
import logger from '@/utils/logger';
1616

1717
const __dirname = import.meta.dirname;
@@ -73,7 +73,7 @@ if (config.isPackage) {
7373
default:
7474
modules = directoryImport({
7575
targetDirectoryPath: path.join(__dirname, './routes'),
76-
importPattern: /\.ts$/,
76+
importPattern: /\.tsx?$/,
7777
}) as typeof modules;
7878
}
7979
}

lib/routes-deprecated/index.js

Lines changed: 123 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const config = require('@/config').value;
2-
const art = require('art-template');
3-
const path = require('path');
2+
const { raw } = require('hono/html');
3+
const { jsx } = require('hono/jsx');
4+
const { renderToString } = require('hono/jsx/dom/server');
45

56
let gitHash;
67
try {
@@ -45,56 +46,124 @@ module.exports = (ctx) => {
4546

4647
const duration = Date.now() - startTime;
4748

48-
ctx.body = art(path.resolve(__dirname, '../views/welcome.art'), {
49-
showDebug,
50-
disallowRobot,
51-
debug: [
52-
nodeName
53-
? {
54-
name: 'Node Name',
55-
value: nodeName,
56-
}
57-
: null,
58-
{
59-
name: 'Git Hash',
60-
value: gitHash,
61-
},
62-
{
63-
name: 'Request Amount',
64-
value: ctx.debug.request,
65-
},
66-
{
67-
name: 'Request Frequency',
68-
value: ((ctx.debug.request / (duration / 1000)) * 60).toFixed(3) + ' times/minute',
69-
},
70-
{
71-
name: 'Cache Hit Ratio',
72-
value: ctx.debug.request ? (ctx.debug.hitCache / ctx.debug.request).toFixed(3) : 0,
73-
},
74-
{
75-
name: 'ETag Matched',
76-
value: ctx.debug.etag,
77-
},
78-
{
79-
name: 'Run Time',
80-
value: (duration / 3_600_000).toFixed(2) + ' hour(s)',
81-
},
82-
{
83-
name: 'Hot Routes',
84-
value: hotRoutesValue,
85-
},
86-
{
87-
name: 'Hot Paths',
88-
value: hotPathsValue,
89-
},
90-
{
91-
name: 'Hot Error Routes',
92-
value: hotErrorRoutesValue,
93-
},
94-
{
95-
name: 'Hot Error Paths',
96-
value: hotErrorPathsValue,
97-
},
98-
],
99-
});
49+
const debugInfo = [
50+
nodeName
51+
? {
52+
name: 'Node Name',
53+
value: nodeName,
54+
}
55+
: null,
56+
{
57+
name: 'Git Hash',
58+
value: gitHash,
59+
},
60+
{
61+
name: 'Request Amount',
62+
value: ctx.debug.request,
63+
},
64+
{
65+
name: 'Request Frequency',
66+
value: ((ctx.debug.request / (duration / 1000)) * 60).toFixed(3) + ' times/minute',
67+
},
68+
{
69+
name: 'Cache Hit Ratio',
70+
value: ctx.debug.request ? (ctx.debug.hitCache / ctx.debug.request).toFixed(3) : 0,
71+
},
72+
{
73+
name: 'ETag Matched',
74+
value: ctx.debug.etag,
75+
},
76+
{
77+
name: 'Run Time',
78+
value: (duration / 3_600_000).toFixed(2) + ' hour(s)',
79+
},
80+
{
81+
name: 'Hot Routes',
82+
value: hotRoutesValue,
83+
},
84+
{
85+
name: 'Hot Paths',
86+
value: hotPathsValue,
87+
},
88+
{
89+
name: 'Hot Error Routes',
90+
value: hotErrorRoutesValue,
91+
},
92+
{
93+
name: 'Hot Error Paths',
94+
value: hotErrorPathsValue,
95+
},
96+
].filter(Boolean);
97+
98+
const formatDebugValue = (value) => (typeof value === 'string' && value.includes('<br>') ? raw(value) : value);
99+
100+
const debugItems = debugInfo.map((item) =>
101+
jsx(
102+
'div',
103+
{ class: 'debug-item' },
104+
jsx('strong', null, `${item.name}: `),
105+
formatDebugValue(item.value)
106+
)
107+
);
108+
109+
const html = renderToString(
110+
jsx(
111+
'html',
112+
{ lang: 'en' },
113+
jsx(
114+
'head',
115+
null,
116+
jsx('meta', { charset: 'utf-8' }),
117+
jsx('meta', { name: 'viewport', content: 'width=device-width, initial-scale=1' }),
118+
jsx('title', null, 'RSSHub'),
119+
disallowRobot ? jsx('meta', { name: 'robots', content: 'noindex, nofollow' }) : null,
120+
jsx(
121+
'style',
122+
null,
123+
`
124+
body {
125+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
126+
margin: 24px;
127+
color: #111827;
128+
background: #ffffff;
129+
}
130+
h1 {
131+
font-size: 24px;
132+
margin-bottom: 8px;
133+
}
134+
.debug {
135+
margin-top: 24px;
136+
padding: 16px;
137+
border: 1px solid #e5e7eb;
138+
border-radius: 8px;
139+
background: #f9fafb;
140+
}
141+
.debug-item {
142+
margin: 6px 0;
143+
}
144+
.debug-item strong {
145+
display: inline-block;
146+
min-width: 160px;
147+
}
148+
`
149+
)
150+
),
151+
jsx(
152+
'body',
153+
null,
154+
jsx('h1', null, 'RSSHub'),
155+
jsx('p', null, 'RSSHub is running.'),
156+
showDebug
157+
? jsx(
158+
'section',
159+
{ class: 'debug' },
160+
jsx('h2', null, 'Debug Info'),
161+
debugItems.length ? debugItems : jsx('p', null, 'No debug data.')
162+
)
163+
: null
164+
)
165+
)
166+
);
167+
168+
ctx.body = `<!DOCTYPE html>${html}`;
100169
};

lib/routes-deprecated/mofish/index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
const got = require('@/utils/got');
22
const { parseDate } = require('@/utils/parse-date');
3-
const { art } = require('@/utils/render');
4-
const path = require('path');
3+
const { renderToString } = require('hono/jsx/dom/server');
4+
const { jsx } = require('hono/jsx');
5+
6+
const renderImageDescription = (imageUrl) =>
7+
renderToString(
8+
jsx(
9+
'p',
10+
null,
11+
jsx('img', {
12+
src: imageUrl,
13+
referrerpolicy: 'no-referrer',
14+
}),
15+
jsx('br', null)
16+
)
17+
);
518

619
module.exports = async (ctx) => {
720
const id = ctx.params.id;
@@ -23,11 +36,7 @@ module.exports = async (ctx) => {
2336
link: 'https://mo.fish/',
2437
item: data.map((item) => {
2538
const isImage = Number(id) === 136 && item.Url.endsWith('.gif');
26-
const description = isImage
27-
? art(path.join(__dirname, 'templates/description.art'), {
28-
imageUrl: item.Url,
29-
})
30-
: title;
39+
const description = isImage ? renderImageDescription(item.Url) : title;
3140

3241
return {
3342
title: item.Title,

lib/routes-deprecated/mofish/templates/description.art

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)