Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme): add support for seo & sitemap plugin options #351

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/notes/theme/guide/功能/seo.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ export default defineUserConfig({
})
```

如需要自定义 SEO 优化,可以通过 `plugins.seo` 配置来实现。

```js
export default defineUserConfig({
theme: plumeTheme({
plugins: {
seo: {
// ... seo 配置
}
}
})
})
```

完整配置请查看 [文档](https://ecosystem.vuejs.press/zh/plugins/seo/seo/config.html)

::: note
示例 Fork 自 [@vuepress/plugin-seo](https://ecosystem.vuejs.press/zh/plugins/seo/seo/),
遵循 [MIT](https://github.com/vuepress/ecosystem/blob/main/LICENSE) 许可证。
Expand Down
16 changes: 16 additions & 0 deletions docs/notes/theme/guide/功能/sitemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ export default defineUserConfig({
})
})
```

如需要自定义 sitemap,可以通过 `plugins.sitemap` 配置来实现。

```js
export default defineUserConfig({
theme: plumeTheme({
plugins: {
sitemap: {
// ... sitemap 配置
}
}
})
})
```

完整配置请查看 [文档](https://ecosystem.vuejs.press/zh/plugins/seo/sitemap/config.html)
17 changes: 12 additions & 5 deletions theme/src/node/plugins/getPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { markdownMathPlugin } from '@vuepress/plugin-markdown-math'
import { nprogressPlugin } from '@vuepress/plugin-nprogress'
import { photoSwipePlugin } from '@vuepress/plugin-photo-swipe'
import { readingTimePlugin } from '@vuepress/plugin-reading-time'
import { seoPlugin } from '@vuepress/plugin-seo'
import { sitemapPlugin } from '@vuepress/plugin-sitemap'
import { seoPlugin, type SeoPluginOptions } from '@vuepress/plugin-seo'
import { sitemapPlugin, type SitemapPluginOptions } from '@vuepress/plugin-sitemap'
import { watermarkPlugin } from '@vuepress/plugin-watermark'
import { contentUpdatePlugin } from '@vuepress-plume/plugin-content-update'
import { fontsPlugin } from '@vuepress-plume/plugin-fonts'
Expand Down Expand Up @@ -154,12 +154,19 @@ export function getPlugins(
plugins.push(commentPlugin(pluginOptions.comment))
}

if (pluginOptions.sitemap !== false && hostname && isProd) {
plugins.push(sitemapPlugin({ hostname }))
if (pluginOptions.sitemap !== false && isProd) {
const sitemapOptions = isPlainObject(pluginOptions.sitemap) ? pluginOptions.sitemap : {}
sitemapOptions.hostname ||= hostname

if (sitemapOptions.hostname)
plugins.push(sitemapPlugin(sitemapOptions as SitemapPluginOptions))
}

if (pluginOptions.seo !== false && hostname && isProd) {
plugins.push(seoPlugin({ hostname }))
const seoOptions = isPlainObject(pluginOptions.seo) ? pluginOptions.seo : {}
seoOptions.hostname ||= hostname
if (seoOptions.hostname)
plugins.push(seoPlugin(seoOptions as SeoPluginOptions))
}

if (cache !== false) {
Expand Down
6 changes: 4 additions & 2 deletions theme/src/shared/options/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { DocSearchOptions } from '@vuepress/plugin-docsearch'
import type { MarkdownImagePluginOptions } from '@vuepress/plugin-markdown-image'
import type { MarkdownMathPluginOptions } from '@vuepress/plugin-markdown-math'
import type { ReadingTimePluginOptions } from '@vuepress/plugin-reading-time'
import type { SeoPluginOptions } from '@vuepress/plugin-seo'
import type { SitemapPluginOptions } from '@vuepress/plugin-sitemap'
import type { WatermarkPluginOptions } from '@vuepress/plugin-watermark'
import type { SearchPluginOptions } from '@vuepress-plume/plugin-search'
import type { ShikiPluginOptions } from '@vuepress-plume/plugin-shikiji'
Expand Down Expand Up @@ -69,9 +71,9 @@ export interface PlumeThemePluginOptions {

comment?: false | CommentPluginOptions

sitemap?: false
sitemap?: false | Omit<SitemapPluginOptions, 'hostname'> & { hostname?: string }

seo?: false
seo?: false | Omit<SeoPluginOptions, 'hostname'> & { hostname?: string }

/**
* 阅读时间、字数统计
Expand Down