Skip to content

Commit b1e79e6

Browse files
committed
feat: 🚀 auto-frontmatter 插件获取文件名时支持去掉序号
1 parent e5ee649 commit b1e79e6

File tree

1 file changed

+32
-14
lines changed
  • plugins/vitepress-plugin-auto-frontmatter/src

1 file changed

+32
-14
lines changed

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

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { normalizePath, Plugin } from "vite";
22
import { AutoFrontmatterOption } from "./types";
3-
import { basename, extname, join, relative, resolve } from "node:path";
3+
import { basename, join, relative, resolve } from "node:path";
44
import { glob } from "tinyglobby";
55
import matter from "gray-matter";
66
import { readFileSync, statSync, writeFileSync } from "node:fs";
@@ -29,14 +29,12 @@ export default function VitePluginVitePressAutoFrontmatter(
2929
// 基于文档源目录 srcDir 匹配
3030
pattern = pattern.map(p => normalizePath(join(srcDir, p)));
3131

32-
const filePaths = (
33-
await glob(pattern, {
34-
expandDirectories: false,
35-
...globOptions,
36-
absolute: true,
37-
ignore: ["**/node_modules/**", "**/dist/**", ...(globOptions?.ignore || [])],
38-
})
39-
).sort();
32+
const filePaths = await glob(pattern, {
33+
expandDirectories: false,
34+
...globOptions,
35+
absolute: true,
36+
ignore: ["**/node_modules/**", "**/dist/**", ...(globOptions?.ignore || [])],
37+
});
4038

4139
writeFrontmatterToFile(filePaths, option, srcDir);
4240
},
@@ -48,15 +46,14 @@ export default function VitePluginVitePressAutoFrontmatter(
4846
*
4947
* @param filePaths 文件路径列表
5048
* @param option 插件配置项
51-
* @param srcDir vp 配置项的 srcDir
49+
* @param srcDir vitepress 配置项的 srcDir
5250
*/
5351
const writeFrontmatterToFile = (filePaths: string[], option: AutoFrontmatterOption, srcDir: string) => {
5452
const { include, exclude, transform } = option;
5553

5654
for (const filePath of filePaths) {
5755
if (!filePath.endsWith(".md")) continue;
5856

59-
const filename = basename(filePath, extname(filePath));
6057
const fileContent = readFileSync(filePath, "utf-8");
6158
const { data: frontmatter, content } = matter(fileContent);
6259

@@ -69,7 +66,7 @@ const writeFrontmatterToFile = (filePaths: string[], option: AutoFrontmatterOpti
6966
let hasChange = false;
7067

7168
const addInfo: Record<string, any> = {
72-
title: filename,
69+
title: getMdFileTitle(basename(filePath)),
7370
date: statSync(filePath).birthtime,
7471
};
7572

@@ -108,7 +105,7 @@ export const checkExcludeAndInclude = (
108105
frontmatter: Record<string, any>,
109106
{ exclude, include }: AutoFrontmatterOption
110107
) => {
111-
// 存在 include 但是不存在 frontmatter,则代表改文件不符合 include 要求
108+
// 存在 include 但是不存在 frontmatter,则代表该文件不符合 include 要求
112109
if (include && !Object.keys(frontmatter).length) return false;
113110

114111
if (exclude || include) {
@@ -123,10 +120,31 @@ export const checkExcludeAndInclude = (
123120
return true;
124121
};
125122

123+
/**
124+
* 获取实际的文件名
125+
*
126+
* @param filename 文件名
127+
*/
128+
export const getMdFileTitle = (filename: string) => {
129+
let title = "";
130+
// 如果文件名带序号,如【1.xx.md】,则取 xx
131+
const fileNameArr = filename.split(".");
132+
133+
if (fileNameArr.length === 2) title = fileNameArr[0];
134+
else {
135+
// 处理多个 . 如 01.guile.md 的情况
136+
const firstDotIndex = filename.indexOf(".");
137+
const lastDotIndex = filename.lastIndexOf(".");
138+
title = filename.substring(firstDotIndex + 1, lastDotIndex);
139+
}
140+
141+
return title;
142+
};
143+
126144
/**
127145
* 获取文件信息
128146
*
129-
* @param srcDir vp 配置项的 srcDir
147+
* @param srcDir vitepress 配置项的 srcDir
130148
* @param filePath 文件路径
131149
*/
132150
export const getFileInfo = (srcDir: string, filePath: string) => {

0 commit comments

Comments
 (0)