// node gen-data.cjs: writes data.js (window.DOC) from the equine draft document, so every reference, citation // count, finding name and type of the boards comes from document.json and none is typed by hand. // Citations are counted as apps/app/src/editor/references/citations.ts does: every finding category and // every finding node, at any depth, adds one per id of its `reference_ids` (zones cite nothing). const fs = require('fs') const path = require('path') const SRC = path.join(__dirname, '../../../supabase/seed/anatomical-sheets/equine/draft/document.json') const doc = JSON.parse(fs.readFileSync(SRC, 'utf8')) // Every citing element, in document order (zone, category, nodes depth first). const citing = [] const walkNodes = (zone, cat, nodes, trail) => { for (const n of nodes) { citing.push({ zone: zone.id, zoneFr: zone.name.fr, trail: [cat.name.fr, ...trail], kind: 'node', id: n.id, fr: n.name.fr, refs: n.reference_ids || [] }) walkNodes(zone, cat, n.children || [], [...trail, n.name.fr]) } } for (const z of doc.finding_zones) { for (const c of z.categories) { citing.push({ zone: z.id, zoneFr: z.name.fr, trail: [], kind: 'category', id: c.id, fr: c.name.fr, refs: c.reference_ids || [] }) walkNodes(z, c, c.children || [], []) } } const counts = new Map() for (const e of citing) for (const id of e.refs) counts.set(id, (counts.get(id) || 0) + 1) const references = doc.references.map((r, i) => ({ id: r.id, index: i, type: r.type, citation: r.citation, url: r.url, doi: r.doi, cited: counts.get(r.id) || 0, citedBy: citing.filter((e) => e.refs.includes(r.id)).map((e) => ({ zone: e.zoneFr, trail: e.trail, fr: e.fr, kind: e.kind })), })) const findings = citing.filter((e) => e.refs.length > 0) .map((e) => ({ zone: e.zone, zoneFr: e.zoneFr, trail: e.trail, fr: e.fr, kind: e.kind, refs: e.refs })) const node = (n) => ({ id: n.id, fr: n.name.fr, children: (n.children || []).map(node) }) const out = { name: doc.name, views: doc.views.map((v) => ({ id: v.id, fr: v.name.fr, parent: v.parent_view_id })), categories: doc.categories.map((c) => ({ id: c.id, fr: c.name.fr, color: c.color })), annotations: doc.annotations.map((a) => ({ id: a.id, view: a.view_id, kind: a.kind, cat: a.category_id, lat: a.laterality, fr: a.name ? a.name.fr : null, en: a.name ? a.name.en : null, zone: a.finding_zone_id, status: a.review_status || 'inherited', g: a.geometry.type === 'circle' ? ['c', a.geometry.cx, a.geometry.cy, a.geometry.r] : ['p', a.geometry.points], })), zones: doc.finding_zones.map((z) => ({ id: z.id, fr: z.name.fr, families: z.categories.map((c) => ({ id: c.id, fr: c.name.fr, children: (c.children || []).map(node) })) })), references, findings, citations: [...counts.values()].reduce((s, n) => s + n, 0), } const body = 'window.DOC = ' + JSON.stringify(out) + '\n' fs.writeFileSync(path.join(__dirname, 'data.js'), '// generated by gen-data.cjs from document.json\n' + body) const zero = references.filter((r) => r.cited === 0).length console.log('references', references.length, 'citations', out.citations, 'uncited', zero, 'citing elements', findings.length, 'zones', out.zones.length, 'bytes', body.length)