Skip to content

Commit

Permalink
feat: 支持自定义文档属性栏 (#12)
Browse files Browse the repository at this point in the history
### 可在 config 文件内配置以下选项:

- HideReadingTime: true, /* 隐藏字数和预计阅读时间 */
- HideLastUpdated: true, /* 隐藏最后更新时间 */
- HideAuthors: true, /* 隐藏作者信息 */
  • Loading branch information
Leetfs authored Oct 26, 2024
1 parent ce7327d commit 7d718e4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
29 changes: 16 additions & 13 deletions example/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import genConfig from '@project-trans/vitepress-theme-project-trans/config'
import type { SidebarOptions } from '@project-trans/vitepress-theme-project-trans/theme'
import type { ThemeContext } from '@project-trans/vitepress-theme-project-trans/utils'
import { withThemeContext } from '@project-trans/vitepress-theme-project-trans/utils'
import type { DefaultTheme } from 'vitepress'
import genConfig from '@project-trans/vitepress-theme-project-trans/config'
import { withThemeContext } from '@project-trans/vitepress-theme-project-trans/utils'

const nav: NavConfig = [
{
text: "大学指南",
link: "/campus/",
text: '大学指南',
link: '/campus/',
},
{
text: "贡献指南",
text: '贡献指南',
items: [
{
text: "校园版块投稿指南",
link: "/contributor-guide/campus.md",
text: '校园版块投稿指南',
link: '/contributor-guide/campus.md',
},
{
text: "其他投稿指南",
link: "/contributor-guide/other.md",
text: '其他投稿指南',
link: '/contributor-guide/other.md',
},
{
text: "校园版块贡献模板",
link: "/contributor-guide/CampusTemplate.md",
text: '校园版块贡献模板',
link: '/contributor-guide/CampusTemplate.md',
},
],
},
];
]

const baseConfig = {
useTitleFromFrontmatter: true,
Expand All @@ -42,7 +42,7 @@ const sidebarOptions = [
scanStartPath: 'campus',
resolvePath: '/campus/',
sortMenusByFrontmatterOrder: true,
}
},
]

const themeConfig: ThemeContext = {
Expand All @@ -59,6 +59,9 @@ const themeConfig: ThemeContext = {
sidebarOptions,
// enableChangeLog: false,
enableSuggestionBox: false,
// HideReadingTime: true, /* 隐藏字数和预计阅读时间 */
// HideLastUpdated: true, /* 隐藏最后更新时间 */
// HideAuthors: true, /* 隐藏作者信息 */
}

// https://vitepress.dev/reference/site-config
Expand Down
6 changes: 3 additions & 3 deletions src/components/PageInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ const displayAuthors = computed(() => {

<template>
<div class="mb-10 mt-4 flex flex-wrap gap-4">
<div class="inline-flex items-center gap-1">
<div v-if="!theme.HideAuthors" class="inline-flex items-center gap-1">
<span class="i-octicon:person" />
<span>作者:</span>
<span>{{ displayAuthors }}</span>
</div>

<div class="inline-flex items-center gap-1">
<div v-if="!theme.HideLastUpdated" class="inline-flex items-center gap-1">
<span class="i-octicon:calendar-16" />
<span>{{ theme.lastUpdated?.text || 'Last updated' }}:</span>
<time :datetime="isoDatetime">{{ datetime }}</time>
</div>
<ClientOnly>
<ReadingTime /> <!-- 添加 ReadingTime 组件 -->
<ReadingTime v-if="!theme.HideReadingTime" /> <!-- 添加 ReadingTime 组件 -->
</ClientOnly>
</div>
</template>
52 changes: 29 additions & 23 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { DefaultTheme } from 'vitepress'
import fs from 'node:fs'
import path, { dirname, resolve } from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'

import { fileURLToPath } from 'node:url'
import { GitChangelog } from '@nolebase/vitepress-plugin-git-changelog/vite'
import { transformHeadMeta } from '@nolebase/vitepress-plugin-meta'
import footnote from 'markdown-it-footnote'
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 type { DefaultTheme } from 'vitepress'
import { defineConfigWithTheme } from 'vitepress'

import { generateSidebar } from './sidebar'
Expand Down Expand Up @@ -61,51 +61,54 @@ function genConfig() {
enableSuggestionBox = true,
sitePattern,
org,
HideReadingTime,
HideLastUpdated,
HideAuthors,
} = themeConfig
const additionalNav = [
{
text: "切换字体",
text: '切换字体',
items: [
{
text: "黑体",
link: "#",
text: '黑体',
link: '#',
},
{
text: "宋体",
link: "#",
text: '宋体',
link: '#',
},
{
text: "更纱黑体",
link: "#",
text: '更纱黑体',
link: '#',
},
{
text: "思源宋体",
link: "#",
text: '思源宋体',
link: '#',
},
{
text: "霞鹜文楷",
link: "#",
text: '霞鹜文楷',
link: '#',
},
{
text: "霞鹜文楷 Mono",
link: "#",
text: '霞鹜文楷 Mono',
link: '#',
},
{
text: "霞鹜新晰黑",
link: "#",
text: '霞鹜新晰黑',
link: '#',
},
{
text: "新晰黑 Code",
link: "#",
text: '新晰黑 Code',
link: '#',
},
{
text: "默认字体",
link: "#",
text: '默认字体',
link: '#',
},
],
},
];
const combinedNav = [...nav, ...additionalNav];
]
const combinedNav = [...nav, ...additionalNav]
return defineConfigWithTheme<PjtsThemeConfig>({
lang: 'zh-CN',
title: siteTitle,
Expand Down Expand Up @@ -152,6 +155,9 @@ function genConfig() {
],
themeConfig: {
org,
HideReadingTime,
HideLastUpdated,
HideAuthors,
enableSuggestionBox,
enableChangeLog,
// https://vitepress.dev/reference/default-theme-config
Expand Down
5 changes: 4 additions & 1 deletion src/utils/themeContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AsyncLocalStorage } from 'node:async_hooks'
import type { DefaultTheme } from 'vitepress'
import type { generateSidebar, VitePressSidebarOptions } from 'vitepress-sidebar'
import { AsyncLocalStorage } from 'node:async_hooks'

type NavConfig = DefaultTheme.Config['nav']

Expand All @@ -19,6 +19,9 @@ export interface ThemeContext {
enableChangeLog?: boolean
sitePattern?: string
org?: string
HideReadingTime?: string
HideLastUpdated?: string
HideAuthors?: string
}

const themeContext = new AsyncLocalStorage<ThemeContext>()
Expand Down

0 comments on commit 7d718e4

Please sign in to comment.