From 45d98731b48a6c73aa27f11a4995f2f3ad6e85b3 Mon Sep 17 00:00:00 2001 From: xugaoyi <894072666@qq.com> Date: Tue, 26 Oct 2021 14:51:09 +0800 Subject: [PATCH] v1.8.4 --- package.json | 2 +- theme-vdoing/node_utils/getSidebarData.js | 12 ++++----- theme-vdoing/node_utils/setFrontmatter.js | 30 +++++++++++------------ theme-vdoing/package.json | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 9af13f7e84..5510800298 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/theme-vdoing/node_utils/getSidebarData.js b/theme-vdoing/node_utils/getSidebarData.js index 2e4b5ed558..6c8979a8d7 100644 --- a/theme-vdoing/node_utils/getSidebarData.js +++ b/theme-vdoing/node_utils/getSidebarData.js @@ -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 || '' // 目录页对应的永久链接,用于给面包屑提供链接 diff --git a/theme-vdoing/node_utils/setFrontmatter.js b/theme-vdoing/node_utils/setFrontmatter.js index 36639bb916..9ad34b984e 100644 --- a/theme-vdoing/node_utils/setFrontmatter.js +++ b/theme-vdoing/node_utils/setFrontmatter.js @@ -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:{}, ...} - 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)}/` } diff --git a/theme-vdoing/package.json b/theme-vdoing/package.json index c813d8a001..cf0ecfaf03 100644 --- a/theme-vdoing/package.json +++ b/theme-vdoing/package.json @@ -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"