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

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

140 lines · 4055 bytes

  1/**
  2 *  Copyright (c) 2024 taskylizard
  3 *
  4 *  Licensed under the Apache License, Version 2.0 (the "License");
  5 *  you may not use this file except in compliance with the License.
  6 *  You may obtain a copy of the License at
  7 *
  8 *  http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 *  Unless required by applicable law or agreed to in writing, software
 11 *  distributed under the License is distributed on an "AS IS" BASIS,
 12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 *  See the License for the specific language governing permissions and
 14 *  limitations under the License.
 15 */
 16import type { HeadConfig, TransformContext } from 'vitepress'
 17import { excludedFiles } from '../constants'
 18
 19export function generateMeta(context: TransformContext, hostname: string) {
 20  const head: HeadConfig[] = []
 21  const { pageData } = context
 22  if (
 23    pageData.isNotFound ||
 24    excludedFiles.includes(pageData.filePath) ||
 25    pageData.frontmatter.exclude
 26  )
 27    return head
 28
 29  const { relativePath, frontmatter, filePath, lastUpdated } = pageData
 30
 31  // Uncomment to debug, but don't commit
 32  // The last filePath printed before the error is the one that's causing the error
 33  // console.info({ filePath });
 34  const url = `${hostname}/${relativePath.replace(/((^|\/)index)?\.md$/, '$2')}`
 35
 36  head.push(
 37    ['link', { rel: 'canonical', href: url }],
 38    ['meta', { property: 'og:url', content: url }],
 39    ['meta', { name: 'twitter:url', content: url }],
 40    ['meta', { name: 'twitter:card', content: 'summary_large_image' }]
 41  )
 42
 43  if (frontmatter.theme) {
 44    head.push(['meta', { name: 'theme-color', content: frontmatter.theme }])
 45  }
 46
 47  if (frontmatter.type) {
 48    head.push(['meta', { property: 'og:type', content: frontmatter.type }])
 49  }
 50
 51  head.push(
 52    [
 53      'meta',
 54      {
 55        property: 'og:description',
 56        content: frontmatter.customDescription ?? frontmatter.description
 57      }
 58    ],
 59    [
 60      'meta',
 61      {
 62        name: 'twitter:description',
 63        content: frontmatter.customDescription ?? frontmatter.description
 64      }
 65    ],
 66    ['meta', { property: 'og:title', content: frontmatter.title }],
 67    ['meta', { name: 'twitter:title', content: frontmatter.title }]
 68  )
 69
 70  if (frontmatter.image) {
 71    head.push([
 72      'meta',
 73      {
 74        property: 'og:image',
 75        content: `${hostname}/${frontmatter.image.replace(/^\//, '')}`
 76      }
 77    ])
 78    head.push([
 79      'meta',
 80      {
 81        name: 'twitter:image',
 82        content: `${hostname}/${frontmatter.image.replace(/^\//, '')}`
 83      }
 84    ])
 85  } else {
 86    // Home page.
 87    const url = filePath.replace('index.md', '').replace('.md', '')
 88    const imageUrl = `${url}/__og_image__/og.png`
 89      .replace(/\/\//g, '/')
 90      .replace(/^\//, '')
 91
 92    head.push([
 93      'meta',
 94      { property: 'og:image', content: `${hostname}/${imageUrl}` }
 95    ])
 96    head.push(['meta', { property: 'og:image:width', content: '1098' }])
 97    head.push(['meta', { property: 'og:image:height', content: '530' }])
 98    head.push(['meta', { property: 'og:image:type', content: 'image/png' }])
 99    head.push([
100      'meta',
101      { property: 'og:image:alt', content: frontmatter.title }
102    ])
103    head.push([
104      'meta',
105      { name: 'twitter:image', content: `${hostname}/${imageUrl}` }
106    ])
107    head.push(['meta', { name: 'twitter:image:width', content: '1098' }])
108    head.push(['meta', { name: 'twitter:image:height', content: '530' }])
109    head.push([
110      'meta',
111      { name: 'twitter:image:alt', content: frontmatter.title }
112    ])
113  }
114
115  if (frontmatter.tag) {
116    head.push(['meta', { property: 'article:tag', content: frontmatter.tag }])
117  }
118
119  if (frontmatter.date) {
120    head.push([
121      'meta',
122      {
123        property: 'article:published_time',
124        content: frontmatter.date
125      }
126    ])
127  }
128
129  if (lastUpdated && pageData.frontmatter.lastUpdated !== false) {
130    head.push([
131      'meta',
132      {
133        property: 'article:modified_time',
134        content: new Date(lastUpdated).toISOString()
135      }
136    ])
137  }
138
139  return head
140}