* Split the CHANGELOG into a header preface + an ordered list of * version blocks `{ header, body[] }`, preserving line content * verbatim so we can re-join without surprises.
(text)
| 71 | * verbatim so we can re-join without surprises. |
| 72 | */ |
| 73 | function parseChangelog(text) { |
| 74 | const lines = text.split('\n'); |
| 75 | const versionHeaderRe = /^## \[([^\]]+)\](?:\s+-\s+(.+))?\s*$/; |
| 76 | const preface = []; |
| 77 | const blocks = []; // { header: string, name: string, body: string[] } |
| 78 | let cur = null; |
| 79 | for (const line of lines) { |
| 80 | const m = line.match(versionHeaderRe); |
| 81 | if (m) { |
| 82 | if (cur) blocks.push(cur); |
| 83 | cur = { header: line, name: m[1], date: m[2] ?? null, body: [] }; |
| 84 | } else if (cur) { |
| 85 | cur.body.push(line); |
| 86 | } else { |
| 87 | preface.push(line); |
| 88 | } |
| 89 | } |
| 90 | if (cur) blocks.push(cur); |
| 91 | return { preface, blocks }; |
| 92 | } |
| 93 | |
| 94 | function joinChangelog({ preface, blocks }) { |
| 95 | const parts = [preface.join('\n')]; |