|
| 1 | +import type { HeadConfig } from 'vitepress' |
| 2 | + |
| 3 | +export const LOCALES = ['en_us', 'zh_cn', 'pt_br'] as const |
| 4 | +export type WikiLocale = (typeof LOCALES)[number] |
| 5 | + |
| 6 | +type SitemapLink = { |
| 7 | + lang: string |
| 8 | + hreflang?: string |
| 9 | + url: string |
| 10 | +} |
| 11 | + |
| 12 | +type WikiSitemapItem = { |
| 13 | + url: string |
| 14 | + links?: SitemapLink[] |
| 15 | + lastmod?: string | number | Date |
| 16 | +} |
| 17 | + |
| 18 | +const HREFLANG: Record<WikiLocale, string> = { |
| 19 | + en_us: 'en', |
| 20 | + zh_cn: 'zh-CN', |
| 21 | + pt_br: 'pt-BR', |
| 22 | +} |
| 23 | + |
| 24 | +const LOCALE_INDEX_RE = /^modern\/(en_us|zh_cn|pt_br)\/index\.md$/ |
| 25 | + |
| 26 | +/** Map a docs-relative path to the public URL (cleanUrls). */ |
| 27 | +export function pageToCanonical(siteUrl: string, page: string): string { |
| 28 | + const base = siteUrl.replace(/\/$/, '') |
| 29 | + if (page === 'index.md') { |
| 30 | + return `${base}/modern/en_us/` |
| 31 | + } |
| 32 | + const indexMatch = page.match(/^modern\/(en_us|zh_cn|pt_br)\/index\.md$/) |
| 33 | + if (indexMatch) { |
| 34 | + return `${base}/modern/${indexMatch[1]}/` |
| 35 | + } |
| 36 | + const path = page.replace(/\.md$/, '') |
| 37 | + return `${base}/${path}` |
| 38 | +} |
| 39 | + |
| 40 | +function localePageSuffix(page: string): string | null { |
| 41 | + const match = page.match(/^modern\/(en_us|zh_cn|pt_br)\/(.+\.md)$/) |
| 42 | + if (!match || match[2] === 'index.md') { |
| 43 | + return null |
| 44 | + } |
| 45 | + return match[2].replace(/\.md$/, '').replace(/\/index$/, '') |
| 46 | +} |
| 47 | + |
| 48 | +export function buildHreflangHead(siteUrl: string, page: string): HeadConfig[] { |
| 49 | + const base = siteUrl.replace(/\/$/, '') |
| 50 | + const suffix = localePageSuffix(page) |
| 51 | + if (!suffix) { |
| 52 | + if (LOCALE_INDEX_RE.test(page) || page === 'index.md') { |
| 53 | + return hreflangLinks(base, LOCALES.map((locale) => `${base}/modern/${locale}/`)) |
| 54 | + } |
| 55 | + return [] |
| 56 | + } |
| 57 | + const urls = LOCALES.map((locale) => `${base}/modern/${locale}/${suffix}`) |
| 58 | + return hreflangLinks(base, urls) |
| 59 | +} |
| 60 | + |
| 61 | +function hreflangLinks(_base: string, urls: string[]): HeadConfig[] { |
| 62 | + const links: HeadConfig[] = LOCALES.map((locale, index) => [ |
| 63 | + 'link', |
| 64 | + { rel: 'alternate', hreflang: HREFLANG[locale], href: urls[index] }, |
| 65 | + ]) |
| 66 | + links.push(['link', { rel: 'alternate', hreflang: 'x-default', href: urls[0] }]) |
| 67 | + return links |
| 68 | +} |
| 69 | + |
| 70 | +export function buildPageSeoHead( |
| 71 | + siteUrl: string, |
| 72 | + page: string, |
| 73 | + title: string, |
| 74 | + description: string, |
| 75 | + ogImage: string, |
| 76 | +): HeadConfig[] { |
| 77 | + const canonical = pageToCanonical(siteUrl, page) |
| 78 | + return [ |
| 79 | + ['link', { rel: 'canonical', href: canonical }], |
| 80 | + ['meta', { property: 'og:url', content: canonical }], |
| 81 | + ['meta', { property: 'og:title', content: title }], |
| 82 | + ['meta', { property: 'og:description', content: description }], |
| 83 | + ['meta', { name: 'twitter:card', content: 'summary' }], |
| 84 | + ['meta', { name: 'twitter:title', content: title }], |
| 85 | + ['meta', { name: 'twitter:description', content: description }], |
| 86 | + ['meta', { name: 'twitter:image', content: ogImage }], |
| 87 | + ...buildHreflangHead(siteUrl, page), |
| 88 | + ] |
| 89 | +} |
| 90 | + |
| 91 | +export function buildWebSiteJsonLd(siteUrl: string): string { |
| 92 | + const base = siteUrl.replace(/\/$/, '') |
| 93 | + return JSON.stringify({ |
| 94 | + '@context': 'https://schema.org', |
| 95 | + '@type': 'WebSite', |
| 96 | + name: 'TerraFirmaGreg Wiki', |
| 97 | + url: base, |
| 98 | + description: |
| 99 | + 'Official TerraFirmaGreg wiki — modpack info, upgrade guides, and developer references.', |
| 100 | + publisher: { |
| 101 | + '@type': 'Organization', |
| 102 | + name: 'TerraFirmaGreg Team', |
| 103 | + url: 'https://terrafirmagreg.team', |
| 104 | + }, |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +export function transformWikiSitemapItems(_siteUrl: string, items: WikiSitemapItem[]): WikiSitemapItem[] { |
| 109 | + return items |
| 110 | + .filter((item) => { |
| 111 | + const path = normalizeSitemapPath(item.url) |
| 112 | + return path !== '/' |
| 113 | + }) |
| 114 | + .map((item) => { |
| 115 | + const links = sitemapHreflangLinks(item.url) |
| 116 | + return links ? { ...item, links } : item |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +function normalizeSitemapPath(url: string): string { |
| 121 | + const path = url.startsWith('/') ? url : `/${url}` |
| 122 | + return path.replace(/\/$/, '') || '/' |
| 123 | +} |
| 124 | + |
| 125 | +function sitemapHreflangLinks(url: string): WikiSitemapItem['links'] | undefined { |
| 126 | + const path = normalizeSitemapPath(url) |
| 127 | + |
| 128 | + const indexMatch = path.match(/^\/modern\/(en_us|zh_cn|pt_br)$/) |
| 129 | + if (indexMatch) { |
| 130 | + return hreflangSitemapLinks((locale) => `/modern/${locale}/`) |
| 131 | + } |
| 132 | + |
| 133 | + const pageMatch = path.match(/^\/modern\/(en_us|zh_cn|pt_br)\/(.+)$/) |
| 134 | + if (pageMatch) { |
| 135 | + const suffix = pageMatch[2]! |
| 136 | + return hreflangSitemapLinks((locale) => `/modern/${locale}/${suffix}`) |
| 137 | + } |
| 138 | + |
| 139 | + return undefined |
| 140 | +} |
| 141 | + |
| 142 | +function hreflangSitemapLinks(urlForLocale: (locale: WikiLocale) => string): WikiSitemapItem['links'] { |
| 143 | + const links = LOCALES.map((locale) => ({ |
| 144 | + lang: HREFLANG[locale], |
| 145 | + hreflang: HREFLANG[locale], |
| 146 | + url: urlForLocale(locale), |
| 147 | + })) |
| 148 | + links.push({ lang: 'x-default', hreflang: 'x-default', url: urlForLocale('en_us') }) |
| 149 | + return links |
| 150 | +} |
| 151 | + |
| 152 | +export function parseSitemapLocs(xml: string): string[] { |
| 153 | + const locs: string[] = [] |
| 154 | + const re = /<loc>([^<]+)<\/loc>/g |
| 155 | + let match: RegExpExecArray | null |
| 156 | + while ((match = re.exec(xml)) !== null) { |
| 157 | + locs.push(match[1]!) |
| 158 | + } |
| 159 | + return locs |
| 160 | +} |
| 161 | + |
| 162 | +export function buildMergedSitemap(urls: Iterable<string>): string { |
| 163 | + const unique = [...new Set([...urls].filter(Boolean))] |
| 164 | + const body = unique.map((url) => ` <url><loc>${escapeXml(url)}</loc></url>`).join('\n') |
| 165 | + return [ |
| 166 | + '<?xml version="1.0" encoding="UTF-8"?>', |
| 167 | + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', |
| 168 | + body, |
| 169 | + '</urlset>', |
| 170 | + '', |
| 171 | + ].join('\n') |
| 172 | +} |
| 173 | + |
| 174 | +function escapeXml(value: string): string { |
| 175 | + return value |
| 176 | + .replace(/&/g, '&') |
| 177 | + .replace(/</g, '<') |
| 178 | + .replace(/>/g, '>') |
| 179 | + .replace(/"/g, '"') |
| 180 | + .replace(/'/g, ''') |
| 181 | +} |
0 commit comments