| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/mapa/internal/mapa/notionmd" |
| 5 | "amadan.net/oficina/notion.git/notion" |
| 6 | ) |
| 7 | |
| 8 | // The module's typed tree, copied into notionmd's (Notion import spec |
| 9 | // §4). The staged tree Mapa seals is notionmd's, so its format is Mapa's |
| 10 | // to keep, and notionmd never imports the module (gate I8). |
| 11 | |
| 12 | // stagedPage is a surveyed page in notionmd's tree. |
| 13 | func stagedPage(page notion.Page, blocks []notion.Block, parent string, |
| 14 | seq int) notionmd.Page { |
| 15 | staged := notionmd.Page{ID: page.ID, Title: convertRuns(page.Title), |
| 16 | Parent: parent, Created: page.CreatedTime, |
| 17 | Edited: page.LastEditedTime, Seq: seq, Blocks: convertBlocks(blocks)} |
| 18 | if icon := page.Icon; icon != nil { |
| 19 | staged.Icon = ¬ionmd.Look{Type: icon.Type, Emoji: icon.Emoji, |
| 20 | URL: icon.URL} |
| 21 | } |
| 22 | if cover := page.Cover; cover != nil { |
| 23 | staged.Cover = ¬ionmd.Look{Type: cover.Type, URL: cover.URL} |
| 24 | } |
| 25 | return staged |
| 26 | } |
| 27 | |
| 28 | func convertBlocks(blocks []notion.Block) []notionmd.Block { |
| 29 | if len(blocks) == 0 { |
| 30 | return nil |
| 31 | } |
| 32 | out := make([]notionmd.Block, 0, len(blocks)) |
| 33 | for _, block := range blocks { |
| 34 | out = append(out, convertBlock(block)) |
| 35 | } |
| 36 | return out |
| 37 | } |
| 38 | |
| 39 | func convertBlock(block notion.Block) notionmd.Block { |
| 40 | out := notionmd.Block{ID: block.ID, Type: block.Type, |
| 41 | Edited: block.LastEditedTime, Known: block.Known, |
| 42 | Unsupported: block.UnsupportedType, Text: convertRuns(block.RichText), |
| 43 | Caption: convertRuns(block.Caption), Checked: block.Checked, |
| 44 | Toggleable: block.Toggleable, Language: block.Language, |
| 45 | WidthRatio: block.WidthRatio, |
| 46 | Title: block.Title, URL: block.URL, Expression: block.Expression, |
| 47 | Color: block.Color, Cells: convertCells(block.Cells), |
| 48 | SyncedFrom: block.SyncedFrom, |
| 49 | OriginalUnshared: block.OriginalUnshared, |
| 50 | Children: convertBlocks(block.Children)} |
| 51 | if block.Icon != nil { |
| 52 | out.IconType, out.Emoji = block.Icon.Type, block.Icon.Emoji |
| 53 | } |
| 54 | if block.File != nil { |
| 55 | out.File = ¬ionmd.File{Type: block.File.Type, URL: block.File.URL, |
| 56 | Name: block.File.Name} |
| 57 | } |
| 58 | if block.Table != nil { |
| 59 | out.Table = ¬ionmd.Table{Width: block.Table.Width, |
| 60 | ColumnHeader: block.Table.HasColumnHeader, |
| 61 | RowHeader: block.Table.HasRowHeader} |
| 62 | } |
| 63 | if block.LinkTo != nil { |
| 64 | out.LinkTo = ¬ionmd.Target{Type: block.LinkTo.Type, |
| 65 | ID: block.LinkTo.ID} |
| 66 | } |
| 67 | return out |
| 68 | } |
| 69 | |
| 70 | func convertCells(cells [][]notion.Run) [][]notionmd.Run { |
| 71 | if len(cells) == 0 { |
| 72 | return nil |
| 73 | } |
| 74 | out := make([][]notionmd.Run, 0, len(cells)) |
| 75 | for _, cell := range cells { |
| 76 | out = append(out, convertRuns(cell)) |
| 77 | } |
| 78 | return out |
| 79 | } |
| 80 | |
| 81 | func convertRuns(runs []notion.Run) []notionmd.Run { |
| 82 | if len(runs) == 0 { |
| 83 | return nil |
| 84 | } |
| 85 | out := make([]notionmd.Run, 0, len(runs)) |
| 86 | for _, run := range runs { |
| 87 | notes := run.Annotations |
| 88 | converted := notionmd.Run{Type: run.Type, Text: run.Text, |
| 89 | Href: run.Href, Link: run.Link, Bold: notes.Bold, |
| 90 | Italic: notes.Italic, Strike: notes.Strikethrough, |
| 91 | Underline: notes.Underline, Code: notes.Code, |
| 92 | Color: notes.Color, Expression: run.Expression} |
| 93 | if mention := run.Mention; mention != nil { |
| 94 | converted.Mention = ¬ionmd.Mention{Type: mention.Type, |
| 95 | ID: mention.ID, Name: mention.Name, URL: mention.URL} |
| 96 | if mention.Date != nil { |
| 97 | converted.Mention.Start = mention.Date.Start |
| 98 | converted.Mention.End = mention.Date.End |
| 99 | converted.Mention.TimeZone = mention.Date.TimeZone |
| 100 | } |
| 101 | } |
| 102 | out = append(out, converted) |
| 103 | } |
| 104 | return out |
| 105 | } |
| 106 | |
| 107 | // childPages is every child_page in a staged tree, in block order, as |
| 108 | // Refs reached from page: what a resumed survey walks without asking |
| 109 | // Notion (spec §8.3). |
| 110 | func childPages(page notionmd.Page) []notion.Ref { |
| 111 | var refs []notion.Ref |
| 112 | var walk func([]notionmd.Block) |
| 113 | walk = func(blocks []notionmd.Block) { |
| 114 | for _, block := range blocks { |
| 115 | if block.Type == "child_page" { |
| 116 | refs = append(refs, notion.Ref{ID: block.ID, Parent: page.ID}) |
| 117 | continue |
| 118 | } |
| 119 | walk(block.Children) |
| 120 | } |
| 121 | } |
| 122 | walk(page.Blocks) |
| 123 | return refs |
| 124 | } |
| 125 | |