-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathPackage.takumi.vue
More file actions
434 lines (384 loc) · 13.7 KB
/
Package.takumi.vue
File metadata and controls
434 lines (384 loc) · 13.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<script lang="ts">
const compactFormat = new Intl.NumberFormat('en', { notation: 'compact', maximumFractionDigits: 1 })
const KIND_ICONS: Record<string, string> = {
function: 'i-lucide:parentheses',
class: 'i-lucide:box',
interface: 'i-lucide:puzzle',
typeAlias: 'i-lucide:type',
variable: 'i-lucide:variable',
enum: 'i-lucide:list',
namespace: 'i-lucide:package',
}
</script>
<script setup lang="ts">
import type { JsDelivrFileNode } from '#shared/types'
import { joinURL } from 'ufo'
import { useCharts } from '~/composables/useCharts'
import { createSmoothPath } from 'vue-data-ui/utils'
const REPO_PROVIDER_ICONS: Record<string, string> = {
github: 'i-simple-icons:github',
gitlab: 'i-simple-icons:gitlab',
bitbucket: 'i-simple-icons:bitbucket',
codeberg: 'i-simple-icons:codeberg',
gitea: 'i-simple-icons:gitea',
forgejo: 'i-simple-icons:forgejo',
gitee: 'i-simple-icons:gitee',
sourcehut: 'i-simple-icons:sourcehut',
tangled: 'i-custom:tangled',
radicle: 'i-lucide:network',
}
function sortJsDelivrNodes(nodes: JsDelivrFileNode[]) {
return [...nodes].sort((a, b) => {
if (a.type !== b.type) return a.type === 'directory' ? -1 : 1
return a.name.localeCompare(b.name)
})
}
const { name, version, variant } = defineProps<{
name: string
version: string | null
variant: 'download-chart' | 'code-tree' | 'function-tree'
}>()
const {
data: resolvedVersion,
status: versionStatus,
error: versionError,
} = await useResolvedVersion(name, version)
if (
versionStatus.value === 'error' &&
versionError.value?.statusCode &&
versionError.value.statusCode >= 400 &&
versionError.value.statusCode < 500
) {
throw createError({
statusCode: 404,
})
}
const { data: pkg, refresh: refreshPkg } = usePackage(name, () => resolvedVersion.value ?? version)
const displayVersion = computed(() => pkg.value?.requestedVersion ?? null)
const repositoryUrl = computed(() => {
const repo = displayVersion.value?.repository
if (!repo?.url) return null
let url = normalizeGitUrl(repo.url)
// append `repository.directory` for monorepo packages
if (repo.directory) {
url = joinURL(`${url}/tree/HEAD`, repo.directory)
}
return url
})
const { repoRef, stars, refresh: refreshRepoMeta } = useRepoMeta(repositoryUrl)
const repoProviderIcon = computed(() => {
const provider = repoRef.value?.provider
return provider ? (REPO_PROVIDER_ICONS[provider] ?? 'i-lucide:code') : 'i-lucide:code'
})
const formattedStars = computed(() => (stars.value > 0 ? compactFormat.format(stars.value) : ''))
const formattedDownloads = computed(() => {
const last = weeklyValues.value.at(-1)
return last ? compactFormat.format(last) : ''
})
const totalLikes = shallowRef(0)
const formattedLikes = computed(() =>
totalLikes.value ? compactFormat.format(totalLikes.value) : '',
)
const pkgNameParts = computed(() => {
const n = pkg.value?.name
if (!n?.startsWith('@')) return { org: null, short: n }
const slashIdx = n.indexOf('/')
return { org: n.slice(0, slashIdx), short: n.slice(slashIdx + 1) }
})
// Fetch 52 weeks of download evolution for sparkline
const { fetchPackageDownloadEvolution } = useCharts()
const weeklyValues = shallowRef<number[]>([])
async function fetchWeeklyEvolution() {
const evolution = await fetchPackageDownloadEvolution(name, null, {
granularity: 'week',
weeks: 52,
}).catch(() => null)
if (evolution?.length) {
weeklyValues.value = evolution.map(w => w.value)
}
}
// Flatten file tree into renderable rows for code-tree variant
interface TreeRow {
name: string
depth: number
isDir: boolean
}
const treeRows = shallowRef<TreeRow[]>([])
async function fetchCodeTree() {
const ver = resolvedVersion.value ?? version
if (!ver) return
// Call jsDelivr directly — $fetch to internal API can deadlock in OG image island context
const resp = await $fetch<{ files: JsDelivrFileNode[] }>(
`https://data.jsdelivr.com/v1/packages/npm/${name}@${ver}`,
{ timeout: 3000 },
).catch(() => null)
if (!resp?.files) return
const rows: TreeRow[] = []
const MAX_ROWS = 25
function walk(nodes: JsDelivrFileNode[], depth: number) {
for (const node of sortJsDelivrNodes(nodes)) {
if (rows.length >= MAX_ROWS) return
rows.push({ name: node.name, depth, isDir: node.type === 'directory' })
if (node.files) walk(node.files, depth + 1)
}
}
walk(resp.files, 0)
treeRows.value = rows
}
// Parse docs TOC HTML to extract API symbols for function-tree variant
interface SymbolRow {
name: string
kind: 'section' | 'symbol'
icon: string
}
const symbolRows = shallowRef<SymbolRow[]>([])
const requestFetch = useRequestFetch()
async function fetchFunctionTree() {
const ver = resolvedVersion.value ?? version
if (!ver) return
const resp = await requestFetch<{ toc: string | null }>(
`/api/registry/docs/${name}/v/${ver}`,
).catch(() => null)
if (!resp?.toc) return
const rows: SymbolRow[] = []
const MAX_ROWS = 25
// Parse TOC HTML: sections have href="#section-{kind}", symbols have class="font-mono"
const symbolRe = /<a href="#[^"]*"[^>]*font-mono[^>]*>([^<]+)<\/a>/g
// Split TOC by <li> sections to preserve grouping
const sectionBlocks = resp.toc.split(/<li>\s*<a href="#section-/)
for (const block of sectionBlocks) {
if (rows.length >= MAX_ROWS) break
// Extract section kind and title
const kindMatch = block.match(/^(\w+)"[^>]*>([^<]+)/)
if (!kindMatch) continue
const kind = kindMatch[1]!
const title = kindMatch[2]!.trim()
rows.push({ name: title, kind: 'section', icon: KIND_ICONS[kind] ?? 'i-lucide:code' })
// Extract symbol names within this section
const symbolMatches = block.matchAll(symbolRe)
for (const m of symbolMatches) {
if (rows.length >= MAX_ROWS) break
rows.push({ name: m[1]!, kind: 'symbol', icon: KIND_ICONS[kind] ?? 'i-lucide:code' })
}
}
symbolRows.value = rows
}
const fetchLikes = import.meta.test
? // need deterministic likes for testing
Promise.resolve().then(() => {
totalLikes.value = 83
})
: $fetch<{ totalLikes: number }>(`/api/social/likes/${name}`).then(d => {
totalLikes.value = d?.totalLikes ?? 0
})
try {
await Promise.all([
refreshPkg().then(() => refreshRepoMeta()),
variant === 'code-tree'
? fetchCodeTree()
: variant === 'function-tree'
? fetchFunctionTree()
: undefined,
fetchWeeklyEvolution(),
fetchLikes,
])
} catch (err) {
console.warn('[og-image-package] Failed to load data server-side:', err)
throw createError({
statusCode: 404,
})
}
// Generate sparkline SVG as base64 data URL
const sparklineSrc = computed(() => {
const values = weeklyValues.value
if (values.length < 2) return ''
const width = 500
const height = 200
const padY = 8
const max = Math.max(...values)
const min = Math.min(...values)
const range = max - min || 1
const points = values.map((v, i) => ({
x: (i / (values.length - 1)) * width,
y: padY + (1 - (v - min) / range) * (height - padY * 2),
}))
const pathData = createSmoothPath(points)
const firstX = points[0]!.x
const lastX = points.at(-1)!.x
const svg = [
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" fill="none" preserveAspectRatio="none">`,
`<defs><linearGradient id="af" x1="0" y1="0" x2="0" y2="1">`,
`<stop offset="0%" stop-color="white" stop-opacity="0.018"/>`,
`<stop offset="100%" stop-color="white" stop-opacity="0.001"/>`,
`</linearGradient></defs>`,
`<path d="M ${firstX},${height} L ${pathData} L ${lastX},${height} Z" fill="url(#af)"/>`,
`<path d="M ${pathData}" stroke="rgba(255,255,255,0.045)" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" fill="none"/>`,
`</svg>`,
].join('')
return `data:image/svg+xml;base64,${btoa(svg)}`
})
</script>
<template>
<OgLayout data-theme="dark">
<div class="px-15 py-12 flex flex-col justify-center gap-12 h-full">
<OgBrand :height="48" />
<div class="flex flex-col max-w-full gap-3">
<div
v-if="pkgNameParts.org"
class="lg:text-5xl text-3xl font-mono tracking-tight leading-none"
style="opacity: 0.5; text-overflow: ellipsis; line-clamp: 1"
>
{{ pkgNameParts.org }}
</div>
<div
class="tracking-tighter font-mono leading-none overflow-hidden"
:class="
(pkgNameParts.short?.length ?? 0) > 20 ? 'lg:text-6xl text-4xl' : 'lg:text-7xl text-5xl'
"
style="text-overflow: ellipsis; line-clamp: 1; word-break: break-all"
>
{{ pkgNameParts.short }}
</div>
<div
v-if="version"
class="pt-3 lg:text-4xl text-3xl font-mono tracking-tight leading-none"
style="opacity: 0.7; text-overflow: ellipsis; line-clamp: 1"
>
v{{ version }}
</div>
</div>
<div class="flex flex-col gap-3 text-4xl text-fg-muted">
<div v-if="repositoryUrl" class="flex items-center gap-2">
<div
:class="[repoProviderIcon, 'shrink-0 text-fg-muted']"
style="width: 24px; height: 24px"
/>
<span v-if="repoRef" class="max-w-[500px]" style="text-overflow: ellipsis">
{{ repoRef.owner }}<span class="opacity-50">/</span>{{ repoRef.repo }}
</span>
<span v-else>{{ $t('package.links.repo') }}</span>
</div>
<div class="flex items-center gap-5">
<span v-if="formattedDownloads" class="flex items-center gap-2" data-testid="downloads">
<div
class="i-lucide:download shrink-0 text-fg-muted"
style="width: 24px; height: 24px"
/>
<span>{{ formattedDownloads }}/wk</span>
</span>
<span v-if="formattedStars" class="flex items-center gap-2" data-testid="stars">
<div
class="i-lucide:star shrink-0 text-fg-muted"
style="width: 24px; height: 24px; fill: white"
/>
<span>{{ formattedStars }}</span>
</span>
<span v-if="formattedLikes" class="flex items-center gap-2" data-testid="likes">
<div
class="i-lucide:heart shrink-0 text-fg-muted"
style="width: 24px; height: 24px; fill: white"
/>
<span>{{ formattedLikes }}</span>
</span>
<div
v-if="pkg?.license && !pkg.license.includes(' ')"
class="flex items-center gap-2"
data-testid="license"
>
<div
class="i-lucide:scale shrink-0 text-fg-subtle self-center"
style="width: 24px; height: 24px"
/>
<span>{{ pkg.license }}</span>
</div>
</div>
</div>
</div>
<!-- Download chart variant -->
<img
v-if="variant === 'download-chart' && sparklineSrc"
:src="sparklineSrc"
class="absolute force-left-0 bottom-0 w-full h-[65%]"
/>
<!-- Code tree variant -->
<div
v-else-if="variant === 'code-tree' && treeRows.length"
class="text-fg-muted absolute force-right-8 top-8 bottom-8 w-[340px] flex flex-col gap-0 opacity-30 overflow-hidden font-mono text-4.5 leading-8"
>
<div
v-for="(row, i) in treeRows"
:key="i"
class="flex items-center gap-3 whitespace-nowrap text-fg"
:style="{ paddingLeft: `${row.depth * 20}px` }"
>
<div
v-if="row.isDir"
class="text-fg-muted shrink-0 i-lucide:folder"
style="width: 20px; height: 20px"
/>
<div
v-else
class="text-fg-muted shrink-0 i-lucide:file"
style="width: 20px; height: 20px"
/>
<span class="text-fg-muted">{{ row.name }}</span>
</div>
</div>
<!-- Function tree variant (API symbols) -->
<div
v-else-if="variant === 'function-tree' && symbolRows.length"
class="absolute force-right-8 top-8 bottom-8 w-[340px] flex flex-col gap-0 opacity-30 overflow-hidden font-mono text-4.5 leading-8"
>
<div
v-for="(row, i) in symbolRows"
:key="i"
class="flex items-center gap-3 whitespace-nowrap text-fg"
:style="{ paddingLeft: row.kind === 'symbol' ? '20px' : '0' }"
>
<div
v-if="row.icon === 'i-lucide:parentheses'"
class="shrink-0 text-fg-muted i-lucide:parentheses"
style="width: 20px; height: 20px"
/>
<div
v-else-if="row.icon === 'i-lucide:box'"
class="shrink-0 text-fg-muted i-lucide:box"
style="width: 20px; height: 20px"
/>
<div
v-else-if="row.icon === 'i-lucide:puzzle'"
class="shrink-0 text-fg-muted i-lucide:puzzle"
style="width: 20px; height: 20px"
/>
<div
v-else-if="row.icon === 'i-lucide:type'"
class="shrink-0 text-fg-muted i-lucide:type"
style="width: 20px; height: 20px"
/>
<div
v-else-if="row.icon === 'i-lucide:variable'"
class="shrink-0 text-fg-muted i-lucide:variable"
style="width: 20px; height: 20px"
/>
<div
v-else-if="row.icon === 'i-lucide:list'"
class="shrink-0 text-fg-muted i-lucide:list"
style="width: 20px; height: 20px"
/>
<div
v-else-if="row.icon === 'i-lucide:package'"
class="shrink-0 text-fg-muted i-lucide:package"
style="width: 20px; height: 20px"
/>
<div
v-else
class="shrink-0 text-fg-muted i-lucide:code"
style="width: 20px; height: 20px"
/>
<span class="text-fg-muted" :class="row.kind === 'section' ? 'text-4 mt-1' : ''">{{
row.name
}}</span>
</div>
</div>
</OgLayout>
</template>