forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemes.ts
More file actions
222 lines (197 loc) · 6.91 KB
/
Copy paththemes.ts
File metadata and controls
222 lines (197 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import type { Context } from 'hono'
import type { Env } from './types'
import { createDesignMdTheme } from '@md/shared/design-md'
const MAX_THEME_BYTES = 256 * 1024
const FETCH_TIMEOUT_MS = 8000
const CURATED_THEMES = [
{
id: `design:claude`,
name: `Claude inspired`,
description: `Warm cream canvas, coral accent, editorial typography.`,
url: `https://raw.githubusercontent.com/VoltAgent/awesome-design-md/main/design-md/claude/DESIGN.md`,
},
{
id: `design:linear`,
name: `Linear inspired`,
description: `Precise, minimal product surfaces with restrained accent color.`,
url: `https://raw.githubusercontent.com/VoltAgent/awesome-design-md/main/design-md/linear.app/DESIGN.md`,
},
{
id: `design:vercel`,
name: `Vercel inspired`,
description: `Black and white precision with developer-product restraint.`,
url: `https://raw.githubusercontent.com/VoltAgent/awesome-design-md/main/design-md/vercel/DESIGN.md`,
},
{
id: `design:stripe`,
name: `Stripe inspired`,
description: `Elegant fintech color, gradients, and polished documentation rhythm.`,
url: `https://raw.githubusercontent.com/VoltAgent/awesome-design-md/main/design-md/stripe/DESIGN.md`,
},
{
id: `design:apple`,
name: `Apple inspired`,
description: `Premium whitespace, quiet typography, and product-focused surfaces.`,
url: `https://raw.githubusercontent.com/VoltAgent/awesome-design-md/main/design-md/apple/DESIGN.md`,
},
{
id: `design:figma`,
name: `Figma inspired`,
description: `Playful creative tooling with crisp multi-color accent language.`,
url: `https://raw.githubusercontent.com/VoltAgent/awesome-design-md/main/design-md/figma/DESIGN.md`,
},
{
id: `design:notion`,
name: `Notion inspired`,
description: `Warm minimal workspace feel with soft surfaces and editorial hierarchy.`,
url: `https://raw.githubusercontent.com/VoltAgent/awesome-design-md/main/design-md/notion/DESIGN.md`,
},
] as const
function jsonError(c: Context, status: 400 | 404 | 502, message: string) {
return c.json({ error: message }, status)
}
function assertThemeUrl(value: unknown): string {
if (typeof value !== `string`)
throw new Error(`请输入 DESIGN.md 地址`)
const url = new URL(value)
if (![`http:`, `https:`].includes(url.protocol))
throw new Error(`只支持 http/https 地址`)
return url.toString()
}
async function readLimitedText(response: Response): Promise<string> {
const contentLength = response.headers.get(`content-length`)
if (contentLength && Number(contentLength) > MAX_THEME_BYTES)
throw new Error(`主题文件超过 256KB`)
const text = await response.text()
if (new TextEncoder().encode(text).byteLength > MAX_THEME_BYTES)
throw new Error(`主题文件超过 256KB`)
if (!text.trim())
throw new Error(`主题文件为空`)
return text
}
// 自托管服务器(国内)直连 raw.githubusercontent.com 经常超时(与 upload-github.ts 同样的问题)。
// 优先走 jsDelivr 镜像(国内有可达节点),再退到 gcore 节点,最后才回退直连。
function toFetchableUrls(url: string): string[] {
const match = url.match(/^https:\/\/raw\.githubusercontent\.com\/([^/]+)\/([^/]+)\/([^/]+)\/(.+)$/)
if (!match)
return [url]
const [, user, repo, branch, path] = match
const ghPath = `gh/${user}/${repo}@${branch}/${path}`
return [
`https://cdn.jsdelivr.net/${ghPath}`,
`https://gcore.jsdelivr.net/${ghPath}`,
url,
]
}
async function fetchMarkdownOnce(url: string): Promise<string> {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
try {
const response = await fetch(url, {
signal: controller.signal,
headers: {
'accept': `text/markdown,text/plain,*/*`,
'user-agent': `mdlook-theme-importer`,
},
})
if (!response.ok)
throw new Error(`拉取失败:${response.status}`)
const contentType = response.headers.get(`content-type`) ?? ``
if (contentType && !/text|markdown|plain|octet-stream|json/i.test(contentType))
throw new Error(`远程文件不是 Markdown 文本`)
return await readLimitedText(response)
}
finally {
clearTimeout(timer)
}
}
async function fetchMarkdown(url: string): Promise<string> {
let lastError: unknown
for (const candidate of toFetchableUrls(url)) {
try {
return await fetchMarkdownOnce(candidate)
}
catch (error) {
lastError = error
}
}
throw lastError instanceof Error ? lastError : new Error(`主题拉取失败`)
}
function cacheKey(url: string): Request {
return new Request(`https://mdlook-theme-cache.local/?url=${encodeURIComponent(url)}`)
}
async function fetchMarkdownCached(url: string): Promise<string> {
const cache = typeof caches !== `undefined` ? caches.default : null
const key = cacheKey(url)
const cached = await cache?.match(key)
if (cached)
return cached.text()
const markdown = await fetchMarkdown(url)
await cache?.put(key, new Response(markdown, {
headers: {
'content-type': `text/markdown; charset=utf-8`,
'cache-control': `public, max-age=86400`,
},
}))
return markdown
}
function toSummary(theme: typeof CURATED_THEMES[number]) {
return {
id: theme.id,
name: theme.name,
description: theme.description,
sourceUrl: theme.url,
}
}
export function listThemesHandler(c: Context<{ Bindings: Env }>) {
return c.json({
themes: CURATED_THEMES.map(toSummary),
})
}
export async function getThemeHandler(c: Context<{ Bindings: Env }>) {
const id = c.req.param(`id`)
const theme = CURATED_THEMES.find(item => item.id === id || item.id === `design:${id}`)
if (!theme)
return jsonError(c, 404, `主题不存在`)
try {
const markdown = await fetchMarkdownCached(theme.url)
return c.json({
theme: createDesignMdTheme({
id: theme.id,
name: theme.name,
description: theme.description,
sourceUrl: theme.url,
markdown,
}),
})
}
catch (error) {
return jsonError(c, 502, error instanceof Error ? error.message : `主题拉取失败`)
}
}
export async function importThemeHandler(c: Context<{ Bindings: Env }>) {
let url: string
try {
const body = await c.req.json<{ url?: string }>()
url = assertThemeUrl(body.url)
}
catch (error) {
return jsonError(c, 400, error instanceof Error ? error.message : `请求格式错误`)
}
try {
const markdown = await fetchMarkdownCached(url)
const pathname = new URL(url).pathname
const name = pathname.split(`/`).filter(Boolean).at(-2) ?? pathname.split(`/`).filter(Boolean).at(-1) ?? `Custom`
return c.json({
theme: createDesignMdTheme({
name: `${name} inspired`,
description: `Imported from ${url}`,
sourceUrl: url,
markdown,
}),
})
}
catch (error) {
return jsonError(c, 502, error instanceof Error ? error.message : `主题导入失败`)
}
}