feat: 添加extendFrontmatter配置项
This commit is contained in:
parent
b554f8f3d4
commit
6fed91182b
|
|
@ -9,8 +9,8 @@ import baiduCode from './config/baiduCode' // 百度统计hm码
|
|||
import htmlModules from './config/htmlModules' // 自定义插入的html块
|
||||
|
||||
export default defineConfig4CustomTheme<VdoingThemeConfig>({
|
||||
theme: 'vdoing', // 使用npm包主题
|
||||
// theme: resolve(__dirname, '../../vdoing'), // 使用本地主题
|
||||
// theme: 'vdoing', // 使用npm主题包
|
||||
theme: resolve(__dirname, '../../vdoing'), // 使用本地主题包
|
||||
|
||||
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>
|
||||
},
|
||||
|
||||
// 扩展自动生成frontmatter。 (不会覆盖已有的数据,仅在相应属性不存在时才会自动添加)
|
||||
extendFrontmatter: {
|
||||
test1: false,
|
||||
test2: 1,
|
||||
test3: '字符串',
|
||||
test4: [
|
||||
1, 2, 3
|
||||
],
|
||||
test5: {
|
||||
prop: 'abc',
|
||||
prop2: [{
|
||||
child: true,
|
||||
}]
|
||||
},
|
||||
test6: 1.9
|
||||
},
|
||||
|
||||
// 自定义hmtl(广告)模块
|
||||
htmlModules
|
||||
},
|
||||
|
|
|
|||
|
|
@ -15,12 +15,14 @@ const PREFIX = '/pages/'
|
|||
* 给.md文件设置frontmatter(标题、日期、永久链接等数据)
|
||||
*/
|
||||
function setFrontmatter(sourceDir, themeConfig) {
|
||||
|
||||
const isCategory = themeConfig.category
|
||||
const isTag = themeConfig.tag
|
||||
const categoryText = themeConfig.categoryText || '随笔'
|
||||
|
||||
const files = readFileList(sourceDir); // 读取所有md文件数据
|
||||
const { isCategory, isTag, categoryText = '随笔', extendFrontmatter } = themeConfig
|
||||
const files = readFileList(sourceDir) // 读取所有md文件数据
|
||||
// 扩展自定义生成frontmatter
|
||||
const extendFrontmatterStr = extendFrontmatter ?
|
||||
jsonToYaml.stringify(extendFrontmatter)
|
||||
.replace(/\n\s{2}/g, "\n")
|
||||
.replace(/"|---\n/g, "")
|
||||
: '';
|
||||
|
||||
files.forEach(file => {
|
||||
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 ? '' : `
|
||||
tags:
|
||||
- `;
|
||||
|
|
@ -61,50 +59,59 @@ tags:
|
|||
title: ${file.name}
|
||||
date: ${dateStr}
|
||||
permalink: ${getPermalink()}${file.filePath.indexOf('_posts') > -1 ? os.EOL + 'sidebar: auto' : ''}${cateStr}${tagsStr}
|
||||
---`;
|
||||
${extendFrontmatterStr}---`;
|
||||
|
||||
fs.writeFileSync(file.filePath, `${fmData}${os.EOL}${fileMatterObj.content}`); // 写入
|
||||
log(chalk.blue('tip ') + chalk.green(`write frontmatter(写入frontmatter):${file.filePath} `))
|
||||
|
||||
} else { // 已有FrontMatter
|
||||
const matterData = fileMatterObj.data;
|
||||
let mark = false;
|
||||
let matterData = fileMatterObj.data;
|
||||
let hasChange = false;
|
||||
|
||||
// 已有FrontMatter,但是没有title、date、permalink、categories、tags数据的
|
||||
if (!matterData.hasOwnProperty('title')) { // 标题
|
||||
matterData.title = file.name;
|
||||
mark = true;
|
||||
hasChange = true;
|
||||
}
|
||||
|
||||
if (!matterData.hasOwnProperty('date')) { // 日期
|
||||
const stat = fs.statSync(file.filePath);
|
||||
matterData.date = dateFormat(getBirthtime(stat));
|
||||
mark = true;
|
||||
hasChange = true;
|
||||
}
|
||||
|
||||
if (!matterData.hasOwnProperty('permalink')) { // 永久链接
|
||||
matterData.permalink = getPermalink();
|
||||
mark = true;
|
||||
hasChange = true;
|
||||
}
|
||||
|
||||
if (file.filePath.indexOf('_posts') > -1 && !matterData.hasOwnProperty('sidebar')) { // auto侧边栏,_posts文件夹特有
|
||||
matterData.sidebar = "auto";
|
||||
mark = true;
|
||||
hasChange = true;
|
||||
}
|
||||
|
||||
if (!matterData.hasOwnProperty('pageComponent') && matterData.article !== false) { // 是文章页才添加分类和标签
|
||||
if (isCategory !== false && !matterData.hasOwnProperty('categories')) { // 分类
|
||||
matterData.categories = getCategories(file, categoryText)
|
||||
mark = true;
|
||||
hasChange = true;
|
||||
}
|
||||
|
||||
if (isTag !== false && !matterData.hasOwnProperty('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') {
|
||||
matterData.date = repairDate(matterData.date) // 修复时间格式
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue