Skip to content

Commit 2adc9fe

Browse files
committed
fix: 🐞 vitepress-plugin-auto-frontmatter 插件修复 exclude 和 include 配置失效问题
1 parent a296f95 commit 2adc9fe

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

demo/docs-test/.vitepress/config.mts

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ const tkConfig = tkThemeConfig({
120120
},
121121
plugins: {
122122
autoFrontmatter: true,
123+
autoFrontmatterOption: {
124+
include: { a: true },
125+
},
123126
},
124127
});
125128

demo/docs-test/test1.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
date: 2025-03-02 19:34:49
33
title: test1
4-
permalink: /pages/24fb48
4+
permalink: /pages/8393b5
55
categories:
6-
-
6+
-
77
---
8+

demo/docs-test/目录1/t.md

-8
This file was deleted.

plugins/vitepress-plugin-auto-frontmatter/src/index.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ const writeFrontmatterToFile = (filePaths: string[], option: AutoFrontmatterOpti
6262

6363
if (frontmatter.layout === "home") continue;
6464

65-
for (const [key, value] of Object.entries(frontmatter)) {
66-
if (exclude && exclude[key] === value) continue;
67-
// 如果设置了 include,则只匹配 include
68-
if (include && include[key] !== value) continue;
69-
}
65+
const isPass = checkExcludeAndInclude(frontmatter, { exclude, include });
66+
if (!isPass) continue;
7067

7168
let tempFrontmatter = { ...frontmatter };
7269
let hasChange = false;
@@ -107,6 +104,25 @@ const writeFrontmatterToFile = (filePaths: string[], option: AutoFrontmatterOpti
107104
}
108105
};
109106

107+
export const checkExcludeAndInclude = (
108+
frontmatter: Record<string, any>,
109+
{ exclude, include }: AutoFrontmatterOption
110+
) => {
111+
// 存在 include 但是不存在 frontmatter,则代表改文件不符合 include 要求
112+
if (include && !Object.keys(frontmatter).length) return false;
113+
114+
if (exclude || include) {
115+
for (const [key, value] of Object.entries(frontmatter)) {
116+
// 如果设置了 exclude,则不匹配 exclude
117+
if (exclude?.[key] === value) return false;
118+
// 如果设置了 include,则只匹配 include
119+
if (include?.[key] !== value) return false;
120+
}
121+
}
122+
123+
return true;
124+
};
125+
110126
/**
111127
* 获取文件信息
112128
*

0 commit comments

Comments
 (0)