| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 7 | ) |
| 8 | |
| 9 | // The structure check (D9, §11.4). A block as written must parse back |
| 10 | // into exactly the block the mapper meant: its kind, level, numbering |
| 11 | // and task box; no ^id; its inline text byte for byte; each generated |
| 12 | // link and code span as that span and nothing else; and every literal |
| 13 | // byte inside plain text, or inside a bare address Mapa links, which |
| 14 | // the table leaves alone. |
| 15 | |
| 16 | // checkBlock is check. A variable only so a test can make a block fail |
| 17 | // and drive the fallback. |
| 18 | var checkBlock = check |
| 19 | |
| 20 | func check(block written) bool { |
| 21 | source := block.source() |
| 22 | parsed := markup.Parse(source) |
| 23 | if len(parsed) != 1 || parsed[0].Source != source || |
| 24 | parsed[0].Kind != block.kind || parsed[0].Marker != "" { |
| 25 | return false |
| 26 | } |
| 27 | got := parsed[0] |
| 28 | switch block.kind { |
| 29 | case markup.Code: |
| 30 | return markup.Plain(got) == block.code |
| 31 | case markup.Rule: |
| 32 | return true |
| 33 | case markup.Heading: |
| 34 | if got.Level != block.level { |
| 35 | return false |
| 36 | } |
| 37 | case markup.Item: |
| 38 | if got.Level != block.level || got.Ordered != block.ordered { |
| 39 | return false |
| 40 | } |
| 41 | } |
| 42 | return inlineAgrees(got, block) |
| 43 | } |
| 44 | |
| 45 | // inlineAgrees holds the block's inline text to what was meant. |
| 46 | func inlineAgrees(got markup.Block, block written) bool { |
| 47 | inline := markup.Strip(got) |
| 48 | if block.kind == markup.Item { |
| 49 | state, width := markup.TaskBox(inline) |
| 50 | if !strings.EqualFold(state, block.task) { |
| 51 | return false |
| 52 | } |
| 53 | inline = inline[width:] |
| 54 | } |
| 55 | if inline != block.inline() { |
| 56 | return false |
| 57 | } |
| 58 | return spansAgree(inline, block.lines) |
| 59 | } |
| 60 | |
| 61 | // spansAgree walks the parsed spans beside the parts that made them. |
| 62 | func spansAgree(inline string, lines []line) bool { |
| 63 | flat := flatten(lines) |
| 64 | _, owners := owned(flat) |
| 65 | starts := map[int]part{} |
| 66 | offset := 0 |
| 67 | for _, piece := range flat { |
| 68 | if piece.generated() { |
| 69 | starts[offset] = piece |
| 70 | } |
| 71 | offset += len(piece.source()) |
| 72 | } |
| 73 | matched, offset := 0, 0 |
| 74 | for _, span := range markup.Spans(inline) { |
| 75 | width := len(span.Source) |
| 76 | if piece, ok := starts[offset]; ok { |
| 77 | if !spanIs(span, piece) { |
| 78 | return false |
| 79 | } |
| 80 | matched++ |
| 81 | } else if !plainSpan(span, owners[offset:offset+width]) { |
| 82 | return false |
| 83 | } |
| 84 | offset += width |
| 85 | } |
| 86 | return matched == len(starts) |
| 87 | } |
| 88 | |
| 89 | // flatten is a block's lines as one run of parts, a line break between. |
| 90 | func flatten(lines []line) line { |
| 91 | var out line |
| 92 | for index, each := range lines { |
| 93 | if index > 0 { |
| 94 | out = append(out, fixed("\n")) |
| 95 | } |
| 96 | out = append(out, each...) |
| 97 | } |
| 98 | return out |
| 99 | } |
| 100 | |
| 101 | // spanIs is whether a parsed span is exactly the generated part. |
| 102 | func spanIs(span markup.Span, piece part) bool { |
| 103 | if span.Source != piece.source() { |
| 104 | return false |
| 105 | } |
| 106 | switch piece.kind { |
| 107 | case wiki: |
| 108 | return span.Kind == markup.WikiLink && span.Target == piece.target && |
| 109 | span.Anchor == "" && span.Text == piece.text |
| 110 | case web: |
| 111 | return span.Kind == markup.WebLink && span.URL == piece.url && |
| 112 | span.Text == piece.text |
| 113 | case image: |
| 114 | return span.Kind == markup.Image |
| 115 | case bare: |
| 116 | // Exactly one autolink, ending where the address ends: nothing |
| 117 | // after it swallowed, nothing of it dropped (Fable round 4, M1). |
| 118 | return span.Kind == markup.WebLink && span.Source == span.URL && |
| 119 | span.URL == piece.url |
| 120 | } |
| 121 | return span.Kind == markup.InCode && span.Text == piece.text |
| 122 | } |
| 123 | |
| 124 | // plainSpan is a span that holds no generated part: plain text, or a |
| 125 | // bare address made only of literal text. |
| 126 | func plainSpan(span markup.Span, owners []partKind) bool { |
| 127 | switch { |
| 128 | case span.Kind == markup.Text: |
| 129 | for _, owner := range owners { |
| 130 | if owner != literal && owner != delim { |
| 131 | return false |
| 132 | } |
| 133 | } |
| 134 | return true |
| 135 | case span.Kind == markup.WebLink && span.Source == span.URL: |
| 136 | for _, owner := range owners { |
| 137 | if owner != literal { |
| 138 | return false |
| 139 | } |
| 140 | } |
| 141 | return true |
| 142 | } |
| 143 | return false |
| 144 | } |
| 145 | |
| 146 | // The fallback's note, settled through the copy review. |
| 147 | const fallbackNote = "this block couldn't be copied exactly. " + |
| 148 | "Its text follows." |
| 149 | |
| 150 | // fallback is a block that failed the check, which means the mapper has |
| 151 | // a bug: a note, then a fence holding its text. The fence is ```, or |
| 152 | // ~~~ when a line of the text starts with ```; a line starting with the |
| 153 | // chosen fence gets U+200B first, so the fence always closes. |
| 154 | func fallback(text string) []written { |
| 155 | fence := "```" |
| 156 | if closes(text, fence) { |
| 157 | fence = "~~~" |
| 158 | } |
| 159 | lines := strings.Split(text, "\n") |
| 160 | for index, each := range lines { |
| 161 | if markup.ClosesFence(each, fence) { |
| 162 | lines[index] = zeroWidth + each |
| 163 | } |
| 164 | } |
| 165 | note := written{kind: markup.Quote, |
| 166 | lines: []line{{fixed("From Notion: " + fallbackNote)}}} |
| 167 | copied := written{kind: markup.Code, fence: fence, |
| 168 | code: strings.Join(lines, "\n")} |
| 169 | if strings.TrimSpace(text) == "" { |
| 170 | return []written{note} |
| 171 | } |
| 172 | return []written{note, copied} |
| 173 | } |
| 174 | |