@@ -15,76 +15,75 @@ const websitePath = process.env.WEBSITE_PATH;
15
15
const debug = process . env . DEBUG ;
16
16
const urls = new Set ( ) ;
17
17
18
- try {
19
- console . log ( `[DEBUG] Debug状态: ${ debug } ` )
20
- if ( debug ) {
21
- console . log ( `[DEBUG] 网站地图存放路径: ${ location } ` )
22
- console . log ( `[DEBUG] 网站基础链接: ${ basicLink } ` )
23
- console . log ( `[DEBUG] 网站文件存放路径: ${ websitePath } ` )
24
- console . log ( `[DEBUG] 页面文件类型: ${ fileTypes } ` )
25
- console . log ( `[DEBUG] 忽略的文件: ${ ignorePatterns } ` )
26
- }
27
- // -----------------
28
-
29
- // 通过 Git 命令,获取文件的最后提交日期
30
- function getLastCommitDate ( filePath ) {
31
- try {
32
- // 使用 git log 命令获取最后一次提交的时间
33
- const result = execFileSync ( 'git' , [ 'log' , '-1' , '--format=%cI' , '--' , filePath ] , { cwd : websitePath } ) ;
34
- const lastCommitDate = result . toString ( ) . trim ( ) ;
35
- return lastCommitDate
36
- } catch ( err ) {
37
- console . error ( `[ERROR] 获取 ${ filePath } 的最后提交时间失败: ` , err ) ;
38
- return '' ; // 出错时返回空字符串
39
- }
18
+ // 通过 Git 命令,获取文件的最后提交日期
19
+ function getLastCommitDate ( filePath ) {
20
+ try {
21
+ // 使用 git log 命令获取最后一次提交的时间
22
+ const result = execFileSync ( 'git' , [ 'log' , '-1' , '--format=%cI' , '--' , filePath ] , { cwd : websitePath } ) ;
23
+ return result . toString ( ) . trim ( ) ;
24
+ } catch ( err ) {
25
+ console . error ( `[ERROR] 获取 ${ filePath } 的最后提交时间失败: ` , err ) ;
26
+ return '' ; // 出错时返回空字符串
40
27
}
28
+ }
41
29
42
- // 扫描目录并生成 URL 列表
43
- function scanDirectory ( dir ) {
44
- const files = readdirSync ( dir ) ;
45
- files . forEach ( file => {
46
- const fullPath = path . join ( dir , file ) ;
47
- const stat = statSync ( fullPath ) ;
48
-
49
- // 如果是目录,递归扫描
50
- if ( stat . isDirectory ( ) ) {
51
- scanDirectory ( fullPath ) ;
52
- } else if ( fileTypes . includes ( path . extname ( file ) . slice ( 1 ) ) ) {
53
- const relativePath = path . relative ( websitePath , fullPath ) . replace ( / \\ / g, '/' ) ;
54
-
55
- // 如果当前路径在忽略列表中,则跳过
56
- if ( ignorePatterns . some ( pattern => {
57
- if ( relativePath . includes ( pattern ) ) {
58
- if ( debug ) {
59
- console . log ( `[DEBUG] 跳过文件 [${ fullPath } ] 因为其路径中包含 [${ pattern } ]` ) ;
60
- }
61
- return true ; // 如果找到了匹配的模式,返回 true,表示该文件应被忽略
30
+ // 扫描目录并生成 URL 列表
31
+ function scanDirectory ( dir ) {
32
+ const files = readdirSync ( dir ) ;
33
+ files . forEach ( file => {
34
+ const fullPath = path . join ( dir , file ) ;
35
+ const stat = statSync ( fullPath ) ;
36
+
37
+ // 如果是目录,递归扫描
38
+ if ( stat . isDirectory ( ) ) {
39
+ scanDirectory ( fullPath ) ;
40
+ } else if ( fileTypes . includes ( path . extname ( file ) . slice ( 1 ) ) ) {
41
+ const relativePath = path . relative ( websitePath , fullPath ) . replace ( / \\ / g, '/' ) ;
42
+
43
+ // 如果当前路径在忽略列表中,则跳过
44
+ if ( ignorePatterns . some ( pattern => {
45
+ if ( relativePath . includes ( pattern ) ) {
46
+ if ( debug ) {
47
+ console . log ( `[DEBUG] 跳过文件 [${ fullPath } ] 因为其路径中包含 [${ pattern } ]` ) ;
62
48
}
63
- return false ; // 如果没有找到匹配的模式,返回 false,继续检查下一个模式
64
- } ) ) {
65
- return ; // 如果前面 true 跳过此文件
49
+ return true ; // 如果找到了匹配的模式,返回 true,表示该文件应被忽略
66
50
}
51
+ return false ; // 如果没有找到匹配的模式,返回 false,继续检查下一个模式
52
+ } ) ) {
53
+ return ; // 如果前面 true 跳过此文件
54
+ }
67
55
68
- const lastmod = getLastCommitDate ( relativePath ) ; // 获取文件最后提交时间
69
- const encodedPath = encodeURIComponent ( relativePath ) . replace ( / % 2 F / g, '/' ) ; // 对路径进行编码并替换%2F为/
56
+ const lastmod = getLastCommitDate ( relativePath ) ; // 获取文件最后提交时间
57
+ const encodedPath = encodeURIComponent ( relativePath ) . replace ( / % 2 F / g, '/' ) ; // 对路径进行编码并替换%2F为/
70
58
71
- // 删除 URL 中的 `.md` 后缀
72
- const urlWithoutMd = encodedPath . replace ( / \. m d $ / , '' ) ;
59
+ // 删除 URL 中的 `.md` 后缀
60
+ const urlWithoutMd = encodedPath . replace ( / \. m d $ / , '' ) ;
73
61
74
- const fullUrl = `${ basicLink } /${ urlWithoutMd } ` ;
62
+ const fullUrl = `${ basicLink } /${ urlWithoutMd } ` ;
75
63
76
- // 只在获取到有效的 lastmod 时添加 <lastmod> 标签
77
- const urlTag = ` <url>\n <loc>${ fullUrl } </loc>` ;
78
- if ( lastmod ) {
79
- // 如果 lastmod 存在,添加 <lastmod>
80
- urls . add ( `${ urlTag } \n <lastmod>${ lastmod } </lastmod>\n </url>` ) ;
81
- } else {
82
- // 如果没有 lastmod,直接添加 <loc>
83
- urls . add ( `${ urlTag } \n </url>` ) ;
84
- }
64
+ // 只在获取到有效的 lastmod 时添加 <lastmod> 标签
65
+ const urlTag = ` <url>\n <loc>${ fullUrl } </loc>` ;
66
+ if ( lastmod ) {
67
+ // 如果 lastmod 存在,添加 <lastmod>
68
+ urls . add ( `${ urlTag } \n <lastmod>${ lastmod } </lastmod>\n </url>` ) ;
69
+ } else {
70
+ // 如果没有 lastmod,直接添加 <loc>
71
+ urls . add ( `${ urlTag } \n </url>` ) ;
85
72
}
86
- } ) ;
73
+ }
74
+ } ) ;
75
+ }
76
+
77
+ try {
78
+ console . log ( `[DEBUG] Debug状态: ${ debug } ` )
79
+ if ( debug ) {
80
+ console . log ( `[DEBUG] 网站地图存放路径: ${ location } ` )
81
+ console . log ( `[DEBUG] 网站基础链接: ${ basicLink } ` )
82
+ console . log ( `[DEBUG] 网站文件存放路径: ${ websitePath } ` )
83
+ console . log ( `[DEBUG] 页面文件类型: ${ fileTypes } ` )
84
+ console . log ( `[DEBUG] 忽略的文件: ${ ignorePatterns } ` )
87
85
}
86
+ // -----------------
88
87
89
88
scanDirectory ( websitePath ) ;
90
89
0 commit comments