Skip to content

Commit 9600f21

Browse files
authored
feat: 添加自定义 head 支持 (#17)
* feat: 添加自定义 head 支持 * update
1 parent 783ca6e commit 9600f21

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,28 @@ const themeConfig: ThemeContext = {
4949
+ '<p>若存在任何有误或不当内容,请联系 <a href="mailto:rlewiki@project-trans.org">rlewiki@project-trans.org</a>。</p>',
5050
},],
5151
disclaimerStatusKey: 'disclaimerStatus', // 若要存储免责声明 banner 隐藏状态,需要指定此字段,存储在localStorage中
52-
disclaimerStatusExpiration: 1000 // 指定 disclaimerStatus 有效的毫秒数,1000这个值显然太短了,你需要选个更大的数,默认持续生效7天
52+
disclaimerStatusExpiration: 1000, // 指定 disclaimerStatus 有效的毫秒数,1000这个值显然太短了,你需要选个更大的数,默认持续生效7天
53+
54+
// i18n
55+
locales: {
56+
root: {
57+
label: '中文',
58+
lang: 'zh',
59+
},
60+
fr: {
61+
label: 'French',
62+
lang: 'fr', // 可选,将作为 `lang` 属性添加到 `html` 标签中
63+
link: '/fr/', // 默认 /fr/ -- 显示在导航栏翻译菜单上,可以是外部的
64+
65+
// 其余 locale 特定属性...
66+
},
67+
},
68+
// 添加自定义 head 元素
69+
additionalHead: [
70+
['link', { rel: 'icon', href: 'https://rle.wiki/logo-with-shadow.png' }],
71+
['meta', { name: 'theme-color', content: '#ffffff' }],
72+
['meta', { name: 'viewport', content: 'width=device-width, initial-scale=1.0' }],
73+
],
5374
}
5475

5576
// https://vitepress.dev/reference/site-config

example/docs/.vitepress/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ const themeConfig: ThemeContext = {
8989
// 其余 locale 特定属性...
9090
},
9191
},
92+
// 添加自定义 head 元素
93+
// additionalHead: [
94+
// ['link', { rel: 'icon', href: 'https://rle.wiki/logo-with-shadow.png' }],
95+
// ['meta', { name: 'theme-color', content: '#ffffff' }],
96+
// ['meta', { name: 'viewport', content: 'width=device-width, initial-scale=1.0' }],
97+
// ],
9298
}
9399

94100
// https://vitepress.dev/reference/site-config

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function genConfig() {
9292
disclaimerStatusKey,
9393
disclaimerStatusExpiration,
9494
locales, // i18n
95+
additionalHead = [],
9596
} = themeConfig
9697

9798
return defineConfigWithTheme<PjtsThemeConfig>({
@@ -140,6 +141,7 @@ function genConfig() {
140141
['meta', { name: 'msapplication-TileColor', content: '#4c4c4c' }],
141142
['meta', { name: 'theme-color', content: '#ffffff' }],
142143
['meta', { property: 'og:site_name', content: siteTitle }],
144+
...additionalHead, // 其他自定义的 head 元素
143145
],
144146
themeConfig: {
145147
org,

src/utils/themeContext.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export interface LocalesConfig {
1717
link?: string
1818
}
1919

20+
export type HeadConfig =
21+
| [string, Record<string, string>]
22+
| [string, Record<string, string>, string]
23+
2024
export interface ThemeContext {
2125
siteTitle: string
2226
siteLogo: string
@@ -42,6 +46,7 @@ export interface ThemeContext {
4246
disclaimerStatusKey?: string
4347
disclaimerStatusExpiration?: number
4448
locales?: { [key: string]: LocalesConfig } // i18n
49+
additionalHead?: HeadConfig[] // Additional head elements
4550
}
4651

4752
const themeContext = new AsyncLocalStorage<ThemeContext>()

0 commit comments

Comments
 (0)