#!/usr/bin/env bun
const filePath = Bun.argv[2]
const file = Bun.file(filePath)
let textContent = await file.text()
let textGroups = textContent.split("
").map(g => g.trim())
textGroups = textGroups.map((g) => {
let ret = g
let footnote
let i = 1
let notes = []
const footnotesRe = /\s?\[(?[^\]]*)\]/
while (ret.match(footnotesRe) !== null) {
footnote = ret.match(footnotesRe)
const content = footnote.groups.footnote
ret = ret.replace(footnotesRe, `${i}`)
notes.push(`[${i}] ${content.replace(' \ ', '
')}
`)
i++
}
ret += "
"+notes.join("\n\n")
return ret
})
const final = textGroups.join("\n\n
\n\n");
file.write(final);