feat: 添加extendFrontmatter配置项

This commit is contained in:
xugaoyi 2022-04-12 14:00:56 +08:00
parent b554f8f3d4
commit 6fed91182b
2 changed files with 47 additions and 23 deletions

View File

@ -9,8 +9,8 @@ import baiduCode from './config/baiduCode' // 百度统计hm码
import htmlModules from './config/htmlModules' // 自定义插入的html块 import htmlModules from './config/htmlModules' // 自定义插入的html块
export default defineConfig4CustomTheme<VdoingThemeConfig>({ export default defineConfig4CustomTheme<VdoingThemeConfig>({
theme: 'vdoing', // 使用npm包主题 // theme: 'vdoing', // 使用npm主题包
// theme: resolve(__dirname, '../../vdoing'), // 使用本地主题 theme: resolve(__dirname, '../../vdoing'), // 使用本地主题包
locales: { locales: {
'/': { '/': {
@ -196,6 +196,23 @@ export default defineConfig4CustomTheme<VdoingThemeConfig>({
'Evan Xu | <a href="https://github.com/xugaoyi/vuepress-theme-vdoing/blob/master/LICENSE" target="_blank">MIT License</a>', // 博客版权信息支持a标签或换行标签</br> 'Evan Xu | <a href="https://github.com/xugaoyi/vuepress-theme-vdoing/blob/master/LICENSE" target="_blank">MIT License</a>', // 博客版权信息支持a标签或换行标签</br>
}, },
// 扩展自动生成frontmatter。 (不会覆盖已有的数据,仅在相应属性不存在时才会自动添加)
extendFrontmatter: {
test1: false,
test2: 1,
test3: '字符串',
test4: [
1, 2, 3
],
test5: {
prop: 'abc',
prop2: [{
child: true,
}]
},
test6: 1.9
},
// 自定义hmtl(广告)模块 // 自定义hmtl(广告)模块
htmlModules htmlModules
}, },

View File

@ -15,12 +15,14 @@ const PREFIX = '/pages/'
* .md文件设置frontmatter(标题日期永久链接等数据) * .md文件设置frontmatter(标题日期永久链接等数据)
*/ */
function setFrontmatter(sourceDir, themeConfig) { function setFrontmatter(sourceDir, themeConfig) {
const { isCategory, isTag, categoryText = '随笔', extendFrontmatter } = themeConfig
const isCategory = themeConfig.category const files = readFileList(sourceDir) // 读取所有md文件数据
const isTag = themeConfig.tag // 扩展自定义生成frontmatter
const categoryText = themeConfig.categoryText || '随笔' const extendFrontmatterStr = extendFrontmatter ?
jsonToYaml.stringify(extendFrontmatter)
const files = readFileList(sourceDir); // 读取所有md文件数据 .replace(/\n\s{2}/g, "\n")
.replace(/"|---\n/g, "")
: '';
files.forEach(file => { files.forEach(file => {
let dataStr = fs.readFileSync(file.filePath, 'utf8');// 读取每个md文件内容 let dataStr = fs.readFileSync(file.filePath, 'utf8');// 读取每个md文件内容
@ -49,10 +51,6 @@ function setFrontmatter(sourceDir, themeConfig) {
}; };
// 注意下面这些反引号字符串的格式会映射到文件 // 注意下面这些反引号字符串的格式会映射到文件
// const cateStr = isCategory === false ? '' : `
// categories:
// - ${categories[0]}${categories[1] ? os.EOL + ' - ' + categories[1] : ''}`;
const tagsStr = isTag === false ? '' : ` const tagsStr = isTag === false ? '' : `
tags: tags:
- `; - `;
@ -61,50 +59,59 @@ tags:
title: ${file.name} title: ${file.name}
date: ${dateStr} date: ${dateStr}
permalink: ${getPermalink()}${file.filePath.indexOf('_posts') > -1 ? os.EOL + 'sidebar: auto' : ''}${cateStr}${tagsStr} permalink: ${getPermalink()}${file.filePath.indexOf('_posts') > -1 ? os.EOL + 'sidebar: auto' : ''}${cateStr}${tagsStr}
---`; ${extendFrontmatterStr}---`;
fs.writeFileSync(file.filePath, `${fmData}${os.EOL}${fileMatterObj.content}`); // 写入 fs.writeFileSync(file.filePath, `${fmData}${os.EOL}${fileMatterObj.content}`); // 写入
log(chalk.blue('tip ') + chalk.green(`write frontmatter(写入frontmatter)${file.filePath} `)) log(chalk.blue('tip ') + chalk.green(`write frontmatter(写入frontmatter)${file.filePath} `))
} else { // 已有FrontMatter } else { // 已有FrontMatter
const matterData = fileMatterObj.data; let matterData = fileMatterObj.data;
let mark = false; let hasChange = false;
// 已有FrontMatter但是没有title、date、permalink、categories、tags数据的 // 已有FrontMatter但是没有title、date、permalink、categories、tags数据的
if (!matterData.hasOwnProperty('title')) { // 标题 if (!matterData.hasOwnProperty('title')) { // 标题
matterData.title = file.name; matterData.title = file.name;
mark = true; hasChange = true;
} }
if (!matterData.hasOwnProperty('date')) { // 日期 if (!matterData.hasOwnProperty('date')) { // 日期
const stat = fs.statSync(file.filePath); const stat = fs.statSync(file.filePath);
matterData.date = dateFormat(getBirthtime(stat)); matterData.date = dateFormat(getBirthtime(stat));
mark = true; hasChange = true;
} }
if (!matterData.hasOwnProperty('permalink')) { // 永久链接 if (!matterData.hasOwnProperty('permalink')) { // 永久链接
matterData.permalink = getPermalink(); matterData.permalink = getPermalink();
mark = true; hasChange = true;
} }
if (file.filePath.indexOf('_posts') > -1 && !matterData.hasOwnProperty('sidebar')) { // auto侧边栏_posts文件夹特有 if (file.filePath.indexOf('_posts') > -1 && !matterData.hasOwnProperty('sidebar')) { // auto侧边栏_posts文件夹特有
matterData.sidebar = "auto"; matterData.sidebar = "auto";
mark = true; hasChange = true;
} }
if (!matterData.hasOwnProperty('pageComponent') && matterData.article !== false) { // 是文章页才添加分类和标签 if (!matterData.hasOwnProperty('pageComponent') && matterData.article !== false) { // 是文章页才添加分类和标签
if (isCategory !== false && !matterData.hasOwnProperty('categories')) { // 分类 if (isCategory !== false && !matterData.hasOwnProperty('categories')) { // 分类
matterData.categories = getCategories(file, categoryText) matterData.categories = getCategories(file, categoryText)
mark = true; hasChange = true;
} }
if (isTag !== false && !matterData.hasOwnProperty('tags')) { // 标签 if (isTag !== false && !matterData.hasOwnProperty('tags')) { // 标签
matterData.tags = ['']; matterData.tags = [''];
mark = true; hasChange = true;
} }
} }
if (mark) { // 扩展自动生成frontmatter的字段
if (type(extendFrontmatter) === 'object') {
Object.keys(extendFrontmatter).forEach(keyName => {
if (!matterData.hasOwnProperty(keyName)) {
matterData[keyName] = extendFrontmatter[keyName]
hasChange = true;
}
})
}
if (hasChange) {
if (matterData.date && type(matterData.date) === 'date') { if (matterData.date && type(matterData.date) === 'date') {
matterData.date = repairDate(matterData.date) // 修复时间格式 matterData.date = repairDate(matterData.date) // 修复时间格式
} }