This commit is contained in:
xugaoyi 2021-10-26 14:51:09 +08:00
parent 03bbcada4f
commit 45d98731b4
4 changed files with 23 additions and 23 deletions

View File

@ -22,7 +22,7 @@
"vuepress-plugin-one-click-copy": "^1.0.2",
"vuepress-plugin-thirdparty-search": "^1.0.2",
"vuepress-plugin-zooming": "^1.1.7",
"vuepress-theme-vdoing": "^1.8.3",
"vuepress-theme-vdoing": "^1.8.4",
"yamljs": "^0.3.0"
},
"dependencies": {

View File

@ -11,7 +11,7 @@ let catalogueData = {}; // 目录页数据
* @param {String} sourceDir .md文件所在源目录(一般是docs目录)
* @param {Boolean} collapsable 是否可折叠
*/
function createSidebarData (sourceDir, collapsable) {
function createSidebarData(sourceDir, collapsable) {
const sidebarData = {};
const tocs = readTocs(sourceDir);
tocs.forEach(toc => { // toc是每个目录的绝对路径
@ -43,7 +43,7 @@ module.exports = createSidebarData;
* 读取指定目录下的文件绝对路径
* @param {String} root 指定的目录
*/
function readTocs (root) {
function readTocs(root) {
const result = [];
const files = fs.readdirSync(root); // 读取目录,返回数组成员是root底下所有的目录名 (包含文件夹和文件)
files.forEach(name => {
@ -60,7 +60,7 @@ function readTocs (root) {
* 将碎片化文章目录(_posts)映射为对应的侧边栏配置数据
* @param {String} root
*/
function mapTocToPostSidebar (root) {
function mapTocToPostSidebar(root) {
let postSidebar = [] // 碎片化文章数据
const files = fs.readdirSync(root); // 读取目录(文件和文件夹),返回数组
@ -85,7 +85,7 @@ function mapTocToPostSidebar (root) {
}
const contentStr = fs.readFileSync(file, 'utf8') // 读取md文件内容返回字符串
const { data } = matter(contentStr) // 解析出front matter数据
const { data } = matter(contentStr, {}) // 解析出front matter数据
const permalink = data.permalink || ''
if (data.title) {
title = data.title
@ -104,7 +104,7 @@ function mapTocToPostSidebar (root) {
* @param {String} prefix
*/
function mapTocToSidebar (root, collapsable, prefix = '') {
function mapTocToSidebar(root, collapsable, prefix = '') {
let sidebar = []; // 结构化文章侧边栏数据
const files = fs.readdirSync(root); // 读取目录(文件和文件夹),返回数组
@ -135,7 +135,7 @@ function mapTocToSidebar (root, collapsable, prefix = '') {
return;
}
const contentStr = fs.readFileSync(file, 'utf8') // 读取md文件内容返回字符串
const { data } = matter(contentStr) // 解析出front matter数据
const { data } = matter(contentStr, {}) // 解析出front matter数据
const permalink = data.permalink || ''
// 目录页对应的永久链接,用于给面包屑提供链接

View File

@ -7,14 +7,14 @@ const readFileList = require('./modules/readFileList');
const { type, repairDate, dateFormat } = require('./modules/fn');
const log = console.log
const path = require('path');
const os = require('os');
const PREFIX = '/pages/'
/**
* .md文件设置frontmatter(标题日期永久链接等数据)
*/
function setFrontmatter (sourceDir, themeConfig) {
function setFrontmatter(sourceDir, themeConfig) {
const isCategory = themeConfig.category
const isTag = themeConfig.tag
@ -26,7 +26,7 @@ function setFrontmatter (sourceDir, themeConfig) {
let dataStr = fs.readFileSync(file.filePath, 'utf8');// 读取每个md文件内容
// fileMatterObj => {content:'剔除frontmatter后的文件内容字符串', data:{<frontmatter对象>}, ...}
const fileMatterObj = matter(dataStr);
const fileMatterObj = matter(dataStr, {});
if (Object.keys(fileMatterObj.data).length === 0) { // 未定义FrontMatter数据
const stat = fs.statSync(file.filePath);
@ -40,18 +40,18 @@ function setFrontmatter (sourceDir, themeConfig) {
let cateLabelStr = '';
categories.forEach(item => {
cateLabelStr += '\r\n - ' + item
cateLabelStr += os.EOL + ' - ' + item
});
let cateStr = '';
if (!(isCategory === false)) {
cateStr = '\r\ncategories:' + cateLabelStr
cateStr = os.EOL + 'categories:' + cateLabelStr
};
// 注意下面这些反引号字符串的格式会映射到文件
// const cateStr = isCategory === false ? '' : `
// categories:
// - ${categories[0]}${categories[1] ? '\r\n - ' + categories[1] : ''}`;
// 注意下面这些反引号字符串的格式会映射到文件
// const cateStr = isCategory === false ? '' : `
// categories:
// - ${categories[0]}${categories[1] ? os.EOL + ' - ' + categories[1] : ''}`;
const tagsStr = isTag === false ? '' : `
tags:
@ -60,10 +60,10 @@ tags:
const fmData = `---
title: ${file.name}
date: ${dateStr}
permalink: ${getPermalink()}${file.filePath.indexOf('_posts') > -1 ? '\r\nsidebar: auto' : ''}${cateStr}${tagsStr}
permalink: ${getPermalink()}${file.filePath.indexOf('_posts') > -1 ? os.EOL + 'sidebar: auto' : ''}${cateStr}${tagsStr}
---`;
fs.writeFileSync(file.filePath, `${fmData}\r\n${fileMatterObj.content}`); // 写入
fs.writeFileSync(file.filePath, `${fmData}${os.EOL}${fileMatterObj.content}`); // 写入
log(chalk.blue('tip ') + chalk.green(`write frontmatter(写入frontmatter)${file.filePath} `))
} else { // 已有FrontMatter
@ -108,7 +108,7 @@ permalink: ${getPermalink()}${file.filePath.indexOf('_posts') > -1 ? '\r\nsideba
if (matterData.date && type(matterData.date) === 'date') {
matterData.date = repairDate(matterData.date) // 修复时间格式
}
const newData = jsonToYaml.stringify(matterData).replace(/\n\s{2}/g, "\n").replace(/"/g, "") + '---\r\n' + fileMatterObj.content;
const newData = jsonToYaml.stringify(matterData).replace(/\n\s{2}/g, "\n").replace(/"/g, "") + '---' + os.EOL + fileMatterObj.content;
fs.writeFileSync(file.filePath, newData); // 写入
log(chalk.blue('tip ') + chalk.green(`write frontmatter(写入frontmatter)${file.filePath} `))
}
@ -118,7 +118,7 @@ permalink: ${getPermalink()}${file.filePath.indexOf('_posts') > -1 ? '\r\nsideba
}
// 获取分类数据
function getCategories (file, categoryText) {
function getCategories(file, categoryText) {
let categories = []
if (file.filePath.indexOf('_posts') === -1) {
@ -139,13 +139,13 @@ function getCategories (file, categoryText) {
}
// 获取文件创建时间
function getBirthtime (stat) {
function getBirthtime(stat) {
// 在一些系统下无法获取birthtime属性的正确时间使用atime代替
return stat.birthtime.getFullYear() != 1970 ? stat.birthtime : stat.atime
}
// 定义永久链接数据
function getPermalink () {
function getPermalink() {
return `${PREFIX + (Math.random() + Math.random()).toString(16).slice(2, 8)}/`
}

View File

@ -1,6 +1,6 @@
{
"name": "vuepress-theme-vdoing",
"version": "1.8.3",
"version": "1.8.4",
"description": "Vdoing theme for VuePress. 一个基于VuePress的知识管理兼博客主题。",
"author": {
"name": "gaoyi(Evan) Xu"