kinsite 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/diff.mjs ADDED
@@ -0,0 +1,266 @@
1
+ // What changed on the site between two GEDCOMs: records as data, sentences under one build's rules.
2
+ import fs from 'node:fs'
3
+ import path from 'node:path'
4
+ import { parseGedcom, restricted } from './gedcom.mjs'
5
+ import { isLiving } from './living.mjs'
6
+ import { place, redact } from './redact.mjs'
7
+ import { when, known, cited, linkLabel as label, ONCE, IMG_EXT } from './render.mjs'
8
+
9
+ const NOUNS = {
10
+ BIRT: 'birth', CHR: 'christening', BAPM: 'baptism', DEAT: 'death', BURI: 'burial',
11
+ RESI: 'residence', EMIG: 'emigration', OCCU: 'occupation',
12
+ }
13
+
14
+ const fact = e => ({
15
+ tag: e.tag, type: e.type ?? '', val: e.val ?? '', date: e.date ?? '', phrase: e.phrase ?? '', plac: e.plac ?? '', resn: e.resn ?? '',
16
+ })
17
+ const factKey = e => JSON.stringify({ ...fact(e), resn: restricted(e) })
18
+ const citeKey = c => `${c.sour || c.title}\n${c.page}`
19
+ const sameCite = (a, b) => citeKey(a) === citeKey(b) && a.text === b.text && a.notes.join('\n') === b.notes.join('\n')
20
+ const spouses = f => (f ? [f.husb, f.wife].filter(Boolean) : [])
21
+ const union = (a, b) => [...new Set([...a, ...b])]
22
+
23
+ function match(olds, news, same, near = () => false) {
24
+ const left = [...olds], pairs = [], rest = []
25
+ const take = (n, ok) => {
26
+ const i = left.findIndex(o => ok(o, n))
27
+ return i < 0 ? null : left.splice(i, 1)[0]
28
+ }
29
+ for (const n of news) {
30
+ const o = take(n, same)
31
+ if (o) pairs.push([o, n, true])
32
+ else rest.push(n)
33
+ }
34
+ const added = []
35
+ for (const n of rest) {
36
+ const o = take(n, near)
37
+ if (o) pairs.push([o, n, false])
38
+ else added.push(n)
39
+ }
40
+ pairs.sort((a, b) => news.indexOf(a[1]) - news.indexOf(b[1]))
41
+ return { pairs, removed: left, added }
42
+ }
43
+
44
+ export function diffTrees(old, cur) {
45
+ const gone = [...old.indi.keys()].filter(id => !cur.indi.has(id))
46
+ if (gone.length * 2 > old.indi.size) {
47
+ throw new Error(`xrefs look renumbered: ${gone.length} of ${old.indi.size} people in the old file are missing from the new one`)
48
+ }
49
+ const out = []
50
+ for (const [id, p] of cur.indi) {
51
+ if (old.indi.has(id)) out.push(...personChanges(old.indi.get(id), p, old, cur))
52
+ else out.push({ kind: 'person', op: 'added', id })
53
+ }
54
+ for (const fid of union(cur.fam.keys(), old.fam.keys())) out.push(...marriageChanges(fid, old.fam.get(fid), cur.fam.get(fid)))
55
+ for (const id of gone) out.push({ kind: 'person', op: 'removed', id, was: structuredClone(old.indi.get(id)) })
56
+ return out
57
+ }
58
+
59
+ function personChanges(o, p, old, cur) {
60
+ const id = p.id, out = []
61
+ const push = (kind, op, from, to, extra) =>
62
+ out.push({ kind, op, id, ...extra, ...(from !== undefined && { from }), ...(to !== undefined && { to }) })
63
+ if (o.name !== p.name) push('name', 'changed', o.name, p.name)
64
+
65
+ const notes = (olds, news, event) => {
66
+ const text = ns => (ns ?? []).filter(n => n.trim())
67
+ const { removed, added } = match(text(olds), text(news), (a, b) => a === b)
68
+ if (removed.length === 1 && added.length === 1) return push('note', 'changed', removed[0], added[0], { event })
69
+ for (const n of removed) push('note', 'removed', n, undefined, { event })
70
+ for (const n of added) push('note', 'added', undefined, n, { event })
71
+ }
72
+ const cites = (olds, news, event) => {
73
+ const shown = cs => (cs ?? []).filter(cited)
74
+ const { pairs, removed, added } = match(shown(olds), shown(news), sameCite, (a, b) => citeKey(a) === citeKey(b))
75
+ for (const [a, b, same] of pairs) if (!same) push('citation', 'changed', structuredClone(a), structuredClone(b), { event })
76
+ for (const c of removed) push('citation', 'removed', structuredClone(c), undefined, { event })
77
+ for (const c of added) push('citation', 'added', undefined, structuredClone(c), { event })
78
+ }
79
+
80
+ const group = e => (e.tag === 'EVEN' ? `EVEN ${e.type ?? ''}` : e.tag)
81
+ for (const g of union(o.events.map(group), p.events.map(group))) {
82
+ const olds = o.events.filter(e => group(e) === g), news = p.events.filter(e => group(e) === g)
83
+ const near = (a, b) => (a.date && a.date === b.date) || (a.plac && a.plac === b.plac)
84
+ const { pairs, removed, added } = match(olds, news, (a, b) => factKey(a) === factKey(b), near)
85
+ for (const [a, b, same] of pairs) {
86
+ if (!same) push('event', 'changed', fact(a), fact(b))
87
+ if (restricted(a)) continue
88
+ notes(a.notes, b.notes, fact(b))
89
+ cites(a.sources, b.sources, fact(b))
90
+ }
91
+ for (const e of removed) push('event', 'removed', fact(e))
92
+ for (const e of added) push('event', 'added', undefined, fact(e))
93
+ if (ONCE.has(g)) {
94
+ const was = olds.find(e => !restricted(e)), now = news.find(e => !restricted(e))
95
+ const counterpart = pairs.find(([a]) => a === was)?.[1]
96
+ if (counterpart && now && counterpart !== now && !restricted(counterpart)) push('preferred', 'changed', fact(counterpart), fact(now))
97
+ }
98
+ }
99
+ notes(o.notes, p.notes)
100
+ cites(o.sources, p.sources)
101
+
102
+ const obje = ({ file, title, form }) => ({ file, title, form })
103
+ const media = match(o.media, p.media, (a, b) => a.file === b.file && a.title === b.title, (a, b) => a.file === b.file)
104
+ for (const [a, b, same] of media.pairs) if (!same) push('media', 'changed', obje(a), obje(b))
105
+ for (const m of media.removed) push('media', 'removed', obje(m))
106
+ for (const m of media.added) push('media', 'added', undefined, obje(m))
107
+
108
+ // Which side of the file records a link (only) is bookkeeping, not a change to report.
109
+ const links = q => new Map(q.parentFams.map(({ only, ...l }) => [l.fam, l]))
110
+ const was = links(o), now = links(p)
111
+ if (p.famc && p.famc !== o.famc) push('parents', 'main', undefined, undefined, { fam: p.famc, parents: spouses(cur.fam.get(p.famc)) })
112
+ for (const fam of union(was.keys(), now.keys())) {
113
+ const a = was.get(fam), b = now.get(fam)
114
+ const parents = spouses((b ? cur : old).fam.get(fam))
115
+ if (a && b) {
116
+ if (label(a) !== label(b)) push('parents', 'changed', { ...a }, { ...b }, { fam, parents })
117
+ } else if (b) {
118
+ if (fam !== p.famc) push('parents', 'added', undefined, { ...b }, { fam, parents })
119
+ } else if (fam !== o.famc || !p.famc) push('parents', 'removed', { ...a }, undefined, { fam, parents })
120
+ }
121
+ return out
122
+ }
123
+
124
+ function marriageChanges(fam, a, b) {
125
+ const was = spouses(a), now = spouses(b), marr = f => fact({ tag: 'MARR', ...f.marr })
126
+ if (was.length === 2 && now.length === 2 && was[0] === now[0] && was[1] === now[1]) {
127
+ const from = marr(a), to = marr(b)
128
+ return factKey(from) === factKey(to) ? [] : [{ kind: 'marriage', op: 'changed', fam, spouses: now, from, to }]
129
+ }
130
+ return [[a, was, 'removed'], [b, now, 'added']].filter(([, pair]) => pair.length === 2)
131
+ .map(([f, pair, op]) => ({ kind: 'marriage', op, fam, spouses: pair, marr: marr(f) }))
132
+ }
133
+
134
+ export function describe(records, tree, { anonymizeLiving = false, notes = true, citations = true } = {}, { year } = {}) {
135
+ const people = op => records.filter(r => r.kind === 'person' && r.op === op)
136
+ const removed = new Map(people('removed').map(r => [r.id, r.was])), added = new Set(people('added').map(r => r.id))
137
+ const person = id => tree.indi.get(id) ?? removed.get(id)
138
+ const hidden = id => anonymizeLiving && (!person(id) || isLiving(person(id), year))
139
+ const name = id => person(id)?.name || '(unnamed)'
140
+ const names = ids => ids.map(name).join(' and ')
141
+ const quote = !anonymizeLiving
142
+ const withheld = new Set(anonymizeLiving
143
+ ? [...tree.indi.values(), ...removed.values()].filter(p => isLiving(p, year)).flatMap(p => p.media.map(m => m.file)) : [])
144
+ const order = new Map([...tree.indi.keys()].map((id, i) => [id, i]))
145
+ const first = ids => [...ids].sort((a, b) => (order.get(a) ?? Infinity) - (order.get(b) ?? Infinity))[0]
146
+
147
+ const items = [], groups = new Map()
148
+ const under = (id, line) => {
149
+ if (!groups.has(id)) {
150
+ groups.set(id, { id, text: `${name(id)} (${id})`, lines: [] })
151
+ items.push(groups.get(id))
152
+ }
153
+ groups.get(id).lines.push(line)
154
+ }
155
+
156
+ const folded = new Map()
157
+ for (const r of records) {
158
+ if (r.kind !== 'marriage' || r.op === 'changed' || r.spouses.some(hidden)) continue
159
+ const who = r.spouses.find(id => (r.op === 'added' ? added : removed).has(id))
160
+ if (who) folded.set(r, who)
161
+ }
162
+ const marriagesOf = id => {
163
+ const partners = [...folded].filter(([, who]) => who === id).map(([r]) => r.spouses.find(s => s !== id))
164
+ if (!partners.length) return ''
165
+ const pron = { M: 'his', F: 'her' }[person(id).sex] ?? 'their'
166
+ return ` and ${pron} marriage${partners.length > 1 ? 's' : ''} to ${names(partners)}`
167
+ }
168
+
169
+ const noun = e => (e.tag === 'MARR' ? 'marriage' : e.tag !== 'EVEN' ? NOUNS[e.tag] : e.type === 'Living' ? 'residence' : e.type || 'event')
170
+ const article = w => (/^[aeiou]/i.test(w) ? 'an' : 'a')
171
+ const where = e => place(known(e.plac)).replace(/[, ]+$/, '')
172
+ const bits = e => [known(e.val) !== 'Y' && known(e.val), when(e), where(e)].filter(Boolean).join(', ')
173
+ const factText = e => [noun(e), bits(e)].filter(Boolean).join(' ')
174
+ const changed = (from, to, what) => {
175
+ const field = from.val !== to.val ? '' : from.plac === to.plac ? 'date' : from.date === to.date && from.phrase === to.phrase ? 'place' : ''
176
+ const show = e => ({ date: when, place: where }[field] ?? bits)(e) || 'none'
177
+ return `Changed ${what(field)} ${quote && !restricted(from) ? `from ${show(from)} ` : ''}to ${show(to)}`
178
+ }
179
+ const citeText = c => [c.title.trim(), c.page.trim(), c.text.trim() && `“${c.text.trim()}”`].filter(Boolean).join(', ')
180
+ const mediaKind = m => (IMG_EXT.has(path.extname(m.file).toLowerCase()) ? 'photo' : 'document')
181
+
182
+ const line = r => {
183
+ const ctx = prep => (r.event ? ` ${prep} the ${noun(r.event)}` : '')
184
+ const { from, to } = r
185
+ if (r.event && restricted(r.event)) return null
186
+ switch (r.kind) {
187
+ case 'name':
188
+ return quote ? `Changed name from ${from} to ${to}` : `Changed name to ${to}`
189
+ case 'event':
190
+ if ((to && restricted(to)) || (!to && restricted(from))) return null
191
+ if (!from || restricted(from)) return `Added: ${factText(to)}`
192
+ if (!to) return quote ? `Removed: ${factText(from)}` : `Removed ${article(noun(from))} ${noun(from)} record`
193
+ return changed(from, to, field => [noun(to), field].filter(Boolean).join(' '))
194
+ case 'preferred':
195
+ return `Preferred ${noun(to)} is now ${bits(to)}` + (quote ? `, not ${bits(from)}` : '')
196
+ case 'note': {
197
+ if (!notes) return null
198
+ const text = n => `“${n.replace(/\s+/g, ' ').trim()}”`
199
+ if (r.op === 'added') return `Added a note${ctx('to')}: ${text(to)}`
200
+ if (r.op === 'removed') return `Removed a note${ctx('from')}` + (quote ? `: ${text(from)}` : '')
201
+ return `Changed a note${ctx('on')} to ${text(to)}`
202
+ }
203
+ case 'citation':
204
+ if (!citations) return null
205
+ if (r.op === 'added') return `Added a citation${ctx('to')}: ${citeText(to)}`
206
+ if (r.op === 'removed') return `Removed a citation${ctx('from')}` + (quote ? `: ${citeText(from)}` : '')
207
+ return `Changed a citation${ctx('on')}: ${citeText(to)}`
208
+ case 'media': {
209
+ const m = to ?? from, title = x => x.title || path.basename(x.file)
210
+ if (withheld.has(m.file)) return null
211
+ if (r.op === 'added') return `Added a ${mediaKind(m)}: ${title(m)}`
212
+ if (r.op === 'removed') return `Removed a ${mediaKind(m)}` + (quote ? `: ${title(m)}` : '')
213
+ return `Retitled a ${mediaKind(m)} ${quote ? `from ${title(from)} ` : ''}to ${title(to)}`
214
+ }
215
+ case 'parents': {
216
+ if (r.parents.some(hidden)) return null
217
+ const who = names(r.parents) || 'unrecorded parents'
218
+ let op = r.op
219
+ if (anonymizeLiving && op !== 'main') {
220
+ const seen = l => !!l && l.stat !== 'DISPROVEN', before = seen(from), after = seen(to)
221
+ if (!before && !after) return null
222
+ if (before !== after) op = after ? 'added' : 'removed'
223
+ if (op === 'added' && r.fam === person(r.id).famc) return null
224
+ }
225
+ if (op === 'main') return `Now listed as a child of ${who}`
226
+ if (op === 'added') return `Now also listed as a child of ${who}` + (label(to) ? ` (${label(to)})` : '')
227
+ if (op === 'removed') return `No longer listed as a child of ${who}`
228
+ return `Relabeled as a child of ${who} from ${label(from) || 'no label'} to ${label(to) || 'no label'}`
229
+ }
230
+ }
231
+ }
232
+
233
+ for (const r of records) {
234
+ if (r.kind === 'person') {
235
+ if (hidden(r.id)) continue
236
+ const p = person(r.id), parents = r.op === 'added' ? spouses(tree.fam.get(p.famc)) : []
237
+ const child = parents.length && !parents.some(hidden) ? `, a child of ${names(parents)}` : ''
238
+ const wed = marriagesOf(r.id)
239
+ items.push({ id: r.id, text: `${r.op === 'added' ? 'Added' : 'Removed'}: ${name(r.id)} (${r.id})${child}${child && wed && ','}${wed}`, lines: [] })
240
+ } else if (r.kind === 'marriage') {
241
+ if (folded.has(r) || r.spouses.some(hidden)) continue
242
+ const id = first(r.spouses), other = name(r.spouses.find(s => s !== id))
243
+ const details = () => (restricted(r.marr) || !bits(r.marr) ? '' : `, ${bits(r.marr)}`)
244
+ if (r.op === 'added') under(id, `Added: marriage to ${other}${details()}`)
245
+ else if (r.op === 'removed') under(id, quote ? `Removed: marriage to ${other}${details()}` : `Removed a marriage to ${other}`)
246
+ else if (!restricted(r.to)) under(id, changed(r.from, r.to, field => [field, field && 'of', 'the marriage to', other].filter(Boolean).join(' ')))
247
+ } else if (!hidden(r.id)) {
248
+ const text = line(r)
249
+ if (text) under(r.id, text)
250
+ }
251
+ }
252
+ return items.map(i => ({ ...i, text: redact(i.text), lines: i.lines.map(redact) }))
253
+ }
254
+
255
+ export const formatChanges = items =>
256
+ items.map(i => [i.text, ...i.lines.map(l => ` ${l}`)].join('\n')).join('\n') || 'No changes.'
257
+
258
+ export function readGedcom(f) {
259
+ if (!fs.statSync(f, { throwIfNoEntry: false })?.isFile()) throw new Error(`GEDCOM not found: ${f}`)
260
+ return parseGedcom(fs.readFileSync(f, 'utf8'))
261
+ }
262
+
263
+ export function changeReport(config, oldFile) {
264
+ const old = readGedcom(oldFile), cur = readGedcom(config.gedcom)
265
+ return formatChanges(describe(diffTrees(old, cur), cur, config))
266
+ }
package/lib/gedcom.mjs CHANGED
@@ -1,19 +1,20 @@
1
1
  // Minimal GEDCOM 7.0 reader: only what the site renders.
2
2
  import { yr } from './dates.mjs'
3
3
 
4
+ const CROP = new Set(['TOP', 'LEFT', 'HEIGHT', 'WIDTH'])
4
5
  const EVENTS = new Set(['BIRT', 'CHR', 'BAPM', 'DEAT', 'BURI', 'RESI', 'EMIG', 'OCCU', 'EVEN'])
5
6
 
6
7
  export const restricted = e => (e.resn ?? '').toUpperCase().split(/[\s,]+/).some(t => t === 'PRIVACY' || t === 'CONFIDENTIAL')
7
8
 
8
9
  export function parseGedcom(text) {
9
10
  const indi = new Map(), fam = new Map(), obje = new Map(), sour = new Map(), pointers = []
10
- let cur = null, ctype = null, evt = null, media = null, sub = null, cite = null, citeLvl = 0, cont = null, shownName = false
11
+ let cur = null, ctype = null, evt = null, famc = null, media = null, link = null, sub = null, cite = null, citeLvl = 0, cont = null, shownName = false
11
12
  const startCitation = (val, lvl, list) => {
12
- cite = { title: '', page: '', text: '', notes: [] }
13
+ const ptr = val.match(/^@(\w+)@$/)
14
+ cite = { sour: ptr?.[1] ?? '', title: '', page: '', text: '', notes: [] }
13
15
  citeLvl = lvl
14
16
  list.push(cite)
15
- const ptr = val.match(/^@(\w+)@$/)
16
- if (ptr) pointers.push([cite, ptr[1]])
17
+ if (ptr) pointers.push(cite)
17
18
  else {
18
19
  cite.title = val.trim()
19
20
  cont = { lvl, obj: cite, key: 'title' }
@@ -26,13 +27,13 @@ export function parseGedcom(text) {
26
27
  if (!m) continue
27
28
  const lvl = Number(m[1]), rest = m[2]
28
29
  if (lvl === 0) {
29
- cur = ctype = evt = media = cite = cont = null
30
+ cur = ctype = evt = famc = media = link = cite = cont = null
30
31
  const r = rest.match(/^@(\w+)@ (\w+)$/)
31
32
  if (r && r[2] === 'INDI') {
32
33
  ctype = 'INDI'
33
34
  if (!indi.has(r[1])) indi.set(r[1], {
34
35
  id: r[1], name: '', surn: '', giv: '', sex: '',
35
- birt: {}, deat: {}, events: [], famc: null, fams: [], media: [], notes: [], sources: [],
36
+ birt: {}, deat: {}, events: [], famc: null, parentFams: [], fams: [], media: [], notes: [], sources: [],
36
37
  })
37
38
  cur = indi.get(r[1])
38
39
  } else if (r && r[2] === 'FAM') {
@@ -92,7 +93,7 @@ export function parseGedcom(text) {
92
93
  }
93
94
  } else if (ctype === 'INDI') {
94
95
  if (lvl === 1) {
95
- evt = null
96
+ evt = famc = link = null
96
97
  shownName = tag === 'NAME' && !cur.name
97
98
  if (shownName) {
98
99
  cur.name = val.replaceAll('/', '').trim()
@@ -103,9 +104,12 @@ export function parseGedcom(text) {
103
104
  else if (EVENTS.has(tag)) {
104
105
  evt = { tag, val: val.trim() }
105
106
  cur.events.push(evt)
106
- } else if (tag === 'FAMC') cur.famc = stripAt(val)
107
+ } else if (tag === 'FAMC') {
108
+ famc = parentLink(stripAt(val))
109
+ cur.parentFams.push(famc)
110
+ }
107
111
  else if (tag === 'FAMS') cur.fams.push(stripAt(val))
108
- else if (tag === 'OBJE') cur.media.push(stripAt(val))
112
+ else if (tag === 'OBJE') cur.media.push(link = { id: stripAt(val) })
109
113
  else if (note) cont = { lvl, obj: cur.notes, key: cur.notes.push(val) - 1 }
110
114
  else if (tag === 'SOUR') startCitation(val, 1, cur.sources)
111
115
  } else if (lvl === 2 && tag === 'SOUR' && (evt || shownName)) {
@@ -118,6 +122,15 @@ export function parseGedcom(text) {
118
122
  else if (note) cont = { lvl, obj: (evt.notes ??= []), key: evt.notes.push(val) - 1 }
119
123
  } else if (evt && phrase) {
120
124
  evt.phrase = val.trim()
125
+ } else if (link && lvl === 2) {
126
+ if (tag === 'TITL') link.caption = val.trim()
127
+ else if (tag === 'CROP') link.crop = {}
128
+ } else if (link?.crop && lvl === 3 && sub === 'CROP' && CROP.has(tag) && /^\d+$/.test(val.trim())) {
129
+ link.crop[tag.toLowerCase()] = Number(val)
130
+ } else if (famc && lvl === 2 && (tag === 'PEDI' || tag === 'STAT')) {
131
+ famc[tag.toLowerCase()] = val.trim().toUpperCase()
132
+ } else if (famc && lvl === 3 && sub === 'PEDI' && tag === 'PHRASE') {
133
+ famc.phrase = val.trim()
121
134
  }
122
135
  } else {
123
136
  if (lvl === 1) {
@@ -133,15 +146,56 @@ export function parseGedcom(text) {
133
146
  } else if (evt && phrase) evt.phrase = val.trim()
134
147
  }
135
148
  }
136
- for (const [c, id] of pointers) c.title = sour.get(id)?.title ?? ''
149
+ for (const c of pointers) c.title = sour.get(c.sour)?.title ?? ''
137
150
  for (const p of indi.values()) {
138
151
  for (const [key, tag] of [['birt', 'BIRT'], ['deat', 'DEAT']]) {
139
152
  const all = p.events.filter(e => e.tag === tag)
140
153
  p[key] = all.find(e => !restricted(e) && yr(e.date)) ?? all[0] ?? {}
141
154
  }
142
- p.media = p.media.flatMap(id => obje.get(id) ?? [])
155
+ p.media = p.media.flatMap(({ id, ...own }) => (obje.has(id) ? { obje: id, ...obje.get(id), ...own } : []))
156
+ }
157
+ const warnings = checkLinks(indi, fam)
158
+ for (const p of indi.values()) p.famc = p.parentFams.find(l => l.stat !== 'DISPROVEN' && fam.has(l.fam))?.fam ?? null
159
+ return { indi, fam, obje, warnings }
160
+ }
161
+
162
+ export const parentLink = fam => ({ fam, pedi: '', phrase: '', stat: '' })
163
+
164
+ // Children in a family whose main parent family it is; ids with no record stay in.
165
+ export const mainChildren = (f, indi) => f.chil.filter(c => !indi.has(c) || indi.get(c).famc === f.id)
166
+
167
+ // Warns about links recorded on one side only, and shows a child link on both sides,
168
+ // marked with the side that recorded it (only: 'child' or 'family').
169
+ function checkLinks(indi, fam) {
170
+ const warnings = []
171
+ const warn = (from, tag, to, back) => warnings.push(`${from} ${tag} @${to}@ has no matching ${back} in ${to}`)
172
+ for (const p of indi.values()) {
173
+ for (const link of p.parentFams) {
174
+ const fid = link.fam
175
+ const f = fam.get(fid)
176
+ if (f?.chil.includes(p.id)) continue
177
+ warn(p.id, 'FAMC', fid, 'CHIL')
178
+ if (!f) continue
179
+ link.only = 'child'
180
+ f.chil.push(p.id)
181
+ }
182
+ for (const fid of p.fams) {
183
+ const f = fam.get(fid)
184
+ if (f?.husb !== p.id && f?.wife !== p.id) warn(p.id, 'FAMS', fid, 'HUSB or WIFE')
185
+ }
186
+ }
187
+ for (const f of fam.values()) {
188
+ for (const [tag, id] of [['HUSB', f.husb], ['WIFE', f.wife]]) {
189
+ if (id && !indi.get(id)?.fams.includes(f.id)) warn(f.id, tag, id, 'FAMS')
190
+ }
191
+ for (const c of f.chil) {
192
+ const p = indi.get(c)
193
+ if (p?.parentFams.some(l => l.fam === f.id)) continue
194
+ warn(f.id, 'CHIL', c, 'FAMC')
195
+ p?.parentFams.push({ ...parentLink(f.id), only: 'family' })
196
+ }
143
197
  }
144
- return { indi, fam }
198
+ return warnings
145
199
  }
146
200
 
147
201
  const stripAt = s => s.replace(/^@+|@+$/g, '')
package/lib/hourglass.mjs CHANGED
@@ -3,6 +3,7 @@
3
3
  // "+N more" box so the chart stays a readable neighbourhood, never a whole tree.
4
4
  import { esc } from './html.mjs'
5
5
  import { byBirth } from './dates.mjs'
6
+ import { mainChildren } from './gedcom.mjs'
6
7
 
7
8
  export const BOX = { w: 150, h: 44 }
8
9
  export const CAPS = { sibs: 3, spouses: 2, kids: 4 } // sibs is per side of the focus
@@ -24,7 +25,8 @@ export function hourglassLayout(pid, { indi, fam }, caps = CAPS) {
24
25
  return f ? [has(f.husb), has(f.wife)].filter(Boolean) : []
25
26
  }
26
27
  const parents = parentsOf(pid)
27
- const chil = (fam.get(indi.get(pid)?.famc)?.chil ?? []).filter(c => c === pid || indi.has(c)).sort(byBirth(indi))
28
+ const pf = fam.get(indi.get(pid)?.famc)
29
+ const chil = (pf ? mainChildren(pf, indi) : []).filter(c => indi.has(c)).sort(byBirth(indi))
28
30
  const older = chil.slice(0, chil.indexOf(pid)), younger = chil.slice(chil.indexOf(pid) + 1)
29
31
  const spouses = [], kids = []
30
32
  for (const fid of indi.get(pid)?.fams ?? []) {
@@ -32,7 +34,7 @@ export function hourglassLayout(pid, { indi, fam }, caps = CAPS) {
32
34
  if (!f) continue
33
35
  const sp = has(f.husb === pid ? f.wife : f.husb)
34
36
  if (sp) spouses.push(sp)
35
- kids.push(...f.chil.filter(c => indi.has(c)))
37
+ kids.push(...mainChildren(f, indi).filter(c => indi.has(c)))
36
38
  }
37
39
  kids.sort(byBirth(indi))
38
40
  const grands = parents.map(parentsOf)
package/lib/kin.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  // Shortest parent/child path from anyone to the primary person (BFS).
2
+ import { mainChildren } from './gedcom.mjs'
2
3
 
3
4
  export class Kin {
4
5
  constructor(indi, fam, primaryId) {
@@ -32,7 +33,7 @@ export class Kin {
32
33
  const kids = []
33
34
  for (const fid of this.indi.get(pid)?.fams ?? []) {
34
35
  const f = this.fam.get(fid)
35
- if (f) kids.push(...f.chil)
36
+ if (f) kids.push(...mainChildren(f, this.indi))
36
37
  }
37
38
  return kids
38
39
  }
package/lib/living.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  // Living people on a public site: kept in the tree for its shape, shown only as "Private".
2
+ import { parentLink } from './gedcom.mjs'
2
3
 
3
4
  export function isLiving(p, year = new Date().getFullYear()) {
4
5
  if (p.deat?.tag) return false
@@ -7,17 +8,26 @@ export function isLiving(p, year = new Date().getFullYear()) {
7
8
  return !years.length || year - Math.max(...years) <= 100
8
9
  }
9
10
 
11
+ // A Private person keeps only the main parent family, without PEDI or STAT.
10
12
  const stub = ({ id, famc, fams }) => ({
11
13
  id, name: 'Private', surn: '', giv: '', sex: '', private: true,
12
- birt: {}, deat: {}, events: [], famc, fams, media: [], notes: [], sources: [],
14
+ birt: {}, deat: {}, events: [], famc, parentFams: famc ? [parentLink(famc)] : [], fams, media: [], notes: [], sources: [],
13
15
  })
14
16
 
15
17
  export function anonymize(indi, fam, year) {
16
18
  const living = new Set([...indi.values()].filter(p => isLiving(p, year)).map(p => p.id))
17
19
  const withheld = new Set([...indi.values()].filter(p => living.has(p.id)).flatMap(p => p.media.map(m => m.file)))
18
- const keep = p => p.media.some(m => withheld.has(m.file)) ? { ...p, media: p.media.filter(m => !withheld.has(m.file)) } : p
19
- return {
20
- indi: new Map([...indi].map(([id, p]) => [id, living.has(id) ? stub(p) : keep(p)])),
21
- fam: new Map([...fam].map(([id, f]) => [id, living.has(f.husb) || living.has(f.wife) ? { ...f, marr: {} } : f])),
20
+ const keep = p => {
21
+ const media = p.media.filter(m => !withheld.has(m.file))
22
+ const parentFams = p.parentFams?.filter(l => l.stat !== 'DISPROVEN')
23
+ return media.length < p.media.length || parentFams?.length < p.parentFams?.length ? { ...p, media, parentFams } : p
22
24
  }
25
+ const people = new Map([...indi].map(([id, p]) => [id, living.has(id) ? stub(p) : keep(p)]))
26
+ const listed = (c, f) => people.get(c)?.parentFams?.some(l => l.fam === f.id) ?? true
27
+ const shown = f => {
28
+ const chil = f.chil.filter(c => listed(c, f))
29
+ const g = chil.length < f.chil.length ? { ...f, chil } : f
30
+ return living.has(f.husb) || living.has(f.wife) ? { ...g, marr: {} } : g
31
+ }
32
+ return { indi: people, fam: new Map([...fam].map(([id, f]) => [id, shown(f)])) }
23
33
  }
package/lib/media.mjs ADDED
@@ -0,0 +1,74 @@
1
+ // Image sizes from PNG, JPEG, GIF and WebP headers, and GEDCOM 7.0 CROP regions within them.
2
+ const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
3
+ const SOF = new Set([0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf])
4
+
5
+ const sized = (width, height) => (width > 0 && height > 0 ? { width, height } : null)
6
+
7
+ function orientation(buf, at, end) {
8
+ if (buf.toString('latin1', at, at + 6) !== 'Exif\0\0') return 1
9
+ const tiff = at + 6, le = buf.toString('latin1', tiff, tiff + 2) === 'II'
10
+ const u16 = i => (le ? buf.readUInt16LE(i) : buf.readUInt16BE(i)), u32 = i => (le ? buf.readUInt32LE(i) : buf.readUInt32BE(i))
11
+ try {
12
+ const ifd = tiff + u32(tiff + 4)
13
+ for (let i = 0, n = u16(ifd); i < n; i++) {
14
+ const entry = ifd + 2 + i * 12
15
+ if (entry + 12 > end) break
16
+ if (u16(entry) === 0x0112) return u16(entry + 8)
17
+ }
18
+ } catch {}
19
+ return 1
20
+ }
21
+
22
+ function jpegSize(buf) {
23
+ let at = 2, turned = false
24
+ while (at + 4 <= buf.length) {
25
+ if (buf[at] !== 0xff) return null
26
+ const marker = buf[at + 1]
27
+ if (marker === 0xff) {
28
+ at++
29
+ continue
30
+ }
31
+ if (marker === 0xd9 || marker === 0xda) return null
32
+ const len = buf.readUInt16BE(at + 2)
33
+ if (marker === 0xe1) turned ||= orientation(buf, at + 4, at + 2 + len) >= 5
34
+ if (SOF.has(marker)) {
35
+ if (at + 9 > buf.length) return null
36
+ const width = buf.readUInt16BE(at + 7), height = buf.readUInt16BE(at + 5)
37
+ return turned ? sized(height, width) : sized(width, height)
38
+ }
39
+ at += 2 + len
40
+ }
41
+ return null
42
+ }
43
+
44
+ function webpSize(buf) {
45
+ const chunk = buf.toString('latin1', 12, 16)
46
+ if (chunk === 'VP8 ' && buf.length >= 30 && buf[23] === 0x9d && buf[24] === 0x01 && buf[25] === 0x2a) {
47
+ return sized(buf.readUInt16LE(26) & 0x3fff, buf.readUInt16LE(28) & 0x3fff)
48
+ }
49
+ if (chunk === 'VP8L' && buf.length >= 25 && buf[20] === 0x2f) {
50
+ const bits = buf.readUInt32LE(21)
51
+ return sized((bits & 0x3fff) + 1, ((bits >>> 14) & 0x3fff) + 1)
52
+ }
53
+ if (chunk === 'VP8X' && buf.length >= 30) return sized(buf.readUIntLE(24, 3) + 1, buf.readUIntLE(27, 3) + 1)
54
+ return null
55
+ }
56
+
57
+ // A JPEG's size is upright, as browsers show it after applying its Exif orientation.
58
+ export function imageSize(buf) {
59
+ if (buf.length >= 24 && buf.subarray(0, 8).equals(PNG) && buf.toString('latin1', 12, 16) === 'IHDR') {
60
+ return sized(buf.readUInt32BE(16), buf.readUInt32BE(20))
61
+ }
62
+ if (buf.length >= 10 && /^GIF8[79]a$/.test(buf.toString('latin1', 0, 6))) return sized(buf.readUInt16LE(6), buf.readUInt16LE(8))
63
+ if (buf.length >= 16 && buf.toString('latin1', 0, 4) === 'RIFF' && buf.toString('latin1', 8, 12) === 'WEBP') return webpSize(buf)
64
+ if (buf[0] === 0xff && buf[1] === 0xd8) return jpegSize(buf)
65
+ return null
66
+ }
67
+
68
+ export function cropBox(crop, size) {
69
+ if (!size) return null
70
+ const left = crop.left ?? 0, top = crop.top ?? 0
71
+ const width = Math.min(crop.width ?? Infinity, size.width - left)
72
+ const height = Math.min(crop.height ?? Infinity, size.height - top)
73
+ return width > 0 && height > 0 ? { left, top, width, height } : null
74
+ }