@@ -52,7 +52,25 @@ const main = async (...args) => {
52
52
. map ( ( e , i ) => `${ i < 1 ? '' : array [ i + 1 ] ? ', ' : ' and ' } <a href="${ e . url } ">${ e . cpu . label } </a>` )
53
53
. join ( '' )
54
54
55
- const id = new Date ( date ) . toISOString ( )
55
+ const fs = require ( 'fs' )
56
+ const sections = fs
57
+ . readFileSync ( 'index.html' , 'utf-8' )
58
+ . split ( / ( < h 2 .+ < \/ h 2 > ) / )
59
+ if ( sections . length < 3 ) throw new Error ( `'index.html' is not in the expected format` )
60
+
61
+ const existingIDs = new Set (
62
+ sections
63
+ . filter ( ( e , i ) => ( i % 2 ) === 1 )
64
+ . map ( e => e . replace ( / ^ < h 2 i d = " ( [ ^ " ] + ) .* / , '$1' ) )
65
+ ) ;
66
+ const id = ( ( ) => {
67
+ const stamp = new Date ( date ) . toISOString ( )
68
+ if ( ! existingIDs . has ( stamp ) ) return stamp
69
+ for ( let i = 2 ; ; i ++ ) {
70
+ if ( ! existingIDs . has ( `${ stamp } -${ i } ` ) ) return `${ stamp } -${ i } `
71
+ }
72
+ } ) ( )
73
+
56
74
const insert = [
57
75
`<h2 id="${ id } ">` ,
58
76
date ,
@@ -69,12 +87,6 @@ const main = async (...args) => {
69
87
'</ul>'
70
88
] . join ( '' )
71
89
72
- const fs = require ( 'fs' )
73
- const sections = fs
74
- . readFileSync ( 'index.html' , 'utf-8' )
75
- . split ( / ( < h 2 .+ < \/ h 2 > ) / )
76
- if ( sections . length < 3 ) throw new Error ( `'index.html' is not in the expected format` )
77
-
78
90
sections [ 1 ] = `${ insert } \n\n${ sections [ 1 ] } `
79
91
fs . writeFileSync ( 'index.html' , sections . join ( '' ) )
80
92
}
0 commit comments