优化:初步优化全局代码样式

This commit is contained in:
2023-02-25 20:36:27 +08:00
parent a0358e1079
commit 57e1051a01
26 changed files with 433 additions and 453 deletions

View File

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

View File

@@ -1,5 +1,5 @@
import type { HeadConfig } from 'vitepress'
import { metaData } from './constants'
import type { HeadConfig } from 'vitepress';
import { metaData } from './constants';
export const head: HeadConfig[] = [
['link', { rel: 'icon', href: '/favicon.ico' }],
@@ -56,4 +56,4 @@ export const head: HeadConfig[] = [
xhr.open('GET', 'https://api.charles7c.top/blog/pv?pageUrl=' + location.href);
xhr.send();
}`]
]
];

View File

@@ -1,4 +1,4 @@
import type { MarkdownOptions } from 'vitepress'
import type { MarkdownOptions } from 'vitepress';
export const markdown: MarkdownOptions = {
// Shiki主题, 所有主题参见: https://github.com/shikijs/shiki/blob/main/docs/themes.md
@@ -11,9 +11,9 @@ export const markdown: MarkdownOptions = {
// 在所有文档的<h1>标签后添加<ArticleMetadata/>组件
config: (md) => {
md.renderer.rules.heading_close = (tokens, idx, options, env, slf) => {
let htmlResult = slf.renderToken(tokens, idx, options, env, slf)
if (tokens[idx].tag === 'h1') htmlResult += `\n<ClientOnly><ArticleMetadata v-if="($frontmatter?.aside ?? true) && ($frontmatter?.showArticleMetadata ?? true)" :article="$frontmatter" /></ClientOnly>`
return htmlResult
let htmlResult = slf.renderToken(tokens, idx, options);
if (tokens[idx].tag === 'h1') htmlResult += `\n<ClientOnly><ArticleMetadata v-if="($frontmatter?.aside ?? true) && ($frontmatter?.showArticleMetadata ?? true)" :article="$frontmatter" /></ClientOnly>`;
return htmlResult;
}
}
}
},
};

View File

@@ -1,4 +1,4 @@
import DefaultTheme from 'vitepress/theme'
import type { DefaultTheme } from 'vitepress';
export const nav: DefaultTheme.Config['nav'] = [
{
@@ -37,5 +37,5 @@ export const nav: DefaultTheme.Config['nav'] = [
{ text: '关于我', link: '/about/me', activeMatch: '/about/me' }
],
activeMatch: '/about/' // // 当前页面处于匹配路径下时, 对应导航菜单将突出显示
}
]
},
];

View File

@@ -1,6 +1,6 @@
import DefaultTheme from 'vitepress/theme'
import { sync } from 'fast-glob'
import matter from 'gray-matter'
import type { DefaultTheme } from 'vitepress';
import { sync } from 'fast-glob';
import * as matter from 'gray-matter';
export const sidebar: DefaultTheme.Config['sidebar'] = {
'/categories/issues/': getItemsByDate("categories/issues"),
@@ -10,7 +10,7 @@ export const sidebar: DefaultTheme.Config['sidebar'] = {
'/courses/java/': getItems("courses/java"),
'/courses/mysql/': getItems("courses/mysql"),
'/courses/mybatis/': getItems("courses/mybatis")
'/courses/mybatis/': getItems("courses/mybatis"),
}
/**
@@ -19,57 +19,56 @@ export const sidebar: DefaultTheme.Config['sidebar'] = {
* /categories/issues/2022/07/20/xxx.md
*
* @param path 扫描基础路径
* @returns {DefaultTheme.SidebarGroup[]}
* @returns {DefaultTheme.SidebarItem[]}
*/
function getItemsByDate (path: string) {
// 侧边栏年份分组数组
let yearGroups: DefaultTheme.SidebarGroup[] = []
let yearGroups: DefaultTheme.SidebarItem[] = [];
// 置顶数组
let topArticleItems: DefaultTheme.SidebarItem[] = []
let topArticleItems: DefaultTheme.SidebarItem[] = [];
// 1.获取所有年份目录
sync(`docs/${path}/*`, {
onlyDirectories: true,
objectMode: true
objectMode: true,
}).forEach(({ name }) => {
let year = name
let year = name;
// 年份数组
let articleItems: DefaultTheme.SidebarItem[] = []
let articleItems: DefaultTheme.SidebarItem[] = [];
// 2.获取所有月份目录
sync(`docs/${path}/${year}/*`, {
onlyDirectories: true,
objectMode: true
objectMode: true,
}).forEach(({ name }) => {
let month = name
// 3.获取所有日期目录
sync(`docs/${path}/${year}/${month}/*`, {
onlyDirectories: true,
objectMode: true
objectMode: true,
}).forEach(({ name }) => {
let day = name
let day = name;
// 4.获取日期目录下的所有文章
sync(`docs/${path}/${year}/${month}/${day}/*`, {
onlyFiles: true,
objectMode: true
objectMode: true,
}).forEach((article) => {
const articleFile = matter.read(`${article.path}`)
const { data } = articleFile
const articleFile = matter.read(`${article.path}`);
const { data } = articleFile;
if (data.isTop) {
// 向置顶分组前追加标题
topArticleItems.unshift({
text: data.title,
link: `/${path}/${year}/${month}/${day}/${article.name.replace('.md', '')}`
})
link: `/${path}/${year}/${month}/${day}/${article.name.replace('.md', '')}`,
});
}
// 向年份分组前追加标题
articleItems.unshift({
text: data.title,
link: `/${path}/${year}/${month}/${day}/${article.name.replace('.md', '')}`
})
link: `/${path}/${year}/${month}/${day}/${article.name.replace('.md', '')}`,
});
})
})
})
@@ -77,29 +76,29 @@ function getItemsByDate (path: string) {
// 添加年份分组
yearGroups.unshift({
text: `${year}年 (${articleItems.length}篇)`,
items: articleItems,
collapsed: true,
items: articleItems
})
});
})
if (topArticleItems.length > 0) {
// 添加置顶分组
yearGroups.unshift({
text: `📑 我的置顶 (${topArticleItems.length}篇)`,
items: topArticleItems,
collapsed: false,
items: topArticleItems
})
});
// 将最近年份分组展开
yearGroups[1].collapsed = false
yearGroups[1].collapsed = false;
} else {
// 将最近年份分组展开
yearGroups[0].collapsed = false
yearGroups[0].collapsed = false;
}
// 添加序号
addOrderNumber(yearGroups)
return yearGroups
addOrderNumber(yearGroups);
return yearGroups;
}
/**
@@ -108,54 +107,54 @@ function getItemsByDate (path: string) {
* courses/mybatis/01-MyBatis基础/01-xxx.md
*
* @param path 扫描基础路径
* @returns {DefaultTheme.SidebarGroup[]}
* @returns {DefaultTheme.SidebarItem[]}
*/
function getItems (path: string) {
// 侧边栏分组数组
let groups: DefaultTheme.SidebarGroup[] = []
let groups: DefaultTheme.SidebarItem[] = [];
// 侧边栏分组下标题数组
let items: DefaultTheme.SidebarItem[] = []
let total = 0
let items: DefaultTheme.SidebarItem[] = [];
let total = 0;
// 当分组内文章数量少于 2 篇或文章总数显示超过 20 篇时,自动折叠分组
const groupCollapsedSize = 2
const titleCollapsedSize = 20
const groupCollapsedSize = 2;
const titleCollapsedSize = 20;
// 1.获取所有分组目录
sync(`docs/${path}/*`, {
onlyDirectories: true,
objectMode: true
objectMode: true,
}).forEach(({ name }) => {
let groupName = name
let groupName = name;
// 2.获取分组下的所有文章
sync(`docs/${path}/${groupName}/*`, {
onlyFiles: true,
objectMode: true
objectMode: true,
}).forEach((article) => {
const articleFile = matter.read(`${article.path}`)
const { data } = articleFile
const articleFile = matter.read(`${article.path}`);
const { data } = articleFile;
// 向前追加标题
items.push({
text: data.title,
link: `/${path}/${groupName}/${article.name.replace('.md', '')}`
})
total ++
link: `/${path}/${groupName}/${article.name.replace('.md', '')}`,
});
total += 1;
})
// 3.向前追加到分组
// 当分组内文章数量少于 A 篇或文章总数显示超过 B 篇时,自动折叠分组
groups.push({
text: `${groupName.substring(groupName.indexOf('-') + 1)} (${items.length}篇)`,
items: items,
collapsed: items.length < groupCollapsedSize || total > titleCollapsedSize,
items: items
})
// 4.清空侧边栏分组下标题数组
items = []
items = [];
})
// 添加序号
addOrderNumber(groups)
return groups
addOrderNumber(groups);
return groups;
}
/**
@@ -164,10 +163,10 @@ function getItems (path: string) {
* @param groups 分组数据
*/
function addOrderNumber(groups) {
for (var i = 0; i < groups.length; i++) {
for (var j = 0; j < groups[i].items.length; j++) {
var items = groups[i].items
items[j].text = `[${j + 1}] ${items[j].text}`
for (let i = 0; i < groups.length; i++) {
for (let j = 0; j < groups[i].items.length; j++) {
const items = groups[i].items;
items[j].text = `[${j + 1}] ${items[j].text}`;
}
}
}

View File

@@ -1,15 +1,20 @@
import DefaultTheme from 'vitepress/theme'
import { nav } from './nav'
import { sidebar } from './sidebar'
import type { DefaultTheme } from 'vitepress';
import { nav } from './nav';
import { sidebar } from './sidebar';
export const themeConfig: DefaultTheme.Config = {
nav, // 导航栏配置
sidebar, // 侧边栏配置
logo: '/logo.png',
outline: 'deep', // 右侧大纲标题层级
outlineTitle: '目录', // 右侧大纲标题文本配置
outline: {
level: 'deep', // 右侧大纲标题层级
label: '目录', // 右侧大纲标题文本配置
},
outlineBadges: false, // 是否在大纲中显示 Badge 文本
darkModeSwitchLabel: '切换日光/暗黑模式',
sidebarMenuLabel: '文章',
returnToTopLabel: '返回顶部',
lastUpdatedText: '最后更新', // 最后更新时间文本配置, 需先配置lastUpdated为true
// 文档页脚文本配置
docFooter: {
@@ -60,10 +65,11 @@ export const themeConfig: DefaultTheme.Config = {
],
// 自定义扩展: 文章元数据配置
// @ts-ignore
articleMetadataConfig: {
author: '查尔斯', // 文章全局默认作者名称
authorLink: '/about/me', // 点击作者名时默认跳转的链接
showViewCount: true // 是否显示文章阅读数, 需要在 docs/.vitepress/theme/api/config.js 及 interface.js 配置好相应 API 接口
showViewCount: true, // 是否显示文章阅读数, 需要在 docs/.vitepress/theme/api/config.js 及 interface.js 配置好相应 API 接口
},
// 自定义扩展: 文章版权配置
copyrightConfig: {