| 10 | }; |
| 11 | |
| 12 | export function extractRootDocPagesMeta(rootDoc: Doc): RootDocMetaExtract { |
| 13 | const recordsById = new Map<string, KeyedRecord>(); |
| 14 | const duplicateIds: string[] = []; |
| 15 | |
| 16 | const meta = rootDoc.getMap('meta'); |
| 17 | const pages = meta.get('pages'); |
| 18 | const entries = |
| 19 | pages instanceof YArray |
| 20 | ? pages.toArray() |
| 21 | : Array.isArray(pages) |
| 22 | ? pages |
| 23 | : null; |
| 24 | if (!entries) { |
| 25 | return { recordsById, duplicateIds }; |
| 26 | } |
| 27 | |
| 28 | for (const entry of entries) { |
| 29 | let id: string | null = null; |
| 30 | if (entry instanceof YMap) { |
| 31 | const idRaw = entry.get('id'); |
| 32 | id = |
| 33 | typeof idRaw === 'string' |
| 34 | ? idRaw |
| 35 | : typeof idRaw?.toString === 'function' |
| 36 | ? idRaw.toString() |
| 37 | : null; |
| 38 | } else if (entry && typeof entry === 'object' && !Array.isArray(entry)) { |
| 39 | const record = entry as Record<string, unknown>; |
| 40 | id = |
| 41 | typeof record.id === 'string' |
| 42 | ? record.id |
| 43 | : (record.id?.toString?.() ?? null); |
| 44 | } |
| 45 | |
| 46 | if (!id) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if (recordsById.has(id)) { |
| 51 | duplicateIds.push(id); |
| 52 | } |
| 53 | |
| 54 | const metaPlain = toPlain(entry) as unknown; |
| 55 | if ( |
| 56 | metaPlain && |
| 57 | typeof metaPlain === 'object' && |
| 58 | !Array.isArray(metaPlain) |
| 59 | ) { |
| 60 | recordsById.set(id, metaPlain as KeyedRecord); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return { recordsById, duplicateIds }; |
| 65 | } |
| 66 | |
| 67 | function docLabel(id: string, meta: KeyedRecord) { |
| 68 | const title = meta.title; |