-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.ts
120 lines (108 loc) · 4.15 KB
/
index.ts
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
import type { StarlightPlugin, StarlightUserConfig } from '@astrojs/starlight/types'
import type { AstroIntegrationLogger } from 'astro'
import { type StarlightBlogConfig, validateConfig, type StarlightBlogUserConfig } from './libs/config'
import { getDefaultLocale } from './libs/i18n'
import { stripLeadingSlash, stripTrailingSlash } from './libs/path'
import { vitePluginStarlightBlogConfig } from './libs/vite'
export type { StarlightBlogConfig, StarlightBlogUserConfig }
export default function starlightBlogPlugin(userConfig?: StarlightBlogUserConfig): StarlightPlugin {
const config = validateConfig(userConfig)
return {
name: 'starlight-blog-plugin',
hooks: {
setup({ addIntegration, astroConfig, config: starlightConfig, logger, updateConfig: updateStarlightConfig }) {
const rssLink = astroConfig.site
? `${stripTrailingSlash(astroConfig.site)}${stripTrailingSlash(astroConfig.base)}/${stripLeadingSlash(
stripTrailingSlash(config.prefix),
)}/rss.xml`
: undefined
updateStarlightConfig({
components: {
...starlightConfig.components,
...overrideStarlightComponent(starlightConfig.components, logger, 'MarkdownContent'),
...overrideStarlightComponent(starlightConfig.components, logger, 'Sidebar'),
...overrideStarlightComponent(starlightConfig.components, logger, 'ThemeSelect'),
},
head: [
...(starlightConfig.head ?? []),
...(astroConfig.site
? [
{
tag: 'link' as const,
attrs: {
href: rssLink,
rel: 'alternate',
title: config.title,
type: 'application/rss+xml',
},
},
]
: []),
],
social: {
...starlightConfig.social,
...(astroConfig.site && rssLink && !starlightConfig.social?.rss
? {
rss: rssLink,
}
: {}),
},
})
addIntegration({
name: 'starlight-blog-integration',
hooks: {
'astro:config:setup': ({ injectRoute, updateConfig }) => {
injectRoute({
entrypoint: 'starlight-blog/routes/Tags.astro',
pattern: '/[prefix]/tags/[tag]',
prerender: true,
})
injectRoute({
entrypoint: 'starlight-blog/routes/Blog.astro',
pattern: '/[prefix]/[...page]',
prerender: true,
})
if (astroConfig.site) {
injectRoute({
entrypoint: 'starlight-blog/routes/rss',
pattern: '/[prefix]/rss.xml',
prerender: true,
})
}
updateConfig({
vite: {
plugins: [
vitePluginStarlightBlogConfig(config, {
defaultLocale: getDefaultLocale(starlightConfig as StarlightUserConfig),
description: starlightConfig.description,
site: astroConfig.site,
title: starlightConfig.title,
titleDelimiter: starlightConfig.titleDelimiter,
trailingSlash: astroConfig.trailingSlash,
}),
],
},
})
},
},
})
},
},
}
}
function overrideStarlightComponent(
components: StarlightUserConfig['components'],
logger: AstroIntegrationLogger,
component: keyof NonNullable<StarlightUserConfig['components']>,
) {
if (components?.[component]) {
logger.warn(`It looks like you already have a \`${component}\` component override in your Starlight configuration.`)
logger.warn(
`To use \`starlight-blog\`, either remove your override or update it to render the content from \`starlight-blog/overrides/${component}.astro\`.`,
)
return {}
}
return {
[component]: `starlight-blog/overrides/${component}.astro`,
}
}