Skip to content

Commit

Permalink
feat(theme): ✨ enable and disable changelog and suggestion box
Browse files Browse the repository at this point in the history
BeiyanYunyi committed Sep 25, 2024

Verified

This commit was signed with the committer’s verified signature.
BeiyanYunyi 北雁云依
1 parent 7664a38 commit c3b826c
Showing 5 changed files with 47 additions and 14 deletions.
2 changes: 2 additions & 0 deletions example/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ const themeConfig: ThemeContext = {
include: ['campus', 'contributor-guide', 'fashion'],
nav,
sidebarOptions,
enableChangeLog: false,
enableSuggestionBox: false,
}

// https://vitepress.dev/reference/site-config
2 changes: 1 addition & 1 deletion src/Layout.vue
Original file line number Diff line number Diff line change
@@ -47,4 +47,4 @@ const { frontmatter } = useData()
--vp-font-family-base: sans-serif;
--vp-font-family-mono: monospace;
}
</style>
</style>
28 changes: 20 additions & 8 deletions src/components/AppFooter.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script setup lang="ts">
import { NolebaseGitChangelog } from '@nolebase/vitepress-plugin-git-changelog/client'
import { useRoute } from 'vitepress'
import { useData, useRoute } from 'vitepress'
import { ref, watch } from 'vue'
import { AppSBox } from '../components'
import type { PjtsThemeConfig } from '../config'
const route = useRoute()
const { theme } = useData<PjtsThemeConfig>()
// 定义一个 ref 来存储动态 key
const componentKey = ref(0)
@@ -19,22 +21,32 @@ function updateKeyAndFrontmatter() {
}
// 监听路由变化,更新 key 和 frontmatter
watch(() => route.path, () => {
isFrontmatterLoaded.value = false
updateKeyAndFrontmatter()
}, { immediate: true }) // 在组件挂载时立即执行一次,确保第一次渲染时 key 和 frontmatter 是正确的
watch(
() => route.path,
() => {
isFrontmatterLoaded.value = false
updateKeyAndFrontmatter()
},
{ immediate: true },
) // 在组件挂载时立即执行一次,确保第一次渲染时 key 和 frontmatter 是正确的
// 在组件挂载时更新 key 和 frontmatter
// onMounted(updateKeyAndFrontmatter);
</script>

<template>
<div :key="componentKey" class="vp-doc">
<h2 id="意见反馈">
<h2 v-if="theme.enableSuggestionBox" id="意见反馈">
意见反馈
</h2>
<AppSBox />
<AppSBox v-if="theme.enableSuggestionBox" />
<!-- 仅在 Frontmatter 加载完成且未设置 hideChangelog 时渲染 GitChangelog -->
<NolebaseGitChangelog v-if="isFrontmatterLoaded && !frontmatter.hideChangelog" />
<NolebaseGitChangelog
v-if="
isFrontmatterLoaded
&& theme.enableChangeLog
&& !frontmatter.hideChangelog
"
/>
</div>
</template>
27 changes: 22 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -10,11 +10,17 @@ import katex from 'markdown-it-katex'
import mdPangu from 'markdown-it-pangu'
import UnoCSS from 'unocss/vite'
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vitepress'
import type { DefaultTheme } from 'vitepress'
import { defineConfigWithTheme } from 'vitepress'

import { generateSidebar } from './sidebar'
import { useThemeContext } from './utils/themeContext'

export interface PjtsThemeConfig extends DefaultTheme.Config {
enableChangeLog?: boolean
enableSuggestionBox?: boolean
}

// 从文件系统读取 Markdown 文件内容
function readMarkdownFileContent(filePath: string): string {
if (fs.existsSync(filePath)) {
@@ -34,7 +40,8 @@ function countWords(content: string): number {
.replace(/\s+/g, ' ') // 将多余的空格归为一个空格
.trim() // 去除首尾空格

const chineseCharacters = cleanedContent.match(/[\u4E00-\u9FFF\uFF01-\uFFE5]/g) || []
const chineseCharacters
= cleanedContent.match(/[\u4E00-\u9FFF\uFF01-\uFFE5]/g) || []
const words = cleanedContent.split(/\s+/).filter(Boolean)

return chineseCharacters.length + words.length
@@ -43,9 +50,16 @@ function countWords(content: string): number {
// https://vitepress.dev/reference/site-config
function genConfig() {
const themeConfig = useThemeContext()
const { siteTitle, githubRepoLink, nav, siteLogo, SiteTitle}
= themeConfig
return defineConfig({
const {
siteTitle,
githubRepoLink,
nav,
siteLogo,
SiteTitle,
enableChangeLog = true,
enableSuggestionBox = true,
} = themeConfig
return defineConfigWithTheme<PjtsThemeConfig>({
lang: 'zh-CN',
title: siteTitle,
cleanUrls: true,
@@ -89,6 +103,8 @@ function genConfig() {
['meta', { property: 'og:site_name', content: siteTitle }],
],
themeConfig: {
enableSuggestionBox,
enableChangeLog,
// https://vitepress.dev/reference/default-theme-config
siteTitle: SiteTitle,
logo: siteLogo,
@@ -163,6 +179,7 @@ function genConfig() {
plugins: [
GitChangelog({
repoURL: githubRepoLink,
include: enableChangeLog ? ['** /*.md', '!node_modules'] : [],
}),
// GitChangelogMarkdownSection({
// sections: {
2 changes: 2 additions & 0 deletions src/utils/themeContext.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,8 @@ export interface ThemeContext {
include: string[]
nav: NavConfig
sidebarOptions: Options | Options[]
enableSuggestionBox?: boolean
enableChangeLog?: boolean
}

const themeContext = new AsyncLocalStorage<ThemeContext>()

0 comments on commit c3b826c

Please sign in to comment.