Skip to content

Commit 12594d0

Browse files
committed
add-entry: ensure that the IDs are unique
Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4074163 commit 12594d0

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

add-entry.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/(<h2 .+<\/h2>)/)
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(/^<h2 id="([^"]+).*/, '$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(/(<h2 .+<\/h2>)/)
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

Comments
 (0)