Allow editing of individual sections

This commit is contained in:
2026-04-15 12:14:40 +02:00
parent b3ca714597
commit 02fa19272d
7 changed files with 104 additions and 2 deletions

31
sections.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"bytes"
"regexp"
)
var sectionHeadingRe = regexp.MustCompile(`(?m)^#{1,6} `)
// splitSections splits raw markdown into sections.
// Section 0 is any content before the first heading.
// Each subsequent section begins at a heading line and runs to the next.
func splitSections(raw []byte) [][]byte {
locs := sectionHeadingRe.FindAllIndex(raw, -1)
if len(locs) == 0 {
return [][]byte{raw}
}
sections := make([][]byte, 0, len(locs)+1)
prev := 0
for _, loc := range locs {
sections = append(sections, raw[prev:loc[0]])
prev = loc[0]
}
sections = append(sections, raw[prev:])
return sections
}
// joinSections reassembles sections produced by splitSections.
func joinSections(sections [][]byte) []byte {
return bytes.Join(sections, nil)
}