1
1
import { normalizePath , Plugin } from "vite" ;
2
2
import { AutoFrontmatterOption } from "./types" ;
3
- import { basename , extname , join , relative , resolve } from "node:path" ;
3
+ import { basename , join , relative , resolve } from "node:path" ;
4
4
import { glob } from "tinyglobby" ;
5
5
import matter from "gray-matter" ;
6
6
import { readFileSync , statSync , writeFileSync } from "node:fs" ;
@@ -29,14 +29,12 @@ export default function VitePluginVitePressAutoFrontmatter(
29
29
// 基于文档源目录 srcDir 匹配
30
30
pattern = pattern . map ( p => normalizePath ( join ( srcDir , p ) ) ) ;
31
31
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
+ } ) ;
40
38
41
39
writeFrontmatterToFile ( filePaths , option , srcDir ) ;
42
40
} ,
@@ -48,15 +46,14 @@ export default function VitePluginVitePressAutoFrontmatter(
48
46
*
49
47
* @param filePaths 文件路径列表
50
48
* @param option 插件配置项
51
- * @param srcDir vp 配置项的 srcDir
49
+ * @param srcDir vitepress 配置项的 srcDir
52
50
*/
53
51
const writeFrontmatterToFile = ( filePaths : string [ ] , option : AutoFrontmatterOption , srcDir : string ) => {
54
52
const { include, exclude, transform } = option ;
55
53
56
54
for ( const filePath of filePaths ) {
57
55
if ( ! filePath . endsWith ( ".md" ) ) continue ;
58
56
59
- const filename = basename ( filePath , extname ( filePath ) ) ;
60
57
const fileContent = readFileSync ( filePath , "utf-8" ) ;
61
58
const { data : frontmatter , content } = matter ( fileContent ) ;
62
59
@@ -69,7 +66,7 @@ const writeFrontmatterToFile = (filePaths: string[], option: AutoFrontmatterOpti
69
66
let hasChange = false ;
70
67
71
68
const addInfo : Record < string , any > = {
72
- title : filename ,
69
+ title : getMdFileTitle ( basename ( filePath ) ) ,
73
70
date : statSync ( filePath ) . birthtime ,
74
71
} ;
75
72
@@ -108,7 +105,7 @@ export const checkExcludeAndInclude = (
108
105
frontmatter : Record < string , any > ,
109
106
{ exclude, include } : AutoFrontmatterOption
110
107
) => {
111
- // 存在 include 但是不存在 frontmatter,则代表改文件不符合 include 要求
108
+ // 存在 include 但是不存在 frontmatter,则代表该文件不符合 include 要求
112
109
if ( include && ! Object . keys ( frontmatter ) . length ) return false ;
113
110
114
111
if ( exclude || include ) {
@@ -123,10 +120,31 @@ export const checkExcludeAndInclude = (
123
120
return true ;
124
121
} ;
125
122
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
+
126
144
/**
127
145
* 获取文件信息
128
146
*
129
- * @param srcDir vp 配置项的 srcDir
147
+ * @param srcDir vitepress 配置项的 srcDir
130
148
* @param filePath 文件路径
131
149
*/
132
150
export const getFileInfo = ( srcDir : string , filePath : string ) => {
0 commit comments