优化:初步优化全局代码样式
This commit is contained in:
@@ -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}`;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user