docs/.vitepress/configs/hooks/opengraph.ts

b61d4f52b46f7ea6eb963e42e646b9b36f8ecb3e
wotaku/docs/.vitepress/configs/hooks/opengraph.ts history · blame · raw

123 lines · 3378 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 { renderAsync } from '@resvg/resvg-js'
  9import { mkdir, readFile, writeFile } from 'node:fs/promises'
 10import { dirname, resolve } from 'node:path'
 11import { fileURLToPath } from 'node:url'
 12import { createContentLoader } from 'vitepress'
 13import type { ContentData, SiteConfig } from 'vitepress'
 14import { type SatoriOptions, satoriVue } from 'x-satori/vue'
 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: frontmatter.layout === 'home'
104        ? (frontmatter.hero.name ?? frontmatter.title)
105        : (frontmatter.customMetaTitle ?? frontmatter.title),
106      description: frontmatter.layout === 'home'
107        ? (frontmatter.hero.tagline ?? frontmatter.description)
108        : frontmatter.description,
109      image: frontmatter.og?.image ?? 'https://i.wotaku.wiki/f/default.png'
110    }
111  }
112
113  const svg = await satoriVue(options, template)
114
115  const render = await renderAsync(svg)
116
117  const outputFolder = resolve(outDir, url.substring(1), '__og_image__')
118  const outputFile = resolve(outputFolder, 'og.png')
119
120  await mkdir(outputFolder, { recursive: true })
121
122  return await writeFile(outputFile, render.asPng())
123}