forked from labex-labs/python-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.ts
More file actions
228 lines (200 loc) · 6.88 KB
/
meta.ts
File metadata and controls
228 lines (200 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { SUPPORTED_LOCALES, useI18n } from './useI18n'
interface ArticleMeta {
title?: string
description?: string
date?: string
updated?: string
tags?: string
socialImage?: string
}
function getBaseUrl(): string {
const envBaseUrl = import.meta.env.VITE_BASE_URL || 'labex.io/pythoncheatsheet'
const isDev = import.meta.env.DEV || import.meta.env.MODE === 'development'
const isLocalhost = typeof window !== 'undefined' &&
(window.location.hostname === 'localhost' ||
window.location.hostname === '127.0.0.1' ||
window.location.hostname.startsWith('192.168.') ||
window.location.hostname.includes('.local'))
const isPreviewDomain = envBaseUrl.includes('next.') ||
envBaseUrl.includes('staging.') ||
envBaseUrl.includes('preview.')
if (isDev || isLocalhost || isPreviewDomain) {
return 'labex.io/pythoncheatsheet'
}
return envBaseUrl.startsWith('http')
? envBaseUrl.replace(/^https?:\/\//, '').replace(/\/$/, '')
: envBaseUrl.replace(/\/$/, '')
}
export function useMeta(articleMeta?: ArticleMeta) {
const route = useRoute()
const { t, currentLocale } = useI18n()
const base_url = getBaseUrl()
const defaultDescription = computed(() => t('meta.description'))
const defaultCardImage =
'https://raw.githubusercontent.com/labex-labs/python-cheatsheet/master/public/screenshots/dark.png'
const themeColor = computed(() => (isDark.value ? '#1f2937' : '#ffffff'))
const url = computed(() => `https://${base_url}${route.path}`)
const isArticle = computed(() => {
return !!(
articleMeta?.title ||
route.path.includes('/blog/') ||
route.meta?.layout === 'article'
)
})
const isHomePage = computed(() => {
return route.path === '/' || route.path === `/${currentLocale.value}`
})
const pageTitle = computed(() => {
return articleMeta?.title || t('meta.title')
})
const pageDescription = computed(() => {
return articleMeta?.description || defaultDescription.value
})
const pageImage = computed(() => {
if (articleMeta?.socialImage) {
return articleMeta.socialImage.startsWith('http')
? articleMeta.socialImage
: `https://${base_url}${articleMeta.socialImage}`
}
return defaultCardImage
})
const ogType = computed(() => {
if (isArticle.value) {
return 'article'
}
if (isHomePage.value) {
return 'website'
}
return 'website'
})
const twitterCardType = computed(() => {
return pageImage.value && pageImage.value !== defaultCardImage
? 'summary_large_image'
: 'summary'
})
const localeCode = computed(() => {
const localeMap: Record<string, string> = {
en: 'en_US',
zh: 'zh_CN',
es: 'es_ES',
fr: 'fr_FR',
de: 'de_DE',
ja: 'ja_JP',
ru: 'ru_RU',
ko: 'ko_KR',
pt: 'pt_BR',
}
return localeMap[currentLocale.value] || 'en_US'
})
// Get base path (remove locale prefix)
const getBasePath = (path: string): string => {
const segments = path.split('/').filter(Boolean)
if (segments.length > 0 && SUPPORTED_LOCALES.includes(segments[0] as typeof SUPPORTED_LOCALES[number])) {
segments.shift()
return segments.length > 0 ? '/' + segments.join('/') : '/'
}
return path
}
// Generate hreflang links
const generateHreflangLinks = computed(() => {
const basePath = getBasePath(route.path)
const links = []
// Generate hreflang link for each supported locale
for (const locale of SUPPORTED_LOCALES) {
const localePath = locale === 'en' ? basePath : `/${locale}${basePath}`
const localeUrl = `https://${base_url}${localePath}`
links.push({
rel: 'alternate',
hreflang: locale,
href: localeUrl,
})
}
// Add x-default (points to default language version)
const defaultPath = basePath
const defaultUrl = `https://${base_url}${defaultPath}`
links.push({
rel: 'alternate',
hreflang: 'x-default',
href: defaultUrl,
})
return links
})
const keywords = computed(() => {
if (articleMeta?.tags) {
if (typeof articleMeta.tags === 'string') {
return articleMeta.tags
}
if (Array.isArray(articleMeta.tags)) {
return (articleMeta.tags as string[]).join(', ')
}
return String(articleMeta.tags)
}
return t('meta.keywords')
})
const parseDate = (dateStr: string | undefined): string | undefined => {
if (!dateStr) return undefined
try {
const date = new Date(dateStr)
if (isNaN(date.getTime())) return undefined
return date.toISOString()
} catch {
return undefined
}
}
const metaTags = computed(() => {
const tags: any[] = [
{ name: 'theme-color', content: themeColor.value },
{ name: 'description', content: pageDescription.value },
{ name: 'author', content: 'Python Cheatsheet' },
{ name: 'keywords', content: keywords.value },
{ property: 'og:title', content: pageTitle.value },
{ property: 'og:description', content: pageDescription.value },
{ property: 'og:url', content: url.value },
{ property: 'og:type', content: ogType.value },
{ property: 'og:image', content: pageImage.value },
{ property: 'og:site_name', content: 'Python Cheatsheet' },
{ property: 'og:locale', content: localeCode.value },
{ name: 'twitter:title', content: pageTitle.value },
{ name: 'twitter:description', content: pageDescription.value },
{ name: 'twitter:image', content: pageImage.value },
{ name: 'twitter:card', content: twitterCardType.value },
{ name: 'twitter:site', content: '@labex_io' },
]
if (isArticle.value && articleMeta) {
const publishedDate = parseDate(articleMeta.date)
if (publishedDate) {
tags.push({ property: 'article:published_time', content: publishedDate })
}
const modifiedDate = parseDate(articleMeta.updated)
if (modifiedDate) {
tags.push({ property: 'article:modified_time', content: modifiedDate })
}
if (articleMeta.tags) {
const tagsValue = typeof articleMeta.tags === 'string'
? articleMeta.tags
: Array.isArray(articleMeta.tags)
? (articleMeta.tags as string[]).join(',')
: String(articleMeta.tags)
const tagList = tagsValue.split(',').map((tag: string) => tag.trim()).filter(Boolean)
tagList.forEach((tag: string) => {
tags.push({ property: 'article:tag', content: tag })
})
}
tags.push({ property: 'article:author', content: 'Python Cheatsheet' })
}
return tags
})
const meta = computed(() => ({
title: pageTitle.value,
description: pageDescription.value,
htmlAttrs: {
lang: currentLocale.value,
},
meta: metaTags.value,
link: [
{ rel: 'canonical', href: url.value },
...generateHreflangLinks.value,
],
}))
return { meta, description: pageDescription }
}