1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 |
package doc
import (
"crypto/sha256"
"encoding/hex"
"strings"
)
// Block kinds.
const (
KindParagraph = "paragraph"
KindHeading = "heading"
KindCode = "code"
KindListItem = "list_item"
KindQuote = "quote"
KindHTML = "html"
KindTable = "table"
KindBreak = "break"
)
// Block is the unit of the block-level diff: a paragraph, a heading, one
// list item with whatever is nested under it, a whole code block, quote or
// table.
type Block struct {
Kind string `json:"kind"`
Source string `json:"source"`
Text string `json:"text"`
Hash string `json:"-"`
}
// SplitBlocks cuts Markdown into blocks with a line scanner instead of a full
// CommonMark parse. A diff needs stable block boundaries, not a render tree,
// and a parser's tree loses the exact source span of fences and thematic
// breaks. The rules follow CommonMark where it matters for boundaries.
func SplitBlocks(body string) []Block {
lines := strings.Split(body, "\n")
n := len(lines)
var blocks []Block
emit := func(kind string, start, end int) {
for end > start && isBlank(lines[end-1]) {
end--
}
src := strings.Join(lines[start:end], "\n")
blocks = append(blocks, Block{Kind: kind, Source: src, Text: plainText(kind, src), Hash: blockHash(src)})
}
for i := 0; i < n; {
l := lines[i]
if isBlank(l) {
i++
continue
}
start := i
switch {
case fenceMarker(l) != "":
f := fenceMarker(l)
for i++; i < n && !closesFence(lines[i], f); i++ {
}
if i < n {
i++
}
emit(KindCode, start, i)
case isATXHeading(l):
i++
emit(KindHeading, start, i)
case isThematicBreak(l):
i++
emit(KindBreak, start, i)
case isQuote(l):
for i++; i < n && !isBlank(lines[i]) && (isQuote(lines[i]) || !interrupts(lines[i])); i++ {
}
emit(KindQuote, start, i)
case isListItem(l):
markerIndent, contentIndent, _ := listMarker(l)
i++
for i < n {
if isBlank(lines[i]) {
j := i
for j < n && isBlank(lines[j]) {
j++
}
if j < n && indentOf(lines[j]) >= contentIndent {
i = j
continue
}
break
}
if mi, _, ok := listMarker(lines[i]); ok && mi <= markerIndent && !isThematicBreak(lines[i]) {
break
}
if indentOf(lines[i]) < contentIndent && interruptsList(lines[i]) {
break
}
i++
}
emit(KindListItem, start, i)
case isHTMLStart(l):
for i++; i < n && !isBlank(lines[i]); i++ {
}
emit(KindHTML, start, i)
case i+1 < n && strings.Contains(l, "|") && isDelimiterRow(lines[i+1]):
for i += 2; i < n && !isBlank(lines[i]) && strings.Contains(lines[i], "|"); i++ {
}
emit(KindTable, start, i)
case indentOf(l) >= 4:
for i++; i < n; {
if isBlank(lines[i]) {
j := i
for j < n && isBlank(lines[j]) {
j++
}
if j < n && indentOf(lines[j]) >= 4 {
i = j
continue
}
break
}
if indentOf(lines[i]) < 4 {
break
}
i++
}
emit(KindCode, start, i)
default:
kind := KindParagraph
for i++; i < n && !isBlank(lines[i]); i++ {
if isSetextUnderline(lines[i]) {
i++
kind = KindHeading
break
}
if interrupts(lines[i]) {
break
}
}
emit(kind, start, i)
}
}
return blocks
}
func blockHash(src string) string {
sum := sha256.Sum256([]byte(strings.Join(strings.Fields(src), " ")))
return hex.EncodeToString(sum[:8])
}
func isBlank(l string) bool { return strings.TrimSpace(l) == "" }
// indentOf counts leading columns, with tabs advancing to the next stop of 4.
func indentOf(l string) int {
col := 0
for _, c := range l {
switch c {
case ' ':
col++
case '\t':
col += 4 - col%4
default:
return col
}
}
return col
}
func fenceMarker(l string) string {
if indentOf(l) > 3 {
return ""
}
t := strings.TrimLeft(l, " \t")
if len(t) < 3 || (t[0] != '`' && t[0] != '~') {
return ""
}
n := 0
for n < len(t) && t[n] == t[0] {
n++
}
if n < 3 || (t[0] == '`' && strings.Contains(t[n:], "`")) {
return ""
}
return t[:n]
}
func closesFence(l, open string) bool {
if indentOf(l) > 3 {
return false
}
t := strings.TrimLeft(l, " \t")
n := 0
for n < len(t) && t[n] == open[0] {
n++
}
return n >= len(open) && strings.TrimSpace(t[n:]) == ""
}
func isATXHeading(l string) bool {
if indentOf(l) > 3 {
return false
}
t := strings.TrimLeft(l, " \t")
n := 0
for n < len(t) && t[n] == '#' {
n++
}
return n >= 1 && n <= 6 && (n == len(t) || t[n] == ' ' || t[n] == '\t')
}
func isThematicBreak(l string) bool {
if indentOf(l) > 3 {
return false
}
t := strings.TrimSpace(l)
if t == "" || !strings.ContainsRune("-*_", rune(t[0])) {
return false
}
count := 0
for _, c := range t {
switch {
case c == rune(t[0]):
count++
case c == ' ' || c == '\t':
default:
return false
}
}
return count >= 3
}
func isSetextUnderline(l string) bool {
if indentOf(l) > 3 {
return false
}
t := strings.TrimSpace(l)
return t != "" && (strings.Trim(t, "=") == "" || strings.Trim(t, "-") == "")
}
func isQuote(l string) bool {
return indentOf(l) <= 3 && strings.HasPrefix(strings.TrimLeft(l, " \t"), ">")
}
func isHTMLStart(l string) bool {
if indentOf(l) > 3 {
return false
}
t := strings.TrimLeft(l, " \t")
if len(t) < 2 || t[0] != '<' {
return false
}
c := t[1]
return c == '/' || c == '!' || c == '?' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func isDelimiterRow(l string) bool {
t := strings.TrimSpace(l)
t = strings.TrimPrefix(t, "|")
t = strings.TrimSuffix(t, "|")
if t == "" {
return false
}
for _, cell := range strings.Split(t, "|") {
c := strings.TrimSpace(cell)
c = strings.TrimPrefix(c, ":")
c = strings.TrimSuffix(c, ":")
if c == "" || strings.Trim(c, "-") != "" {
return false
}
}
return true
}
// listMarker reports a list item start: "- ", "* ", "+ ", "1. " or "1) ".
// contentIndent is the column where the item's text begins, which nested
// content must reach to stay inside the item.
func listMarker(l string) (markerIndent, contentIndent int, ok bool) {
markerIndent = indentOf(l)
if markerIndent > 3 {
return 0, 0, false
}
t := strings.TrimLeft(l, " \t")
m := 0
switch {
case t != "" && strings.ContainsRune("-*+", rune(t[0])):
m = 1
default:
for m < len(t) && m < 9 && t[m] >= '0' && t[m] <= '9' {
m++
}
if m == 0 || m >= len(t) || (t[m] != '.' && t[m] != ')') {
return 0, 0, false
}
m++
}
if m < len(t) && t[m] != ' ' && t[m] != '\t' {
return 0, 0, false
}
spaces := 0
for m+spaces < len(t) && t[m+spaces] == ' ' {
spaces++
}
if spaces == 0 || spaces > 4 || m+spaces == len(t) {
spaces = 1
}
return markerIndent, markerIndent + m + spaces, true
}
func isListItem(l string) bool {
_, _, ok := listMarker(l)
return ok && !isThematicBreak(l)
}
// interrupts reports whether a line ends a paragraph without a blank line.
// An ordered item interrupts only when it starts at 1, as in CommonMark, so
// "2026. was a long year" stays inside its paragraph.
func interrupts(l string) bool {
if isATXHeading(l) || fenceMarker(l) != "" || isThematicBreak(l) || isQuote(l) || isHTMLStart(l) {
return true
}
if !isListItem(l) {
return false
}
t := strings.TrimLeft(l, " \t")
if strings.ContainsRune("-*+", rune(t[0])) {
return true
}
return strings.HasPrefix(t, "1.") || strings.HasPrefix(t, "1)")
}
func interruptsList(l string) bool {
return isATXHeading(l) || fenceMarker(l) != "" || isThematicBreak(l) || isQuote(l) || isHTMLStart(l)
}
|