@@ -52,7 +52,25 @@ const main = async (...args) => {
5252 . map ( ( e , i ) => `${ i < 1 ? '' : array [ i + 1 ] ? ', ' : ' and ' } <a href="${ e . url } ">${ e . cpu . label } </a>` )
5353 . join ( '' )
5454
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+
5674 const insert = [
5775 `<h2 id="${ id } ">` ,
5876 date ,
@@ -69,12 +87,6 @@ const main = async (...args) => {
6987 '</ul>'
7088 ] . join ( '' )
7189
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-
7890 sections [ 1 ] = `${ insert } \n\n${ sections [ 1 ] } `
7991 fs . writeFileSync ( 'index.html' , sections . join ( '' ) )
8092}
0 commit comments