-
Notifications
You must be signed in to change notification settings - Fork 713
/
Copy pathshowcase.ts
56 lines (48 loc) · 1.53 KB
/
showcase.ts
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
import { defineNuxtModule } from '@nuxt/kit'
import { existsSync } from 'node:fs'
import { join } from 'pathe'
import captureWebsite from 'capture-website'
interface ContentFile {
id?: string
body?: {
items: TemplateItem[]
}
}
interface TemplateItem {
name: string
url?: string
screenshotUrl?: string
screenshotOptions?: Record<string, any>
}
export default defineNuxtModule((_, nuxt) => {
nuxt.hook('content:file:afterParse', async ({ content: file }: { content: ContentFile }) => {
if (!file.id?.includes('showcase')) {
return
}
if (!file.body?.items?.length) {
return
}
for (const template of file.body.items) {
const url = template.screenshotUrl || template.url
if (!url) {
console.error(`Template ${template.name} has no "url" or "screenshotUrl" to take a screenshot from`)
continue
}
const name = template.name.toLowerCase().replace(/\s/g, '-')
const filename = join(process.cwd(), 'docs/public/assets/showcase', `${name}.png`)
if (existsSync(filename)) {
continue
}
console.log(`Generating screenshot for Template ${template.name} hitting ${url}...`)
try {
await captureWebsite.file(url, filename, {
...(template.screenshotOptions || {}),
launchOptions: { headless: true }
})
console.log(`Screenshot for ${template.name} generated successfully`)
} catch (error) {
console.error(`Error generating screenshot for ${template.name}:`, error)
}
}
})
})