* Split a block body into ordered sub-sections keyed by their * `### Heading`. Lines before the first `### Heading` go in * `leading`. Preserves the original (line-array) body inside each * sub-section so we can splice cleanly when merging.
(body)
| 108 | * sub-section so we can splice cleanly when merging. |
| 109 | */ |
| 110 | function splitSubsections(body) { |
| 111 | const subsectionRe = /^### (\w+)\s*$/; |
| 112 | const leading = []; |
| 113 | const subs = []; // { heading: 'Added' | 'Fixed' | …, headerLine: string, body: string[] } |
| 114 | let cur = null; |
| 115 | for (const line of body) { |
| 116 | const m = line.match(subsectionRe); |
| 117 | if (m) { |
| 118 | if (cur) subs.push(cur); |
| 119 | cur = { heading: m[1], headerLine: line, body: [] }; |
| 120 | } else if (cur) { |
| 121 | cur.body.push(line); |
| 122 | } else { |
| 123 | leading.push(line); |
| 124 | } |
| 125 | } |
| 126 | if (cur) subs.push(cur); |
| 127 | return { leading, subs }; |
| 128 | } |
| 129 | |
| 130 | function rebuildBody({ leading, subs }) { |
| 131 | const parts = []; |