| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | ) |
| 9 | |
| 10 | // Blocks (§10.1). Each Notion block maps to Mapa's subset, or to a |
| 11 | // placeholder, or to a counted loss. Nothing is dropped silently. |
| 12 | |
| 13 | // blocks maps siblings in order. Numbered items are numbered in |
| 14 | // sequence within each run of them. |
| 15 | func (mapper *mapper) blocks(siblings []Block) { |
| 16 | number := 0 |
| 17 | for _, block := range siblings { |
| 18 | if block.Type == "numbered_list_item" && block.Known { |
| 19 | number++ |
| 20 | } else { |
| 21 | number = 0 |
| 22 | } |
| 23 | mapper.block(block, number) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // block maps one block by its type. |
| 28 | func (mapper *mapper) block(block Block, number int) { |
| 29 | if !block.Known { |
| 30 | mapper.unknown(block) |
| 31 | return |
| 32 | } |
| 33 | if block.Color != "" && block.Color != "default" && |
| 34 | block.Type != "callout" { |
| 35 | mapper.loss(LossColor) |
| 36 | } |
| 37 | if handle, ok := handlers[block.Type]; ok { |
| 38 | handle(mapper, block, number) |
| 39 | return |
| 40 | } |
| 41 | if strings.HasPrefix(block.Type, "heading_") { |
| 42 | mapper.heading(block) |
| 43 | return |
| 44 | } |
| 45 | mapper.unknown(block) |
| 46 | } |
| 47 | |
| 48 | // handler maps one kind of block. number is a numbered item's. |
| 49 | type handler func(*mapper, Block, int) |
| 50 | |
| 51 | var handlers map[string]handler |
| 52 | |
| 53 | func init() { |
| 54 | handlers = map[string]handler{ |
| 55 | "paragraph": (*mapper).paragraph, |
| 56 | "bulleted_list_item": (*mapper).listItem, |
| 57 | "numbered_list_item": (*mapper).listItem, |
| 58 | "to_do": (*mapper).listItem, |
| 59 | "toggle": (*mapper).toggle, |
| 60 | "quote": (*mapper).quote, |
| 61 | "callout": (*mapper).quote, |
| 62 | "code": (*mapper).codeBlock, |
| 63 | "divider": (*mapper).divider, |
| 64 | "table": (*mapper).table, |
| 65 | "column_list": (*mapper).columns, |
| 66 | "column": (*mapper).container, |
| 67 | "tab": (*mapper).tab, |
| 68 | "synced_block": (*mapper).synced, |
| 69 | "meeting_notes": (*mapper).meeting, |
| 70 | "child_page": (*mapper).childPage, |
| 71 | "link_to_page": (*mapper).linkToPage, |
| 72 | "child_database": (*mapper).childDatabase, |
| 73 | "image": (*mapper).image, |
| 74 | "video": (*mapper).file, |
| 75 | "audio": (*mapper).file, |
| 76 | "pdf": (*mapper).file, |
| 77 | "file": (*mapper).file, |
| 78 | "embed": (*mapper).embed, |
| 79 | "bookmark": (*mapper).embed, |
| 80 | "link_preview": (*mapper).embed, |
| 81 | "equation": (*mapper).equationBlock, |
| 82 | "breadcrumb": (*mapper).dropped, |
| 83 | "table_of_contents": (*mapper).dropped, |
| 84 | "template": (*mapper).dropped, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // paragraph is a paragraph. Its children, Notion's indented blocks, |
| 89 | // follow it at the top level, each counted. |
| 90 | func (mapper *mapper) paragraph(block Block, _ int) { |
| 91 | mapper.emit(written{kind: markup.Paragraph, |
| 92 | lines: mapper.inline(block.Text, marks{})}) |
| 93 | mapper.hoist(block.Children, LossIndent) |
| 94 | } |
| 95 | |
| 96 | // hoist writes children after their block, each counted under kind. |
| 97 | func (mapper *mapper) hoist(children []Block, kind string) { |
| 98 | for range children { |
| 99 | mapper.loss(kind) |
| 100 | } |
| 101 | mapper.blocks(children) |
| 102 | } |
| 103 | |
| 104 | // heading is one line: Notion's line breaks become spaces. A heading's |
| 105 | // level is its number (the page title is the H1 on screen), at most 6. |
| 106 | func (mapper *mapper) heading(block Block) { |
| 107 | level, err := strconv.Atoi(strings.TrimPrefix(block.Type, "heading_")) |
| 108 | if err != nil || level < 1 { |
| 109 | mapper.unknown(block) |
| 110 | return |
| 111 | } |
| 112 | text := trimLeading(joinLines(mapper.inline(block.Text, marks{}))) |
| 113 | mapper.emit(written{kind: markup.Heading, level: min(level, 6), |
| 114 | lines: []line{text}}) |
| 115 | if block.Toggleable { |
| 116 | mapper.loss(LossToggle) |
| 117 | mapper.blocks(block.Children) |
| 118 | return |
| 119 | } |
| 120 | mapper.hoist(block.Children, LossIndent) |
| 121 | } |
| 122 | |
| 123 | // toggle is its summary in bold, then its children as ordinary blocks. |
| 124 | func (mapper *mapper) toggle(block Block, _ int) { |
| 125 | mapper.loss(LossToggle) |
| 126 | mapper.emit(written{kind: markup.Paragraph, |
| 127 | lines: mapper.inline(block.Text, marks{bold: true})}) |
| 128 | mapper.blocks(block.Children) |
| 129 | } |
| 130 | |
| 131 | func (mapper *mapper) divider(Block, int) { |
| 132 | mapper.emit(written{kind: markup.Rule}) |
| 133 | } |
| 134 | |
| 135 | // columns writes a column list of two to four columns as a column |
| 136 | // layout (columns and icons spec §6.2): ":::: columns", each column as |
| 137 | // "::: column" … ":::", then "::::". One column is its blocks in place; |
| 138 | // five or more, or a list inside a column, at any depth, each column's |
| 139 | // blocks in order, counted. |
| 140 | func (mapper *mapper) columns(block Block, _ int) { |
| 141 | count := len(block.Children) |
| 142 | if mapper.inColumn || count < 2 || count > 4 { |
| 143 | if count != 1 { |
| 144 | mapper.loss(LossColumns) |
| 145 | } |
| 146 | for _, column := range block.Children { |
| 147 | mapper.blocks(column.Children) |
| 148 | } |
| 149 | return |
| 150 | } |
| 151 | if unevenWidths(block.Children) { |
| 152 | mapper.loss(LossWidths) |
| 153 | } |
| 154 | mapper.fence(markup.Columns, ":::: columns") |
| 155 | mapper.inColumn = true |
| 156 | for _, column := range block.Children { |
| 157 | mapper.fence(markup.Column, "::: column") |
| 158 | mapper.blocks(column.Children) |
| 159 | mapper.fence(markup.ColumnEnd, ":::") |
| 160 | } |
| 161 | mapper.inColumn = false |
| 162 | mapper.fence(markup.ColumnsEnd, "::::") |
| 163 | } |
| 164 | |
| 165 | // unevenWidths is columns whose width ratios differ: Mapa's layouts are |
| 166 | // even, so the widths are counted (columns and icons spec §6.2). When |
| 167 | // every column has a ratio they are compared with each other, whatever |
| 168 | // they sum to (Fable on phase 4, L5); a column with none is an even |
| 169 | // share, 1/n, against which the others are compared. |
| 170 | func unevenWidths(columns []Block) bool { |
| 171 | share := columns[0].WidthRatio |
| 172 | for _, column := range columns { |
| 173 | if column.WidthRatio == 0 { |
| 174 | share = 1 / float64(len(columns)) |
| 175 | break |
| 176 | } |
| 177 | } |
| 178 | for _, column := range columns { |
| 179 | ratio := column.WidthRatio |
| 180 | if ratio == 0 { |
| 181 | ratio = 1 / float64(len(columns)) |
| 182 | } |
| 183 | if ratio-share > 0.001*share || share-ratio > 0.001*share { |
| 184 | return true |
| 185 | } |
| 186 | } |
| 187 | return false |
| 188 | } |
| 189 | |
| 190 | // fence adds one of a layout's fence lines. |
| 191 | func (mapper *mapper) fence(kind, line string) { |
| 192 | mapper.out = append(mapper.out, written{kind: kind, literal: line}) |
| 193 | } |
| 194 | |
| 195 | // container writes a block's children in place, and nothing of its own. |
| 196 | func (mapper *mapper) container(block Block, _ int) { |
| 197 | mapper.blocks(block.Children) |
| 198 | } |
| 199 | |
| 200 | // tab is its label in bold, then its blocks. |
| 201 | func (mapper *mapper) tab(block Block, _ int) { |
| 202 | mapper.loss(LossTabs) |
| 203 | mapper.emit(written{kind: markup.Paragraph, |
| 204 | lines: mapper.inline(block.Text, marks{bold: true})}) |
| 205 | mapper.blocks(block.Children) |
| 206 | } |
| 207 | |
| 208 | // synced writes the synced content in place: an original's children, or |
| 209 | // a copy's, which the survey read from the copy or its original. |
| 210 | func (mapper *mapper) synced(block Block, _ int) { |
| 211 | if block.OriginalUnshared { |
| 212 | mapper.placeholder(block.SyncedFrom, fixed("a synced block whose "+ |
| 213 | "original wasn't shared with the import.")) |
| 214 | return |
| 215 | } |
| 216 | mapper.loss(LossSynced) |
| 217 | mapper.blocks(block.Children) |
| 218 | } |
| 219 | |
| 220 | // meeting is meeting notes' child blocks, if the API gave any. |
| 221 | func (mapper *mapper) meeting(block Block, _ int) { |
| 222 | if len(block.Children) > 0 { |
| 223 | mapper.blocks(block.Children) |
| 224 | return |
| 225 | } |
| 226 | mapper.loss(LossMeeting) |
| 227 | mapper.placeholder(block.ID, fixed("the meeting notes “"), |
| 228 | lit(firstOf(block.Title, "Untitled")), fixed("” weren't imported.")) |
| 229 | } |
| 230 | |
| 231 | // dropped is a block written as nothing, counted. |
| 232 | func (mapper *mapper) dropped(block Block, _ int) { |
| 233 | mapper.loss(LossBlock + block.Type) |
| 234 | } |
| 235 | |
| 236 | // unknown is a block the subset has no row for: its text, if it has |
| 237 | // any, then its children, and counted by its type. |
| 238 | func (mapper *mapper) unknown(block Block) { |
| 239 | name := block.Type |
| 240 | if block.Type == "unsupported" && block.Unsupported != "" { |
| 241 | name = block.Unsupported |
| 242 | } |
| 243 | mapper.loss(LossBlock + name) |
| 244 | mapper.emit(written{kind: markup.Paragraph, |
| 245 | lines: mapper.inline(block.Text, marks{})}) |
| 246 | mapper.blocks(block.Children) |
| 247 | } |
| 248 | |