| 1 | export interface SessionGeneratedPage { |
| 2 | pageNumber: number |
| 3 | title: string |
| 4 | pageId?: string |
| 5 | htmlPath?: string |
| 6 | } |
| 7 | |
| 8 | export interface SessionFailedPage { |
| 9 | pageId?: string |
| 10 | title?: string |
| 11 | reason?: string |
| 12 | } |
| 13 | |
| 14 | export interface SessionMetadata { |
| 15 | lastRunId?: string |
| 16 | entryMode?: 'multi_page' | 'single_page' |
| 17 | generatedPages?: SessionGeneratedPage[] |
| 18 | failedPages?: SessionFailedPage[] |
| 19 | indexPath?: string |
| 20 | projectId?: string |
| 21 | // pptx-import specific |
| 22 | source?: string |
| 23 | importedAt?: number |
| 24 | originalFileName?: string |
| 25 | warnings?: string[] |
| 26 | } |
| 27 | |
| 28 | export function parseSessionMetadata(raw: string | undefined | null): SessionMetadata { |
| 29 | if (!raw || !raw.trim()) return {} |
| 30 | try { |
| 31 | return JSON.parse(raw) as SessionMetadata |
| 32 | } catch { |
| 33 | return {} |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Derive a stable pageNumber from pageId when it follows the `page-N` convention. |
| 39 | * Falls back to `fallback` when pageId doesn't match the pattern. |
| 40 | */ |
| 41 | export function derivePageNumber(pageId: string | undefined, fallback: number): number { |
| 42 | if (pageId) { |
| 43 | const n = Number(pageId.match(/^page-(\d+)$/i)?.[1]) |
| 44 | if (n > 0) return n |
| 45 | } |
| 46 | return fallback |
| 47 | } |
| 48 |