重构:拆分配置文件

This commit is contained in:
2022-07-20 22:29:09 +08:00
parent eb0420c32b
commit ca9169fddf
9 changed files with 187 additions and 138 deletions

View File

@@ -1,5 +1,5 @@
{ {
"name": "MyRepos", "name": "my-repos",
"version": "1.0.0", "version": "1.0.0",
"description": "Charles's personal knowledge repositories.", "description": "Charles's personal knowledge repositories.",
"main": "index.js", "main": "index.js",
@@ -12,7 +12,10 @@
}, },
"devDependencies": { "devDependencies": {
"vitepress": "^1.0.0-alpha.4", "vitepress": "^1.0.0-alpha.4",
"vue": "^3.2.37", "vue": "^3.2.37"
"fast-glob": "^3.2.11" },
"dependencies": {
"fast-glob": "^3.2.11",
"element-plus": "^2.2.9"
} }
} }

View File

@@ -1,142 +1,21 @@
import { defineConfig, type DefaultTheme } from 'vitepress' import { defineConfig } from 'vitepress'
import { sync } from "fast-glob" import { metaData } from './config/constants'
import { head } from './config/head'
import { themeConfig } from './config/theme'
export default defineConfig({ export default defineConfig({
lang: 'zh-CN', lang: metaData.lang,
title: '查尔斯的知识库', title: metaData.title,
description: '个人知识库,记录 & 分享个人碎片化、结构化、体系化的知识内容。', description: metaData.description,
head, // <head>内标签配置
themeConfig, // 主题配置
lastUpdated: true, // 显示最后更新时间 lastUpdated: true, // 显示最后更新时间
// <head>内标签配置
head: [
['link', { rel: 'icon', href: '/favicon.ico' }]
],
// Markdown配置 // Markdown配置
markdown: { markdown: {
theme: 'material-palenight', theme: 'one-dark-pro', // Shiki主题, 所有主题参见: https://github.com/shikijs/shiki/blob/main/docs/themes.md
// lineNumbers: true lineNumbers: true // 启用行号
},
// 主题配置
themeConfig: {
logo: '/logo.jpg',
outlineTitle: '目录', // 右侧边栏的大纲标题文本
// 编辑链接
editLink: {
pattern: 'https://github.com/Charles7c/charles7c.github.io/edit/main/repos/:path',
text: '在 GitHub 上编辑此页面'
},
lastUpdatedText: '最后更新', // 最后更新时间文本, 需配置 lastUpdated 为 true
docFooter: {
prev: '上一页',
next: '下一页'
},
// 版权标识
footer: {
message: 'Released under the MIT License.',
copyright: 'Copyright © 2019-present Charles7c'
},
// 社交链接
socialLinks: [
{ icon: 'github', link: 'https://github.com/Charles7c/charles7c.github.io' }
],
// 全文搜索
algolia: {
appId: 'DBZ0G9HBUY',
apiKey: '00cef480a543003d05d9808110ea5f65',
indexName: 'charles7c'
},
// 导航栏
nav: nav(),
// 侧边栏
sidebar: {
'/issues/': getItems("issues"),
'/fragments/': getItems("fragments"),
'/about/': [
{
items: [
{ text: '关于笔者', link: '/about/me' }
]
}
]
}
} }
}) })
/**
* 获取导航栏
*/
function nav() {
return [
{
text: 'Bug万象集',
link: '/issues/index'
},
{
text: '"杂碎"逆袭史',
link: '/fragments/index'
},
{
text: '关于知识库',
link: '/about/index'
}
]
}
/**
* 获取侧边栏分组及分组下标题
*
* @param path 扫描基础路径
* @returns {DefaultTheme.SidebarGroup[]}
*/
function getItems (path: string) {
// 侧边栏分组数组
let groups: DefaultTheme.SidebarGroup[] = []
// 侧边栏分组下标题数组
let items: DefaultTheme.SidebarItem[] = []
// 1.获取所有年份目录
sync(`repos/${path}/*`, {
onlyDirectories: true,
objectMode: true,
}).forEach(({ name }) => {
let year = name
for (let i = 1; i <= 12; i++) {
let month = i < 10 ? `0${i}` : i
// 2.获取所有月份目录下的文章
sync(`repos/${path}/${year}/${month}/*`, {
objectMode: true,
}).forEach(({ name }) => {
// 向前追加标题
items.unshift({
text: name,
link: `/${path}/${year}/${month}/${name}`,
})
})
// 3.向前追加分组
if (items.length > 0) {
// 去除标题名中的日期前缀和扩展名
for (let i = 0; i < items.length; i++) {
let text = items[i].text
items[i].text = text.replace('.md', '').substring(text.indexOf('.') + 1)
}
groups.unshift({
text: `${year}${month}月 (${items.length}篇)`,
collapsible: true,
collapsed: true,
items: items
})
}
// 4.清空侧边栏分组下标题数组
items = []
}
})
// 5.将第一个侧边栏分组的标题展开
groups[0].collapsed = false
return groups
}

View File

@@ -0,0 +1,10 @@
const site = 'https://blog.charles7c.top'
export const metaData = {
lang: 'zh-CN',
locale: 'zh_CN',
title: '查尔斯的知识库',
description: '个人知识库,记录 & 分享个人碎片化、结构化、体系化的知识内容。',
site,
image: `${site}/logo.jpg`
}

View File

@@ -0,0 +1,19 @@
import type { HeadConfig } from 'vitepress'
import { metaData } from './constants'
export const head: HeadConfig[] = [
['link', { rel: 'icon', href: '/favicon.ico' }],
['meta', { name: 'author', content: 'Charles7c' }],
['meta', { name: 'keywords', content: '查尔斯的知识库, 知识库, 博客, Charles7c' }],
['meta', { name: 'HandheldFriendly', content: 'True' }],
['meta', { name: 'MobileOptimized', content: '320' }],
['meta', { property: 'og:type', content: 'website' }],
['meta', { property: 'og:locale', content: metaData.locale }],
['meta', { property: 'og:title', content: metaData.title }],
['meta', { property: 'og:description', content: metaData.description }],
['meta', { property: 'og:site', content: metaData.site }],
['meta', { property: 'og:site_name', content: metaData.title }],
['meta', { property: 'og:image', content: metaData.image }]
]

View File

@@ -0,0 +1,19 @@
import type { DefaultTheme } from 'vitepress'
export const nav: DefaultTheme.Config['nav'] = [
{
text: 'Bug万象集',
link: '/issues/index',
activeMatch: '/issues/' // 当前页面处于匹配路径下时, 对应导航菜单将突出显示
},
{
text: '"杂碎"逆袭史',
link: '/fragments/index',
activeMatch: '/fragments/'
},
{
text: '关于知识库',
link: '/about/index',
activeMatch: '/about/',
}
]

View File

@@ -0,0 +1,70 @@
import type { DefaultTheme } from 'vitepress'
import { sync } from "fast-glob"
export const sidebar: DefaultTheme.Config['sidebar'] = {
'/issues/': getItems("issues"),
'/fragments/': getItems("fragments"),
'/about/': [
{
items: [
{ text: '关于笔者', link: '/about/me' }
]
}
]
}
/**
* 获取侧边栏分组及分组下标题
*
* @param path 扫描基础路径
* @returns {DefaultTheme.SidebarGroup[]}
*/
function getItems (path: string) {
// 侧边栏分组数组
let groups: DefaultTheme.SidebarGroup[] = []
// 侧边栏分组下标题数组
let items: DefaultTheme.SidebarItem[] = []
// 1.获取所有年份目录
sync(`repos/${path}/*`, {
onlyDirectories: true,
objectMode: true,
}).forEach(({ name }) => {
let year = name
for (let i = 1; i <= 12; i++) {
let month = i < 10 ? `0${i}` : i
// 2.获取所有月份目录下的文章
sync(`repos/${path}/${year}/${month}/*`, {
objectMode: true,
}).forEach(({ name }) => {
// 向前追加标题
items.unshift({
text: name,
link: `/${path}/${year}/${month}/${name}`,
})
})
// 3.向前追加分组
if (items.length > 0) {
// 去除标题名中的日期前缀和扩展名
for (let i = 0; i < items.length; i++) {
let text = items[i].text
items[i].text = text.replace('.md', '').substring(text.indexOf('.') + 1)
}
groups.unshift({
text: `${year}${month}月 (${items.length}篇)`,
collapsible: true,
collapsed: true,
items: items
})
}
// 4.清空侧边栏分组下标题数组
items = []
}
})
// 5.将第一个侧边栏分组的标题展开
groups[0].collapsed = false
return groups
}

View File

@@ -0,0 +1,35 @@
import type { DefaultTheme } from 'vitepress'
import { nav } from './nav'
import { sidebar } from './sidebar'
export const themeConfig: DefaultTheme.Config = {
logo: '/logo.jpg',
outlineTitle: '目录', // 右侧边栏的大纲标题文本
lastUpdatedText: '最后更新', // 最后更新时间文本, 需配置 lastUpdated 为 true
docFooter: {
prev: '上一页',
next: '下一页'
},
// 编辑链接
editLink: {
pattern: 'https://github.com/Charles7c/charles7c.github.io/edit/main/repos/:path',
text: '在 GitHub 上编辑此页面'
},
// 版权标识
footer: {
message: 'Released under the MIT License.',
copyright: `Copyright © 2019-${new Date().getFullYear()} Charles7c`
},
// 全文搜索
algolia: {
appId: 'DBZ0G9HBUY',
apiKey: '00cef480a543003d05d9808110ea5f65',
indexName: 'charles7c'
},
// 社交链接
socialLinks: [
{ icon: 'github', link: 'https://github.com/Charles7c/charles7c.github.io' }
],
nav, // 导航栏
sidebar // 侧边栏
}

View File

@@ -0,0 +1,5 @@
.vp-doc h2 {
margin: 15px 0 16px;
padding-top: 10px;
border-top: none;
}

View File

@@ -0,0 +1,9 @@
import DefaultTheme from 'vitepress/theme'
import './custom.css'
export default {
...DefaultTheme,
enhanceApp({ app }) {
// register global components
}
}