@@ -3,9 +3,13 @@ import axios from "axios";
33
44async function fetchCiTime ( filePath ) {
55 const url = `https://api.github.com/repos/tw93/weekly/commits?path=${ filePath } &page=1&per_page=1` ;
6- const response = await axios . get ( url ) ;
7- const ciTime = response . data [ 0 ] . commit . committer . date . split ( "T" ) [ 0 ] ;
8- return ciTime ;
6+ try {
7+ const response = await axios . get ( url ) ;
8+ const commitDate = response . data ?. [ 0 ] ?. commit ?. committer ?. date ;
9+ return commitDate ? commitDate . split ( "T" ) [ 0 ] : null ;
10+ } catch ( error ) {
11+ return null ;
12+ }
913}
1014
1115async function main ( ) {
@@ -16,8 +20,11 @@ async function main() {
1620 const mdFiles = files
1721 . filter ( ( file ) => file . endsWith ( ".md" ) )
1822 . sort ( ( a , b ) => {
19- const numA = parseInt ( a . match ( / ( \d + ) / ) [ 0 ] ) ;
20- const numB = parseInt ( b . match ( / ( \d + ) / ) [ 0 ] ) ;
23+ const matchA = a . match ( / ( \d + ) / ) ;
24+ const matchB = b . match ( / ( \d + ) / ) ;
25+ if ( ! matchA || ! matchB ) return 0 ;
26+ const numA = parseInt ( matchA [ 0 ] , 10 ) ;
27+ const numB = parseInt ( matchB [ 0 ] , 10 ) ;
2128 return numB - numA ;
2229 } ) ;
2330
@@ -36,7 +43,8 @@ async function main() {
3643 const title = `第 ${ num } 期 - ${ shortTitle } ` ;
3744
3845 // Read markdown file to extract cover image and description
39- const mdContent = await fs . readFile ( `./src/pages/posts/${ name } ` , "utf8" ) ;
46+ const fullPath = `./src/pages/posts/${ name } ` ;
47+ const mdContent = await fs . readFile ( fullPath , "utf8" ) ;
4048 const imgMatch = mdContent . match ( / < i m g \s + s r c = " ( [ ^ " ] + ) " / ) ;
4149 const pic = imgMatch ? imgMatch [ 1 ] : "" ;
4250
@@ -47,7 +55,9 @@ async function main() {
4755 readmeContent2 += `* [${ title } ](${ url } )\n` ;
4856
4957 if ( i < 5 ) {
50- const modified = await fetchCiTime ( `/src/pages/posts/${ filePath } ` ) ;
58+ const modified =
59+ ( await fetchCiTime ( `/src/pages/posts/${ filePath } ` ) ) ||
60+ new Date ( ( await fs . stat ( fullPath ) ) . mtime ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
5161 recentContent += `* [${ title } ](${ url } ) - ${ modified } \n` ;
5262 }
5363 }
0 commit comments