| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | ) |
| 9 | |
| 10 | // What the mapper means is computed before anything is written (§11.4): |
| 11 | // each block's kind, level, task box and lines, and each line as parts. |
| 12 | // A part is either literal text from Notion, which must read as literal |
| 13 | // text, or syntax the mapper generated. The structure check (check.go) |
| 14 | // holds the written block to exactly this. |
| 15 | |
| 16 | // partKind is what a part is. |
| 17 | type partKind int |
| 18 | |
| 19 | const ( |
| 20 | // literal is Notion's own text, neutralised where it would read as |
| 21 | // markup (neutral.go). |
| 22 | literal partKind = iota |
| 23 | // delim is generated plain text: emphasis delimiters, a list |
| 24 | // marker inside a quote, the placeholders' fixed words, "In: ". |
| 25 | delim |
| 26 | // wiki is a generated [[target]] or [[target|shown]]. |
| 27 | wiki |
| 28 | // web is a generated [label](url). |
| 29 | web |
| 30 | // code is a generated `text`. |
| 31 | code |
| 32 | // image is a generated : the run's answer for |
| 33 | // an image Mapa holds, which markup reads as an image span (images |
| 34 | // spec §2.1). |
| 35 | image |
| 36 | // bare is a generated bare address, past markup.MaxAddress, which |
| 37 | // Mapa's autolink links by itself; url is the address exactly. |
| 38 | bare |
| 39 | ) |
| 40 | |
| 41 | // part is one piece of a line. |
| 42 | type part struct { |
| 43 | kind partKind |
| 44 | // text is a literal's or delimiter's text, a code span's code, a |
| 45 | // web link's label or a wiki link's shown text. |
| 46 | text string |
| 47 | target string |
| 48 | url string |
| 49 | } |
| 50 | |
| 51 | // source is the part as written. |
| 52 | func (piece part) source() string { |
| 53 | switch piece.kind { |
| 54 | case wiki: |
| 55 | if piece.text == "" { |
| 56 | return "[[" + piece.target + "]]" |
| 57 | } |
| 58 | return "[[" + piece.target + "|" + piece.text + "]]" |
| 59 | case web: |
| 60 | return "[" + piece.text + "](" + piece.url + ")" |
| 61 | case code: |
| 62 | return "`" + piece.text + "`" |
| 63 | case bare: |
| 64 | return piece.url |
| 65 | } |
| 66 | return piece.text // literal, delim and image are their own text |
| 67 | } |
| 68 | |
| 69 | // generated is a link, code span or image: a span of its own when |
| 70 | // parsed. |
| 71 | func (piece part) generated() bool { |
| 72 | return piece.kind == wiki || piece.kind == web || piece.kind == code || |
| 73 | piece.kind == image || piece.kind == bare |
| 74 | } |
| 75 | |
| 76 | // line is one line of a block's inline text. |
| 77 | type line []part |
| 78 | |
| 79 | func (each line) text() string { |
| 80 | var out strings.Builder |
| 81 | for _, piece := range each { |
| 82 | out.WriteString(piece.source()) |
| 83 | } |
| 84 | return out.String() |
| 85 | } |
| 86 | |
| 87 | // written is one block as the mapper means it. |
| 88 | type written struct { |
| 89 | kind string |
| 90 | level int |
| 91 | ordered bool |
| 92 | number int |
| 93 | // task is a to-do's box state, " " or "x", or "" for none. |
| 94 | task string |
| 95 | lines []line |
| 96 | // fence, info and code are a code block's. |
| 97 | fence, info, code string |
| 98 | // plain is the block's text for the fallback (§11.4). |
| 99 | plain string |
| 100 | // literal is a column layout's fence line, as written. |
| 101 | literal string |
| 102 | } |
| 103 | |
| 104 | // inline is the block's inline text: its lines, joined. |
| 105 | func (block written) inline() string { |
| 106 | texts := make([]string, len(block.lines)) |
| 107 | for index, each := range block.lines { |
| 108 | texts[index] = each.text() |
| 109 | } |
| 110 | return strings.Join(texts, "\n") |
| 111 | } |
| 112 | |
| 113 | // source is the block as written: its syntax around its inline text. |
| 114 | func (block written) source() string { |
| 115 | if markup.IsFence(block.kind) { |
| 116 | return block.literal |
| 117 | } |
| 118 | switch block.kind { |
| 119 | case markup.Code: |
| 120 | return codeSource(block) |
| 121 | case markup.Rule: |
| 122 | return "---" |
| 123 | case markup.Heading: |
| 124 | return strings.Repeat("#", block.level) + " " + block.inline() |
| 125 | case markup.Quote: |
| 126 | return quoteSource(block) |
| 127 | case markup.Item: |
| 128 | return itemPrefix(block) + block.inline() |
| 129 | } |
| 130 | return block.inline() |
| 131 | } |
| 132 | |
| 133 | func codeSource(block written) string { |
| 134 | out := block.fence + block.info + "\n" |
| 135 | if block.code != "" { |
| 136 | out += block.code + "\n" |
| 137 | } |
| 138 | return out + block.fence |
| 139 | } |
| 140 | |
| 141 | func quoteSource(block written) string { |
| 142 | texts := make([]string, len(block.lines)) |
| 143 | for index, each := range block.lines { |
| 144 | texts[index] = "> " + each.text() |
| 145 | } |
| 146 | return strings.Join(texts, "\n") |
| 147 | } |
| 148 | |
| 149 | // itemPrefix is an item's indent, marker and task box. |
| 150 | func itemPrefix(block written) string { |
| 151 | marker := "- " |
| 152 | if block.ordered { |
| 153 | marker = strconv.Itoa(block.number) + ". " |
| 154 | } |
| 155 | box := "" |
| 156 | if block.task != "" { |
| 157 | box = "[" + block.task + "] " |
| 158 | } |
| 159 | return strings.Repeat(" ", block.level) + marker + box |
| 160 | } |
| 161 | |
| 162 | // lit and gen are part constructors. |
| 163 | func lit(text string) part { return part{kind: literal, text: text} } |
| 164 | func fixed(text string) part { return part{kind: delim, text: text} } |
| 165 | |