| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | |
| 6 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 7 | ) |
| 8 | |
| 9 | // List items (§10.1). Nested items nest; a paragraph child is a hard |
| 10 | // break in its item; every other child is written straight after its |
| 11 | // item, which ends the list there (Paul, Q10, 2026-11-17). The numbers |
| 12 | // stored stay right, though Mapa draws a split list from 1 again. |
| 13 | |
| 14 | // isListItem is a block that is a list item. |
| 15 | func isListItem(block Block) bool { |
| 16 | if !block.Known { |
| 17 | return false |
| 18 | } |
| 19 | switch block.Type { |
| 20 | case "bulleted_list_item", "numbered_list_item", "to_do": |
| 21 | return true |
| 22 | } |
| 23 | return false |
| 24 | } |
| 25 | |
| 26 | func (mapper *mapper) listItem(block Block, number int) { |
| 27 | mapper.item(block, 0, number) |
| 28 | } |
| 29 | |
| 30 | // item writes one item at level, then its children. |
| 31 | func (mapper *mapper) item(block Block, level, number int) { |
| 32 | lines := mapper.inline(block.Text, marks{}) |
| 33 | if len(lines) > 0 { |
| 34 | lines[0] = trimLeading(lines[0]) |
| 35 | } |
| 36 | entry := written{kind: markup.Item, level: level, |
| 37 | ordered: block.Type == "numbered_list_item", number: number, |
| 38 | lines: lines} |
| 39 | if block.Type == "to_do" { |
| 40 | entry.task = " " |
| 41 | if block.Checked { |
| 42 | entry.task = "x" |
| 43 | } |
| 44 | } |
| 45 | before := len(mapper.out) |
| 46 | mapper.emit(entry) |
| 47 | open := len(mapper.out) > before |
| 48 | mapper.itemChildren(block.Children, level, open) |
| 49 | } |
| 50 | |
| 51 | // itemChildren writes an item's children. While nothing has been |
| 52 | // written after the item, a paragraph child continues it. |
| 53 | func (mapper *mapper) itemChildren(children []Block, level int, |
| 54 | open bool) { |
| 55 | number := 0 |
| 56 | for _, child := range children { |
| 57 | if child.Type == "numbered_list_item" && child.Known { |
| 58 | number++ |
| 59 | } else { |
| 60 | number = 0 |
| 61 | } |
| 62 | switch { |
| 63 | case isListItem(child): |
| 64 | mapper.item(child, level+1, number) |
| 65 | open = false |
| 66 | case open && child.Known && child.Type == "paragraph": |
| 67 | mapper.continueItem(child) |
| 68 | open = len(child.Children) == 0 |
| 69 | default: |
| 70 | mapper.loss(LossNesting) |
| 71 | mapper.block(child, 0) |
| 72 | open = false |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // continueItem adds a paragraph child to the last item as more lines. |
| 78 | func (mapper *mapper) continueItem(child Block) { |
| 79 | last := &mapper.out[len(mapper.out)-1] |
| 80 | last.lines = append(last.lines, mapper.inline(child.Text, marks{})...) |
| 81 | mapper.hoist(child.Children, LossIndent) |
| 82 | } |
| 83 | |
| 84 | // quote is a quote, or a callout: a quote with its emoji first. Text |
| 85 | // children follow as more lines of the quote; every other child, and |
| 86 | // every child's own children, is written straight after it, counted. |
| 87 | func (mapper *mapper) quote(block Block, _ int) { |
| 88 | lines := mapper.inline(block.Text, marks{}) |
| 89 | if block.Type == "callout" { |
| 90 | lines = mapper.calloutIcon(block, lines) |
| 91 | } |
| 92 | var after []Block |
| 93 | number := 0 |
| 94 | for _, child := range block.Children { |
| 95 | prefix, ok := quotedPrefix(child, &number) |
| 96 | if !ok { |
| 97 | after = append(after, child) |
| 98 | continue |
| 99 | } |
| 100 | more := mapper.inline(child.Text, marks{}) |
| 101 | if len(more) > 0 { |
| 102 | more[0] = prefixed(trimLeading(more[0]), prefix...) |
| 103 | } |
| 104 | lines = append(lines, more...) |
| 105 | after = append(after, child.Children...) |
| 106 | } |
| 107 | mapper.emit(written{kind: markup.Quote, lines: lines}) |
| 108 | mapper.hoist(after, LossHoisted) |
| 109 | } |
| 110 | |
| 111 | // quotedPrefix is how a text child reads as a line of a quote: a list |
| 112 | // item keeps its marker, as text. ok is false for any other child. |
| 113 | func quotedPrefix(child Block, number *int) ([]part, bool) { |
| 114 | if child.Type != "numbered_list_item" { |
| 115 | *number = 0 |
| 116 | } |
| 117 | if !child.Known { |
| 118 | return nil, false |
| 119 | } |
| 120 | switch child.Type { |
| 121 | case "paragraph": |
| 122 | return nil, true |
| 123 | case "bulleted_list_item": |
| 124 | return []part{fixed("- ")}, true |
| 125 | case "numbered_list_item": |
| 126 | *number++ |
| 127 | return []part{fixed(strconv.Itoa(*number) + ". ")}, true |
| 128 | case "to_do": |
| 129 | if child.Checked { |
| 130 | return []part{fixed("- [x] ")}, true |
| 131 | } |
| 132 | return []part{fixed("- [ ] ")}, true |
| 133 | } |
| 134 | return nil, false |
| 135 | } |
| 136 | |
| 137 | // calloutIcon puts a callout's emoji first. Its colour, and an icon that |
| 138 | // is not an emoji, are counted. |
| 139 | func (mapper *mapper) calloutIcon(block Block, lines []line) []line { |
| 140 | if block.Color != "" && block.Color != "default" { |
| 141 | mapper.loss(LossColor) |
| 142 | } |
| 143 | if block.Emoji == "" { |
| 144 | if block.IconType != "" { |
| 145 | mapper.loss(LossCallout) |
| 146 | } |
| 147 | return lines |
| 148 | } |
| 149 | if len(lines) == 0 { |
| 150 | return []line{{lit(block.Emoji)}} |
| 151 | } |
| 152 | lines[0] = prefixed(lines[0], lit(block.Emoji+" ")) |
| 153 | return lines |
| 154 | } |
| 155 | |