docs/.vitepress/configs/hooks/opengraph.ts
126 lines · 3422 bytes
1/**
2* All Rights Reserved
3*
4* Copyright (c) 2025 taskylizard
5*
6* All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
7*/
8import { mkdir, readFile, writeFile } from 'node:fs/promises'
9import { dirname, resolve } from 'node:path'
10import { fileURLToPath } from 'node:url'
11import { createContentLoader } from 'vitepress'
12import type { ContentData, SiteConfig } from 'vitepress'
13import { type SatoriOptions, satoriVue } from 'x-satori/vue'
14import { renderAsync } from '@resvg/resvg-js'
15import { excludedFiles } from '../constants'
16
17const __dirname = dirname(fileURLToPath(import.meta.url))
18const __fonts = resolve(__dirname, '../fonts')
19
20export async function generateImages(config: SiteConfig) {
21 const pages = await createContentLoader('**/*.md', {
22 excerpt: true,
23 globOptions: {
24 ignore: [...excludedFiles, 'node_modules', 'dist']
25 }
26 }).load()
27 const template = await readFile(
28 resolve(__dirname, '../../theme/components/OgImageTemplate.vue'),
29 'utf-8'
30 )
31
32 const fonts: SatoriOptions['fonts'] = [
33 {
34 name: 'Inter',
35 data: await readFile(resolve(__fonts, 'Inter-Regular.otf')),
36 weight: 400,
37 style: 'normal'
38 },
39 {
40 name: 'Inter',
41 data: await readFile(resolve(__fonts, 'Inter-Medium.otf')),
42 weight: 500,
43 style: 'normal'
44 },
45 {
46 name: 'Inter',
47 data: await readFile(resolve(__fonts, 'Inter-SemiBold.otf')),
48 weight: 600,
49 style: 'normal'
50 },
51 {
52 name: 'Inter',
53 data: await readFile(resolve(__fonts, 'Inter-Bold.otf')),
54 weight: 700,
55 style: 'normal'
56 }
57 ]
58
59 const filteredPages = pages.filter((p) => p.frontmatter.image === undefined)
60
61 for (const page of filteredPages) {
62 await generateImage({
63 page,
64 template,
65 outDir: config.outDir,
66 fonts
67 })
68 }
69}
70
71interface GenerateImagesOptions {
72 page: ContentData
73 template: string
74 outDir: string
75 fonts: SatoriOptions['fonts']
76}
77
78function getDir(url: string) {
79 if (url.startsWith('/glossary/')) {
80 return 'Glossary'
81 } else if (url.startsWith('/guides/')) {
82 return 'Guide'
83 }
84
85 // Means we are at root.
86 return undefined
87}
88
89async function generateImage({
90 page,
91 template,
92 outDir,
93 fonts
94}: GenerateImagesOptions) {
95 const { frontmatter, url } = page
96
97 const options: SatoriOptions = {
98 // Use custom dimensions from frontmatter.og if provided
99 width: frontmatter.og?.width ?? 1800,
100 height: frontmatter.og?.height ?? 900,
101 fonts,
102 props: {
103 title:
104 frontmatter.layout === 'home'
105 ? (frontmatter.hero.name ?? frontmatter.title)
106 : (frontmatter.customMetaTitle ?? frontmatter.title),
107 description:
108 frontmatter.layout === 'home'
109 ? (frontmatter.hero.tagline ?? frontmatter.description)
110 : frontmatter.description,
111 // dir: getDir(url)
112 image: frontmatter.og?.image ?? 'https://i.wotaku.wiki/f/default.png'
113 }
114 }
115
116 const svg = await satoriVue(options, template)
117
118 const render = await renderAsync(svg)
119
120 const outputFolder = resolve(outDir, url.substring(1), '__og_image__')
121 const outputFile = resolve(outputFolder, 'og.png')
122
123 await mkdir(outputFolder, { recursive: true })
124
125 return await writeFile(outputFile, render.asPng())
126}