Repository navigation
Expand file tree
/
Copy pathcompiler_publication.go
More file actions
394 lines (386 loc) · 11.7 KB
/
Copy pathcompiler_publication.go
File metadata and controls
394 lines (386 loc) · 11.7 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package knowledge
import (
"regexp"
"sort"
"strings"
"time"
"unicode"
"unicode/utf8"
)
func applyNativeOverrides(vault document, overrides map[string]pageOverride) document {
if len(overrides) == 0 {
return vault
}
claims := index(items(vault["claims"]))
applyHumanChoices(claims, overrides)
kept := []document{}
suppressed := map[string]bool{}
for _, note := range items(vault["notes"]) {
if change, ok := overrides[textValue(note["id"])]; ok {
applyTitleChange(note, change)
if change.ChoicesOnly {
applyAccountChoices(note, change.Resolutions, claims)
for _, choice := range change.Resolutions {
if claims[choice.Keep] == nil {
continue
}
for _, id := range choice.Claims {
if id != choice.Keep {
suppressed[id] = true
}
}
}
kept = append(kept, note)
continue
}
original := stringsValue(note["original_claims"])
if note["original_claims"] == nil {
original = stringsValue(note["claims"])
}
for _, id := range original {
suppressed[id] = true
}
if change.Deleted {
continue
}
evidence := items(note["original_evidence"])
for _, id := range stringsValue(note["claims"]) {
evidence = unionEvidence(evidence, items(claims[id]["evidence"]))
}
for _, s := range items(note["sections"]) {
for _, p := range items(s["paragraphs"]) {
evidence = unionEvidence(evidence, items(p["evidence"]))
}
}
note["original_evidence"] = evidence
note["original_claims"] = original
note["human_edited"] = true
note["human_edited_at"] = change.UpdatedAt
note["manual_markdown"] = change.Markdown
note["claims"] = []string{}
note["links"] = []document{}
note["backlinks"] = []document{}
note["needs_editorial_update"] = false
note["learning_state"] = "human_edited"
note["sections"] = []document{{"heading": "", "paragraphs": []document{{"text": change.Markdown, "claims": []string{}, "evidence": []document{}, "derivation": "human_edit"}}}}
for _, key := range []string{"questions", "conflicts", "composition", "article_rejections"} {
delete(note, key)
}
}
kept = append(kept, note)
}
visible := index(kept)
refs := map[string]bool{}
for _, override := range overrides {
for _, choice := range override.Resolutions {
for _, id := range choice.Claims {
refs[id] = true
}
}
}
for _, note := range kept {
for _, id := range stringsValue(note["claims"]) {
refs[id] = true
}
for _, conflict := range items(note["conflicts"]) {
for _, id := range stringsValue(conflict["claims"]) {
refs[id] = true
}
}
for _, section := range items(note["sections"]) {
for _, p := range items(section["paragraphs"]) {
for _, id := range stringsValue(p["claims"]) {
refs[id] = true
}
p["inline_links"] = visibleDocumentLinks(items(p["inline_links"]), visible)
}
}
note["links"] = visibleDocumentLinks(items(note["links"]), visible)
note["backlinks"] = visibleDocumentLinks(items(note["backlinks"]), visible)
}
keptClaims := []document{}
for _, c := range items(vault["claims"]) {
id := textValue(c["id"])
if !suppressed[id] || refs[id] {
keptClaims = append(keptClaims, c)
}
}
remaining := index(keptClaims)
relations := []document{}
for _, r := range items(vault["relations"]) {
if remaining[textValue(r["from"])] != nil && remaining[textValue(r["to"])] != nil {
relations = append(relations, r)
}
}
vault["notes"] = kept
vault["claims"] = keptClaims
vault["relations"] = relations
return vault
}
// applyTitleChange makes labels part of the shared publication and its revision,
// so every open reader and the full-text index see the same rename.
func applyTitleChange(note document, change pageOverride) {
if change.Title == "" {
return
}
note["title"] = change.Title
note["title_edited"] = true
note["aliases"] = sortedUnique(append(stringsValue(note["aliases"]), change.TitleAliases...))
}
func unionEvidence(left, right []document) []document {
seen := map[string]bool{}
result := []document{}
for _, e := range append(append([]document{}, left...), right...) {
key := textValue(e["source"]) + "\x00" + textValue(e["quote"])
if !seen[key] {
seen[key] = true
result = append(result, e)
}
}
return result
}
func visibleDocumentLinks(links []document, visible map[string]document) []document {
kept := []document{}
for _, l := range links {
if visible[textValue(l["id"])] != nil {
kept = append(kept, l)
}
}
return kept
}
var protectedMarkup = regexp.MustCompile("(?s)```.*?```|~~~.*?~~~|`+[^`]*`+|\\[\\[[^\\]]*\\]\\]|!?\\[[^\\]]*\\]\\([^)]*\\)|<[^>]*>|https?://\\S+")
func linkCompilation(vault document) {
notes := items(vault["notes"])
byID := index(notes)
labels := map[string]string{}
ambiguous := map[string]bool{}
for _, n := range notes {
for _, label := range append([]string{textValue(n["title"])}, stringsValue(n["aliases"])...) {
label = strings.TrimSpace(label)
if label == "" {
continue
}
key := strings.ToLower(label)
id := textValue(n["id"])
if prior := labels[key]; prior != "" && prior != id {
ambiguous[key] = true
}
labels[key] = id
}
}
spellings := []string{}
for label := range labels {
if !ambiguous[label] {
spellings = append(spellings, label)
}
}
sort.Slice(spellings, func(i, j int) bool {
if len(spellings[i]) != len(spellings[j]) {
return len(spellings[i]) > len(spellings[j])
}
return spellings[i] < spellings[j]
})
incoming := map[string][]string{}
for _, note := range notes {
id := textValue(note["id"])
edges := []document{}
for _, edge := range items(note["links"]) {
if textValue(edge["kind"]) != "mention" {
edges = append(edges, edge)
}
}
targets := []string{}
for _, s := range items(note["sections"]) {
for _, p := range items(s["paragraphs"]) {
text := textValue(p["text"])
protected := protectedMarkup.FindAllStringIndex(text, -1)
spans := []document{}
for _, span := range protected {
token := text[span[0]:span[1]]
if !strings.HasPrefix(token, "[[") || !strings.HasSuffix(token, "]]") {
continue
}
target, display, hasDisplay := strings.Cut(token[2:len(token)-2], "|")
target = strings.TrimSpace(target)
label := target
if hasDisplay {
label = display
}
if byID[target] == nil {
key := strings.ToLower(target)
if ambiguous[key] {
continue
}
target = labels[key]
}
if target != "" && target != id {
spans = append(spans, document{"start": utf8.RuneCountInString(text[:span[0]]), "end": utf8.RuneCountInString(text[:span[1]]), "id": target, "label": label})
targets = append(targets, target)
}
}
// Rune scanning avoids byte-offset errors on non-ASCII source text. Longest
// matching unambiguous title wins; existing markup stays opaque.
runes := []rune(text)
byteOffset := 0
for pos := 0; pos < len(runes); {
protectedEnd := 0
for _, span := range protected {
if byteOffset >= span[0] && byteOffset < span[1] {
protectedEnd = span[1]
break
}
}
if protectedEnd > 0 {
advance := utf8.RuneCountInString(text[byteOffset:protectedEnd])
pos += advance
byteOffset = protectedEnd
continue
}
matched := 0
for _, label := range spellings {
labelRunes := []rune(label)
end := pos + len(labelRunes)
if end > len(runes) || labels[label] == id {
continue
}
if pos > 0 && isWord(runes[pos-1]) || end < len(runes) && isWord(runes[end]) {
continue
}
if !strings.EqualFold(string(runes[pos:end]), label) {
continue
}
overlap := false
byteEnd := byteOffset + len(string(runes[pos:end]))
for _, span := range protected {
if byteOffset < span[1] && byteEnd > span[0] {
overlap = true
break
}
}
if overlap {
continue
}
spans = append(spans, document{"start": pos, "end": end, "id": labels[label]})
targets = append(targets, labels[label])
matched = len(labelRunes)
break
}
if matched == 0 {
matched = 1
}
byteOffset += len(string(runes[pos : pos+matched]))
pos += matched
}
sort.Slice(spans, func(i, j int) bool { return number(spans[i]["start"]) < number(spans[j]["start"]) })
p["inline_links"] = spans
}
}
for _, target := range sortedUnique(targets) {
incoming[target] = append(incoming[target], id)
exists := false
for _, edge := range edges {
if textValue(edge["id"]) == target {
exists = true
break
}
}
if !exists {
edges = append(edges, document{"id": target, "kind": "mention", "reason": "Mentioned in the article; navigation only"})
}
}
note["links"] = edges
}
for _, n := range notes {
backlinks := []document{}
for _, id := range sortedUnique(incoming[textValue(n["id"])]) {
backlinks = append(backlinks, document{"id": id, "kind": "mention"})
}
n["backlinks"] = backlinks
}
}
func isWord(r rune) bool { return unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' }
// A choice changes only its named disagreement. Other claims, sources and
// disagreements remain available to both the reader and the recall compiler.
func applyAccountChoices(note document, choices []Resolution, claims map[string]document) {
for _, choice := range choices {
if claims[choice.Keep] == nil {
note["needs_editorial_update"] = true
continue
}
losing := map[string]bool{}
for _, id := range choice.Claims {
if id != choice.Keep {
losing[id] = true
}
}
keptClaims := []string{}
for _, id := range stringsValue(note["claims"]) {
if !losing[id] {
keptClaims = append(keptClaims, id)
}
}
note["claims"] = keptClaims
included := false
for _, section := range items(note["sections"]) {
paragraphs := []document{}
for _, p := range items(section["paragraphs"]) {
omit, keep := false, false
for _, id := range stringsValue(p["claims"]) {
if losing[id] {
omit = true
}
if id == choice.Keep {
keep = true
included = true
}
}
if !omit {
paragraphs = append(paragraphs, p)
} else if keep {
paragraphs = append(paragraphs, document{"text": textValue(claims[choice.Keep]["text"]), "claims": []string{choice.Keep}, "evidence": items(claims[choice.Keep]["evidence"])})
}
}
section["paragraphs"] = paragraphs
}
if !included {
note["sections"] = append(items(note["sections"]), document{"heading": "", "paragraphs": []document{{"text": textValue(claims[choice.Keep]["text"]), "claims": []string{choice.Keep}, "evidence": items(claims[choice.Keep]["evidence"])}}})
}
conflicts := []document{}
for _, c := range items(note["conflicts"]) {
if ConflictID(Conflict{Claims: stringsValue(c["claims"])}) != choice.Conflict {
conflicts = append(conflicts, c)
}
}
note["conflicts"] = conflicts
}
}
// Legacy notes may still point at the same claim. A human choice retires the
// rejected account for recall everywhere, while its evidence stays in history.
func applyHumanChoices(claims map[string]document, overrides map[string]pageOverride) {
choices := []Resolution{}
for _, override := range overrides {
choices = append(choices, override.Resolutions...)
}
sort.Slice(choices, func(i, j int) bool {
a, _ := time.Parse(time.RFC3339Nano, choices[i].At)
b, _ := time.Parse(time.RFC3339Nano, choices[j].At)
if a.Equal(b) {
return choices[i].Conflict < choices[j].Conflict
}
return a.Before(b)
})
for _, choice := range choices {
chosen := claims[choice.Keep]
if chosen == nil {
continue
}
for _, id := range choice.Claims {
if id != choice.Keep && claims[id] != nil {
claims[id]["status"] = "superseded"
}
}
chosen["status"] = "active"
chosen["human_edited"] = true
chosen["human_edited_at"] = choice.At
}
}