docs/.vitepress/configs/hooks/meta.ts

b2c3e4e6874256bc959c763c2427cd2f3e4e4fcd
wotaku/docs/.vitepress/configs/hooks/meta.ts history · blame · raw

132 lines · 3733 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 type { HeadConfig, TransformContext } from 'vitepress'
  9import { excludedFiles } from '../constants'
 10
 11export function generateMeta(context: TransformContext, hostname: string) {
 12  const head: HeadConfig[] = []
 13  const { pageData } = context
 14  if (
 15    pageData.isNotFound ||
 16    excludedFiles.includes(pageData.filePath) ||
 17    pageData.frontmatter.exclude
 18  )
 19    return head
 20
 21  const { relativePath, frontmatter, filePath, lastUpdated } = pageData
 22
 23  // Uncomment to debug, but don't commit
 24  // The last filePath printed before the error is the one that's causing the error
 25  // console.info({ filePath });
 26  const url = `${hostname}/${relativePath.replace(/((^|\/)index)?\.md$/, '$2')}`
 27
 28  head.push(
 29    ['link', { rel: 'canonical', href: url }],
 30    ['meta', { property: 'og:url', content: url }],
 31    ['meta', { name: 'twitter:url', content: url }],
 32    ['meta', { name: 'twitter:card', content: 'summary_large_image' }]
 33  )
 34
 35  if (frontmatter.theme) {
 36    head.push(['meta', { name: 'theme-color', content: frontmatter.theme }])
 37  }
 38
 39  if (frontmatter.type) {
 40    head.push(['meta', { property: 'og:type', content: frontmatter.type }])
 41  }
 42
 43  head.push(
 44    [
 45      'meta',
 46      {
 47        property: 'og:description',
 48        content: frontmatter.customDescription ?? frontmatter.description
 49      }
 50    ],
 51    [
 52      'meta',
 53      {
 54        name: 'twitter:description',
 55        content: frontmatter.customDescription ?? frontmatter.description
 56      }
 57    ],
 58    ['meta', { property: 'og:title', content: frontmatter.title }],
 59    ['meta', { name: 'twitter:title', content: frontmatter.title }]
 60  )
 61
 62  if (frontmatter.image) {
 63    head.push([
 64      'meta',
 65      {
 66        property: 'og:image',
 67        content: `${hostname}/${frontmatter.image.replace(/^\//, '')}`
 68      }
 69    ])
 70    head.push([
 71      'meta',
 72      {
 73        name: 'twitter:image',
 74        content: `${hostname}/${frontmatter.image.replace(/^\//, '')}`
 75      }
 76    ])
 77  } else {
 78    // Home page.
 79    const url = filePath.replace('index.md', '').replace('.md', '')
 80    const imageUrl = `${url}/__og_image__/og.png`
 81      .replace(/\/\//g, '/')
 82      .replace(/^\//, '')
 83
 84    head.push([
 85      'meta',
 86      { property: 'og:image', content: `${hostname}/${imageUrl}` }
 87    ])
 88    head.push(['meta', { property: 'og:image:width', content: '1098' }])
 89    head.push(['meta', { property: 'og:image:height', content: '530' }])
 90    head.push(['meta', { property: 'og:image:type', content: 'image/png' }])
 91    head.push([
 92      'meta',
 93      { property: 'og:image:alt', content: frontmatter.title }
 94    ])
 95    head.push([
 96      'meta',
 97      { name: 'twitter:image', content: `${hostname}/${imageUrl}` }
 98    ])
 99    head.push(['meta', { name: 'twitter:image:width', content: '1098' }])
100    head.push(['meta', { name: 'twitter:image:height', content: '530' }])
101    head.push([
102      'meta',
103      { name: 'twitter:image:alt', content: frontmatter.title }
104    ])
105  }
106
107  if (frontmatter.tag) {
108    head.push(['meta', { property: 'article:tag', content: frontmatter.tag }])
109  }
110
111  if (frontmatter.date) {
112    head.push([
113      'meta',
114      {
115        property: 'article:published_time',
116        content: frontmatter.date
117      }
118    ])
119  }
120
121  if (lastUpdated && pageData.frontmatter.lastUpdated !== false) {
122    head.push([
123      'meta',
124      {
125        property: 'article:modified_time',
126        content: new Date(lastUpdated).toISOString()
127      }
128    ])
129  }
130
131  return head
132}