kinsite 0.2.0 → 0.4.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/README.md +217 -5
- package/build.mjs +17 -4
- package/lib/build.mjs +17 -3
- package/lib/config.mjs +9 -0
- package/lib/css.mjs +7 -0
- package/lib/diff.mjs +265 -0
- package/lib/gedcom.mjs +111 -19
- package/lib/hourglass.mjs +4 -2
- package/lib/kin.mjs +2 -1
- package/lib/living.mjs +18 -7
- package/lib/news.mjs +173 -0
- package/lib/render.mjs +102 -24
- package/package.json +1 -1
package/lib/gedcom.mjs
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
// Minimal GEDCOM 7.0 reader: only what the site renders.
|
|
2
|
+
import { yr } from './dates.mjs'
|
|
2
3
|
|
|
3
4
|
const EVENTS = new Set(['BIRT', 'CHR', 'BAPM', 'DEAT', 'BURI', 'RESI', 'EMIG', 'OCCU', 'EVEN'])
|
|
4
5
|
|
|
6
|
+
export const restricted = e => (e.resn ?? '').toUpperCase().split(/[\s,]+/).some(t => t === 'PRIVACY' || t === 'CONFIDENTIAL')
|
|
7
|
+
|
|
5
8
|
export function parseGedcom(text) {
|
|
6
|
-
const indi = new Map(), fam = new Map(), obje = new Map()
|
|
7
|
-
let cur = null, ctype = null, evt = null,
|
|
9
|
+
const indi = new Map(), fam = new Map(), obje = new Map(), sour = new Map(), pointers = []
|
|
10
|
+
let cur = null, ctype = null, evt = null, famc = null, media = null, sub = null, cite = null, citeLvl = 0, cont = null, shownName = false
|
|
11
|
+
const startCitation = (val, lvl, list) => {
|
|
12
|
+
const ptr = val.match(/^@(\w+)@$/)
|
|
13
|
+
cite = { sour: ptr?.[1] ?? '', title: '', page: '', text: '', notes: [] }
|
|
14
|
+
citeLvl = lvl
|
|
15
|
+
list.push(cite)
|
|
16
|
+
if (ptr) pointers.push(cite)
|
|
17
|
+
else {
|
|
18
|
+
cite.title = val.trim()
|
|
19
|
+
cont = { lvl, obj: cite, key: 'title' }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
8
22
|
|
|
9
23
|
for (const ln of text.replace(/\r\n?/g, '\n').split('\n')) {
|
|
10
24
|
if (!ln.trim()) continue
|
|
@@ -12,14 +26,13 @@ export function parseGedcom(text) {
|
|
|
12
26
|
if (!m) continue
|
|
13
27
|
const lvl = Number(m[1]), rest = m[2]
|
|
14
28
|
if (lvl === 0) {
|
|
15
|
-
cur = ctype = evt = media = null
|
|
16
|
-
note = false
|
|
29
|
+
cur = ctype = evt = famc = media = cite = cont = null
|
|
17
30
|
const r = rest.match(/^@(\w+)@ (\w+)$/)
|
|
18
31
|
if (r && r[2] === 'INDI') {
|
|
19
32
|
ctype = 'INDI'
|
|
20
33
|
if (!indi.has(r[1])) indi.set(r[1], {
|
|
21
34
|
id: r[1], name: '', surn: '', giv: '', sex: '',
|
|
22
|
-
birt: {}, deat: {}, events: [], famc: null, fams: [], media: [], notes: [],
|
|
35
|
+
birt: {}, deat: {}, events: [], famc: null, parentFams: [], fams: [], media: [], notes: [], sources: [],
|
|
23
36
|
})
|
|
24
37
|
cur = indi.get(r[1])
|
|
25
38
|
} else if (r && r[2] === 'FAM') {
|
|
@@ -30,6 +43,10 @@ export function parseGedcom(text) {
|
|
|
30
43
|
ctype = 'OBJE'
|
|
31
44
|
cur = { file: '', title: '', form: '' }
|
|
32
45
|
obje.set(r[1], cur)
|
|
46
|
+
} else if (r && r[2] === 'SOUR') {
|
|
47
|
+
ctype = 'SOUR'
|
|
48
|
+
cur = { title: '' }
|
|
49
|
+
sour.set(r[1], cur)
|
|
33
50
|
}
|
|
34
51
|
continue
|
|
35
52
|
}
|
|
@@ -37,10 +54,35 @@ export function parseGedcom(text) {
|
|
|
37
54
|
const sp = rest.indexOf(' ')
|
|
38
55
|
const tag = sp < 0 ? rest : rest.slice(0, sp)
|
|
39
56
|
let val = sp < 0 ? '' : rest.slice(sp + 1)
|
|
57
|
+
const note = tag === 'NOTE' && !/^@\w+@$/.test(val)
|
|
40
58
|
if (val.startsWith('@@')) val = val.slice(1)
|
|
59
|
+
if (cont && lvl <= cont.lvl) cont = null
|
|
60
|
+
if (tag === 'CONT') {
|
|
61
|
+
if (cont?.lvl === lvl - 1) cont.obj[cont.key] += '\n' + val
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
41
64
|
if (lvl === 2) sub = tag
|
|
65
|
+
if (cite && lvl <= citeLvl) cite = null
|
|
42
66
|
const phrase = lvl === 3 && sub === 'DATE' && tag === 'PHRASE'
|
|
43
|
-
if (
|
|
67
|
+
if (cite) {
|
|
68
|
+
if (lvl === citeLvl + 1 && tag === 'PAGE') {
|
|
69
|
+
cite.page = val.trim()
|
|
70
|
+
cont = { lvl, obj: cite, key: 'page' }
|
|
71
|
+
}
|
|
72
|
+
else if (lvl === citeLvl + 1 && note) cont = { lvl, obj: cite.notes, key: cite.notes.push(val) - 1 }
|
|
73
|
+
else if (lvl === citeLvl + 2 && sub === 'DATA' && tag === 'TEXT') {
|
|
74
|
+
cite.text = cite.text ? `${cite.text}\n${val}` : val
|
|
75
|
+
cont = { lvl, obj: cite, key: 'text' }
|
|
76
|
+
}
|
|
77
|
+
if (lvl === citeLvl + 1) sub = tag
|
|
78
|
+
continue
|
|
79
|
+
}
|
|
80
|
+
if (ctype === 'SOUR') {
|
|
81
|
+
if (lvl === 1 && tag === 'TITL') {
|
|
82
|
+
cur.title = val
|
|
83
|
+
cont = { lvl, obj: cur, key: 'title' }
|
|
84
|
+
}
|
|
85
|
+
} else if (ctype === 'OBJE') {
|
|
44
86
|
if (lvl === 1) {
|
|
45
87
|
media = tag === 'FILE' && !cur.file ? cur : null
|
|
46
88
|
if (media) media.file = val.trim()
|
|
@@ -50,9 +92,9 @@ export function parseGedcom(text) {
|
|
|
50
92
|
}
|
|
51
93
|
} else if (ctype === 'INDI') {
|
|
52
94
|
if (lvl === 1) {
|
|
53
|
-
evt = null
|
|
54
|
-
|
|
55
|
-
if (
|
|
95
|
+
evt = famc = null
|
|
96
|
+
shownName = tag === 'NAME' && !cur.name
|
|
97
|
+
if (shownName) {
|
|
56
98
|
cur.name = val.replaceAll('/', '').trim()
|
|
57
99
|
const s = val.match(/\/([^/]*)\//)
|
|
58
100
|
cur.surn = s ? s[1].trim() : ''
|
|
@@ -61,24 +103,28 @@ export function parseGedcom(text) {
|
|
|
61
103
|
else if (EVENTS.has(tag)) {
|
|
62
104
|
evt = { tag, val: val.trim() }
|
|
63
105
|
cur.events.push(evt)
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
106
|
+
} else if (tag === 'FAMC') {
|
|
107
|
+
famc = parentLink(stripAt(val))
|
|
108
|
+
cur.parentFams.push(famc)
|
|
109
|
+
}
|
|
67
110
|
else if (tag === 'FAMS') cur.fams.push(stripAt(val))
|
|
68
111
|
else if (tag === 'OBJE') cur.media.push(stripAt(val))
|
|
69
|
-
else if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
112
|
+
else if (note) cont = { lvl, obj: cur.notes, key: cur.notes.push(val) - 1 }
|
|
113
|
+
else if (tag === 'SOUR') startCitation(val, 1, cur.sources)
|
|
114
|
+
} else if (lvl === 2 && tag === 'SOUR' && (evt || shownName)) {
|
|
115
|
+
startCitation(val, 2, evt ? (evt.sources ??= []) : cur.sources)
|
|
73
116
|
} else if (evt && lvl === 2) {
|
|
74
117
|
if (tag === 'DATE') evt.date = val.trim()
|
|
75
118
|
else if (tag === 'PLAC') evt.plac = val.trim()
|
|
76
119
|
else if (tag === 'TYPE') evt.type = val.trim()
|
|
77
120
|
else if (tag === 'RESN') evt.resn = val.trim()
|
|
121
|
+
else if (note) cont = { lvl, obj: (evt.notes ??= []), key: evt.notes.push(val) - 1 }
|
|
78
122
|
} else if (evt && phrase) {
|
|
79
123
|
evt.phrase = val.trim()
|
|
80
|
-
} else if (
|
|
81
|
-
|
|
124
|
+
} else if (famc && lvl === 2 && (tag === 'PEDI' || tag === 'STAT')) {
|
|
125
|
+
famc[tag.toLowerCase()] = val.trim().toUpperCase()
|
|
126
|
+
} else if (famc && lvl === 3 && sub === 'PEDI' && tag === 'PHRASE') {
|
|
127
|
+
famc.phrase = val.trim()
|
|
82
128
|
}
|
|
83
129
|
} else {
|
|
84
130
|
if (lvl === 1) {
|
|
@@ -94,10 +140,56 @@ export function parseGedcom(text) {
|
|
|
94
140
|
} else if (evt && phrase) evt.phrase = val.trim()
|
|
95
141
|
}
|
|
96
142
|
}
|
|
143
|
+
for (const c of pointers) c.title = sour.get(c.sour)?.title ?? ''
|
|
97
144
|
for (const p of indi.values()) {
|
|
145
|
+
for (const [key, tag] of [['birt', 'BIRT'], ['deat', 'DEAT']]) {
|
|
146
|
+
const all = p.events.filter(e => e.tag === tag)
|
|
147
|
+
p[key] = all.find(e => !restricted(e) && yr(e.date)) ?? all[0] ?? {}
|
|
148
|
+
}
|
|
98
149
|
p.media = p.media.flatMap(id => obje.get(id) ?? [])
|
|
99
150
|
}
|
|
100
|
-
|
|
151
|
+
const warnings = checkLinks(indi, fam)
|
|
152
|
+
for (const p of indi.values()) p.famc = p.parentFams.find(l => l.stat !== 'DISPROVEN' && fam.has(l.fam))?.fam ?? null
|
|
153
|
+
return { indi, fam, warnings }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const parentLink = fam => ({ fam, pedi: '', phrase: '', stat: '' })
|
|
157
|
+
|
|
158
|
+
// Children in a family whose main parent family it is; ids with no record stay in.
|
|
159
|
+
export const mainChildren = (f, indi) => f.chil.filter(c => !indi.has(c) || indi.get(c).famc === f.id)
|
|
160
|
+
|
|
161
|
+
// Warns about links recorded on one side only, and shows a child link on both sides,
|
|
162
|
+
// marked with the side that recorded it (only: 'child' or 'family').
|
|
163
|
+
function checkLinks(indi, fam) {
|
|
164
|
+
const warnings = []
|
|
165
|
+
const warn = (from, tag, to, back) => warnings.push(`${from} ${tag} @${to}@ has no matching ${back} in ${to}`)
|
|
166
|
+
for (const p of indi.values()) {
|
|
167
|
+
for (const link of p.parentFams) {
|
|
168
|
+
const fid = link.fam
|
|
169
|
+
const f = fam.get(fid)
|
|
170
|
+
if (f?.chil.includes(p.id)) continue
|
|
171
|
+
warn(p.id, 'FAMC', fid, 'CHIL')
|
|
172
|
+
if (!f) continue
|
|
173
|
+
link.only = 'child'
|
|
174
|
+
f.chil.push(p.id)
|
|
175
|
+
}
|
|
176
|
+
for (const fid of p.fams) {
|
|
177
|
+
const f = fam.get(fid)
|
|
178
|
+
if (f?.husb !== p.id && f?.wife !== p.id) warn(p.id, 'FAMS', fid, 'HUSB or WIFE')
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (const f of fam.values()) {
|
|
182
|
+
for (const [tag, id] of [['HUSB', f.husb], ['WIFE', f.wife]]) {
|
|
183
|
+
if (id && !indi.get(id)?.fams.includes(f.id)) warn(f.id, tag, id, 'FAMS')
|
|
184
|
+
}
|
|
185
|
+
for (const c of f.chil) {
|
|
186
|
+
const p = indi.get(c)
|
|
187
|
+
if (p?.parentFams.some(l => l.fam === f.id)) continue
|
|
188
|
+
warn(f.id, 'CHIL', c, 'FAMC')
|
|
189
|
+
p?.parentFams.push({ ...parentLink(f.id), only: 'family' })
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return warnings
|
|
101
193
|
}
|
|
102
194
|
|
|
103
195
|
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
|
|
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.
|
|
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.
|
|
36
|
+
if (f) kids.push(...mainChildren(f, this.indi))
|
|
36
37
|
}
|
|
37
38
|
return kids
|
|
38
39
|
}
|
package/lib/living.mjs
CHANGED
|
@@ -1,22 +1,33 @@
|
|
|
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
|
|
5
|
-
const
|
|
6
|
-
|
|
6
|
+
const births = [p.birt, ...(p.events ?? []).filter(e => e.tag === 'BIRT')]
|
|
7
|
+
const years = births.flatMap(e => e?.date?.match(/\d{4}/g) ?? [])
|
|
8
|
+
return !years.length || year - Math.max(...years) <= 100
|
|
7
9
|
}
|
|
8
10
|
|
|
11
|
+
// A Private person keeps only the main parent family, without PEDI or STAT.
|
|
9
12
|
const stub = ({ id, famc, fams }) => ({
|
|
10
13
|
id, name: 'Private', surn: '', giv: '', sex: '', private: true,
|
|
11
|
-
birt: {}, deat: {}, events: [], famc, fams, media: [], notes: [],
|
|
14
|
+
birt: {}, deat: {}, events: [], famc, parentFams: famc ? [parentLink(famc)] : [], fams, media: [], notes: [], sources: [],
|
|
12
15
|
})
|
|
13
16
|
|
|
14
17
|
export function anonymize(indi, fam, year) {
|
|
15
18
|
const living = new Set([...indi.values()].filter(p => isLiving(p, year)).map(p => p.id))
|
|
16
19
|
const withheld = new Set([...indi.values()].filter(p => living.has(p.id)).flatMap(p => p.media.map(m => m.file)))
|
|
17
|
-
const keep = p =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
21
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)])) }
|
|
22
33
|
}
|
package/lib/news.mjs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// What's new: the owner's narrative entries, and the change records stamped at each deploy.
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { restricted } from './gedcom.mjs'
|
|
5
|
+
import { diffTrees, describe, readGedcom } from './diff.mjs'
|
|
6
|
+
import { esc } from './html.mjs'
|
|
7
|
+
import { redact } from './redact.mjs'
|
|
8
|
+
|
|
9
|
+
export const count = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`
|
|
10
|
+
|
|
11
|
+
const INLINE = /\*\*(?!\s)(.+?)(?<!\s)\*\*|\*(?!\s)(.+?)(?<!\s)\*|\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)|(https?:\/\/\S*[^\s.,;:!?)'"\]])|@(\w+)@/g
|
|
12
|
+
|
|
13
|
+
function inline(s, mention) {
|
|
14
|
+
let html = '', at = 0
|
|
15
|
+
for (const m of s.matchAll(INLINE)) {
|
|
16
|
+
const [all, strong, em, text, href, url, xref] = m
|
|
17
|
+
html += esc(s.slice(at, m.index))
|
|
18
|
+
at = m.index + all.length
|
|
19
|
+
if (strong) html += `<strong>${inline(strong, mention)}</strong>`
|
|
20
|
+
else if (em) html += `<em>${inline(em, mention)}</em>`
|
|
21
|
+
else if (href) html += `<a href="${esc(href)}">${esc(text)}</a>`
|
|
22
|
+
else if (url) html += `<a href="${esc(url)}">${esc(url)}</a>`
|
|
23
|
+
else html += mention(xref) ?? esc(all)
|
|
24
|
+
}
|
|
25
|
+
return html + esc(s.slice(at))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The documented subset: a `# Title` first line, paragraphs, *em*, **strong**, http(s) links and @xref@ mentions.
|
|
29
|
+
// mention(id) returns the HTML for a person, or null to leave the mention as written.
|
|
30
|
+
export function markdown(text, mention) {
|
|
31
|
+
const lines = redact(text).replace(/\r\n?/g, '\n').split('\n')
|
|
32
|
+
const title = lines[0].match(/^# (.+)/)?.[1].trim()
|
|
33
|
+
const paras = (title ? lines.slice(1) : lines).join('\n').split(/\n\s*\n/).map(p => p.trim()).filter(Boolean)
|
|
34
|
+
return { title: title ? inline(title, mention) : '', html: paras.map(p => `<p>${inline(p, mention)}</p>`).join('') }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const STAMP = /^(\d{4}-\d{2}-\d{2})(?:-(\d+))?\.changes\.json$/
|
|
38
|
+
|
|
39
|
+
export function readNews(dir) {
|
|
40
|
+
const files = fs.existsSync(dir) ? fs.readdirSync(dir).sort() : []
|
|
41
|
+
const entries = new Map(files.filter(f => f.endsWith('.md')).map(f => [f.slice(0, -3), fs.readFileSync(path.join(dir, f), 'utf8')]))
|
|
42
|
+
const order = f => {
|
|
43
|
+
const [, date, n = '1'] = f.match(STAMP)
|
|
44
|
+
return `${date} ${n.padStart(6, '0')}`
|
|
45
|
+
}
|
|
46
|
+
const deploys = files.filter(f => STAMP.test(f)).map(file => {
|
|
47
|
+
try {
|
|
48
|
+
return { entries: [], changes: [], ...JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8')), file }
|
|
49
|
+
} catch (e) {
|
|
50
|
+
throw new Error(`${path.join(dir, file)}: ${e.message}`)
|
|
51
|
+
}
|
|
52
|
+
}).sort((a, b) => order(b.file).localeCompare(order(a.file)))
|
|
53
|
+
const stamped = new Set(deploys.flatMap(d => d.entries))
|
|
54
|
+
return { entries, deploys, pending: [...entries.keys()].filter(e => !stamped.has(e)) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const localDate = d => [d.getFullYear(), d.getMonth() + 1, d.getDate()].map(n => String(n).padStart(2, '0')).join('-')
|
|
58
|
+
|
|
59
|
+
export function stampNews(config, oldFile, { today = new Date(), log = console.log } = {}) {
|
|
60
|
+
if (!config.news) throw new Error('the config has no "news" directory to stamp')
|
|
61
|
+
const changes = diffTrees(readGedcom(oldFile), readGedcom(config.gedcom))
|
|
62
|
+
const { pending: entries } = readNews(config.news)
|
|
63
|
+
if (!changes.length && !entries.length) {
|
|
64
|
+
log('Nothing to stamp: no changes and no new entries.')
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
const date = localDate(today)
|
|
68
|
+
fs.mkdirSync(config.news, { recursive: true })
|
|
69
|
+
let file = path.join(config.news, `${date}.changes.json`)
|
|
70
|
+
for (let n = 2; fs.existsSync(file); n++) file = path.join(config.news, `${date}-${n}.changes.json`)
|
|
71
|
+
fs.writeFileSync(file, JSON.stringify({ date, entries, changes }, null, 2) + '\n')
|
|
72
|
+
log(`Wrote ${path.relative(process.cwd(), file)}: ${count(entries.length, 'entry', 'entries')}, ${count(changes.length, 'change')}`)
|
|
73
|
+
return file
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// A fact restricted in today's GEDCOM drops out of past entries, and so do its earlier versions. Walking back from
|
|
77
|
+
// today, a fact changed to a hidden or restricted version, or removed while restricted, is hidden in older deploys.
|
|
78
|
+
const FACT = ['tag', 'type', 'val', 'date', 'phrase', 'plac']
|
|
79
|
+
const sameFact = (a, b) => FACT.every(k => (a[k] ?? '') === (b[k] ?? ''))
|
|
80
|
+
const FACTS = new Set(['event', 'preferred', 'marriage'])
|
|
81
|
+
function unrestricted(deploys, tree) {
|
|
82
|
+
const hidden = new Map()
|
|
83
|
+
const hide = (key, f) => hidden.set(key, [...(hidden.get(key) ?? []), f])
|
|
84
|
+
const isHidden = (key, f) => !!f && !!hidden.get(key)?.some(v => sameFact(v, f))
|
|
85
|
+
const key = r => (r.kind === 'marriage' ? r.fam : r.id)
|
|
86
|
+
for (const [id, p] of tree.indi) for (const e of p.events.filter(restricted)) hide(id, e)
|
|
87
|
+
for (const [id, f] of tree.fam) if (restricted(f.marr)) hide(id, { tag: 'MARR', ...f.marr })
|
|
88
|
+
return deploys.map(d => {
|
|
89
|
+
const kept = d.changes.filter(r => ![r.event, ...(FACTS.has(r.kind) ? [r.from, r.to, r.marr] : [])].some(f => isHidden(key(r), f)))
|
|
90
|
+
for (const r of d.changes) {
|
|
91
|
+
if (r.op === 'added' || !['event', 'marriage'].includes(r.kind)) continue
|
|
92
|
+
const earlier = r.from ?? r.marr, later = r.op === 'changed' ? r.to : earlier
|
|
93
|
+
if (restricted(later) || isHidden(key(r), later)) hide(key(r), earlier)
|
|
94
|
+
}
|
|
95
|
+
return kept
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
|
|
100
|
+
const longDate = (d, len) => {
|
|
101
|
+
const [y, m, day] = (d ?? '').split('-').map(Number)
|
|
102
|
+
return MONTHS[m - 1] && day ? `${day} ${MONTHS[m - 1].slice(0, len)} ${y}` : d ?? ''
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Each deploy's What's new section, and the people it changed or mentioned under this build's rules.
|
|
106
|
+
// indi is the built (possibly anonymized) tree; tree is the parsed GEDCOM the records are described against.
|
|
107
|
+
export function newsSections(news, indi, tree, config, warn) {
|
|
108
|
+
const shown = id => indi.has(id) && !indi.get(id).private
|
|
109
|
+
const mention = (file, people) => id => {
|
|
110
|
+
const p = indi.get(id)
|
|
111
|
+
if (!p) {
|
|
112
|
+
warn(`news ${file}: @${id}@ is not in the tree`)
|
|
113
|
+
return null
|
|
114
|
+
}
|
|
115
|
+
if (p.private) return 'Private'
|
|
116
|
+
people.add(id)
|
|
117
|
+
return `<a href="${id}.html">${esc(p.name || '(unnamed)')}</a>`
|
|
118
|
+
}
|
|
119
|
+
// The preview, when there is one, is newer than every deploy.
|
|
120
|
+
const all = [news.preview, ...news.deploys].filter(Boolean)
|
|
121
|
+
// People removed since keep the snapshot their newest removal recorded, so older records can still name them.
|
|
122
|
+
const snapshots = all.flatMap(d => d.changes).filter(c => c.kind === 'person' && c.op === 'removed').map(c => [c.id, c.was]).reverse()
|
|
123
|
+
const past = { indi: new Map([...tree.indi, ...snapshots.filter(([id]) => !tree.indi.has(id))]), fam: tree.fam }
|
|
124
|
+
const linked = i => {
|
|
125
|
+
const p = indi.get(i.id), label = shown(i.id) ? redact(`${p.name || '(unnamed)'} (${i.id})`) : null
|
|
126
|
+
const at = label ? i.text.indexOf(label) : -1
|
|
127
|
+
if (at < 0) return esc(i.text)
|
|
128
|
+
return esc(i.text.slice(0, at)) + `<a href="${i.id}.html">${esc(label)}</a>` + esc(i.text.slice(at + label.length))
|
|
129
|
+
}
|
|
130
|
+
const kept = unrestricted(all, tree)
|
|
131
|
+
return all.map((d, n) => {
|
|
132
|
+
const people = new Set()
|
|
133
|
+
const stories = [...d.entries].sort().flatMap(e => {
|
|
134
|
+
if (!news.entries.has(e)) {
|
|
135
|
+
warn(`news ${d.file}: ${e}.md not found`)
|
|
136
|
+
return []
|
|
137
|
+
}
|
|
138
|
+
const { title, html } = markdown(news.entries.get(e), mention(`${e}.md`, people))
|
|
139
|
+
return title || html ? [`<div class=card>${title ? `<h3>${title}</h3>` : ''}${html}</div>`] : []
|
|
140
|
+
})
|
|
141
|
+
const items = describe(kept[n], past, config)
|
|
142
|
+
for (const i of items) if (shown(i.id)) people.add(i.id)
|
|
143
|
+
const pending = d === news.preview
|
|
144
|
+
const anchor = pending ? 'pending' : path.basename(d.file, '.changes.json')
|
|
145
|
+
const list = items.map(i => `<li>${linked(i)}` +
|
|
146
|
+
(i.lines.length ? `<ul>${i.lines.map(l => `<li>${esc(l)}</li>`).join('')}</ul>` : '') + '</li>').join('')
|
|
147
|
+
const body = stories.join('') + (list ? `<ul class=changes>${list}</ul>` : '')
|
|
148
|
+
const heading = `<h2 id=${anchor}>${esc(pending ? 'Not yet published' : longDate(d.date))}</h2>`
|
|
149
|
+
const html = body ? heading + body : pending ? `${heading}<p class=sub>Nothing yet.</p>` : ''
|
|
150
|
+
return { anchor, date: d.date, pending, html, people }
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// For each person changed or mentioned: the latest stamped section, and whether the preview has them.
|
|
155
|
+
// Sections are newest first.
|
|
156
|
+
export function lastUpdated(sections) {
|
|
157
|
+
const updated = new Map()
|
|
158
|
+
for (const s of sections) {
|
|
159
|
+
for (const id of s.people) {
|
|
160
|
+
const u = updated.get(id) ?? {}
|
|
161
|
+
if (s.pending) u.pending = true
|
|
162
|
+
else if (!u.date) Object.assign(u, { anchor: s.anchor, date: longDate(s.date, 3) })
|
|
163
|
+
updated.set(id, u)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return updated
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function whatsNewPage(sections, r) {
|
|
170
|
+
const title = "What's new"
|
|
171
|
+
return r.page(title, `<p class=back><a href="index.html">← All people</a></p><h1>${esc(title)}</h1>` +
|
|
172
|
+
(sections.map(s => s.html).join('') || '<p class=sub>Nothing yet.</p>'))
|
|
173
|
+
}
|