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

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

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