| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 9 | ) |
| 10 | |
| 11 | // Code, tables, links to pages, databases, images and files (§10.1). |
| 12 | |
| 13 | // imageReference is the images spec's form, : the |
| 14 | // run's answer for an image Mapa holds, written as syntax. |
| 15 | var imageReference = regexp.MustCompile( |
| 16 | `^!\[[^\[\]\n]*\]\(/i/[0-9a-z]{1,13}\)$`) |
| 17 | |
| 18 | // codeBlock is a fence with its language, then its caption in italics. |
| 19 | func (mapper *mapper) codeBlock(block Block, _ int) { |
| 20 | text := plainRuns(block.Text) |
| 21 | mapper.fenced(text, language(block.Language)) |
| 22 | mapper.emit(written{kind: markup.Paragraph, |
| 23 | lines: mapper.inline(block.Caption, marks{italic: true})}) |
| 24 | } |
| 25 | |
| 26 | // fenced writes code in a fence that its own lines cannot close: ```, |
| 27 | // or ~~~ when a line starts with ```. Code holding both is written as |
| 28 | // a placeholder with its text fenced, counted (§10.1). |
| 29 | func (mapper *mapper) fenced(text, info string) { |
| 30 | if text == "" { |
| 31 | return |
| 32 | } |
| 33 | ticks, tildes := closes(text, "```"), closes(text, "~~~") |
| 34 | if ticks && tildes { |
| 35 | mapper.loss(LossCodeFence) |
| 36 | mapper.out = append(mapper.out, fallback(text)...) |
| 37 | return |
| 38 | } |
| 39 | fence := "```" |
| 40 | if ticks { |
| 41 | fence = "~~~" |
| 42 | } |
| 43 | mapper.emit(written{kind: markup.Code, fence: fence, info: info, |
| 44 | code: text}) |
| 45 | } |
| 46 | |
| 47 | // closes is whether any line of text would close fence. |
| 48 | func closes(text, fence string) bool { |
| 49 | for _, each := range strings.Split(text, "\n") { |
| 50 | if markup.ClosesFence(each, fence) { |
| 51 | return true |
| 52 | } |
| 53 | } |
| 54 | return false |
| 55 | } |
| 56 | |
| 57 | // language is a fence's info: lower case, spaces as hyphens, and none |
| 58 | // for plain text. |
| 59 | func language(name string) string { |
| 60 | name = strings.ToLower(strings.TrimSpace(name)) |
| 61 | if name == "plain text" || strings.ContainsAny(name, "`~\n") { |
| 62 | return "" |
| 63 | } |
| 64 | return strings.ReplaceAll(name, " ", "-") |
| 65 | } |
| 66 | |
| 67 | func plainRuns(runs []Run) string { |
| 68 | var out strings.Builder |
| 69 | for _, run := range runs { |
| 70 | out.WriteString(run.Text) |
| 71 | } |
| 72 | return out.String() |
| 73 | } |
| 74 | |
| 75 | // equationBlock is its expression in a math fence. |
| 76 | func (mapper *mapper) equationBlock(block Block, _ int) { |
| 77 | mapper.fenced(block.Expression, "math") |
| 78 | } |
| 79 | |
| 80 | // childPage is a [[link]] to the child, where Notion shows it (§9.4). |
| 81 | // The block's id is the child page's. |
| 82 | func (mapper *mapper) childPage(block Block, _ int) { |
| 83 | title, state := mapper.resolve(block.ID) |
| 84 | if state == Live { |
| 85 | mapper.emit(written{kind: markup.Paragraph, |
| 86 | lines: []line{{mapper.wikiTo(block.ID, title, "")}}}) |
| 87 | return |
| 88 | } |
| 89 | mapper.loss(LossDeadLink) |
| 90 | mapper.emit(written{kind: markup.Paragraph, |
| 91 | lines: []line{{lit(firstOf(block.Title, "Untitled"))}}}) |
| 92 | } |
| 93 | |
| 94 | // linkToPage is a [[link]] when its target was imported, else a |
| 95 | // placeholder. |
| 96 | func (mapper *mapper) linkToPage(block Block, _ int) { |
| 97 | if block.LinkTo != nil { |
| 98 | title, state := mapper.resolve(block.LinkTo.ID) |
| 99 | if state == Live { |
| 100 | mapper.emit(written{kind: markup.Paragraph, lines: []line{{ |
| 101 | mapper.wikiTo(block.LinkTo.ID, title, "")}}}) |
| 102 | return |
| 103 | } |
| 104 | } |
| 105 | id := block.ID |
| 106 | if block.LinkTo != nil { |
| 107 | id = block.LinkTo.ID |
| 108 | } |
| 109 | mapper.placeholder(id, fixed("a linked page that wasn't shared "+ |
| 110 | "with the import.")) |
| 111 | } |
| 112 | |
| 113 | // childDatabase is a placeholder naming the database, until phase 2 |
| 114 | // moves it to Fichas (§17). |
| 115 | func (mapper *mapper) childDatabase(block Block, _ int) { |
| 116 | mapper.loss(LossDatabase) |
| 117 | mapper.placeholder(block.ID, fixed("the database “"), |
| 118 | lit(firstOf(block.Title, "Untitled")), |
| 119 | fixed("” isn't here yet. It will move to Fichas.")) |
| 120 | } |
| 121 | |
| 122 | // hosted is a file Notion holds: its links are signed and expire. |
| 123 | func hosted(file *File) bool { |
| 124 | return file != nil && (file.Type == "file" || file.Type == "file_upload") |
| 125 | } |
| 126 | |
| 127 | // image is the run's line for a Notion-hosted image (§12), and a web |
| 128 | // link for an external one (D13, Q2). |
| 129 | func (mapper *mapper) image(block Block, _ int) { |
| 130 | switch { |
| 131 | case hosted(block.File): |
| 132 | mapper.emit(written{kind: markup.Paragraph, |
| 133 | lines: []line{{mapper.imageLine(block)}}}) |
| 134 | case block.File != nil && block.File.URL != "": |
| 135 | label := firstOf(plainRuns(block.Caption), block.File.URL) |
| 136 | mapper.emit(written{kind: markup.Paragraph, |
| 137 | lines: []line{mapper.webLink(label, block.File.URL)}}) |
| 138 | default: |
| 139 | mapper.loss(LossFile) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // imageLine is what the image step answers: syntax when it is an image |
| 144 | // Mapa holds, else text, held to the check like any other. |
| 145 | func (mapper *mapper) imageLine(block Block) part { |
| 146 | text := "" |
| 147 | if mapper.options.Images != nil { |
| 148 | text = mapper.options.Images.Image(block.ID) |
| 149 | } |
| 150 | if text == "" { |
| 151 | text = "(Image not imported: " + fileName(block.File) + |
| 152 | ", images aren't set up in Mapa yet)" |
| 153 | } |
| 154 | if imageReference.MatchString(text) { |
| 155 | return part{kind: image, text: text} |
| 156 | } |
| 157 | return lit(text) |
| 158 | } |
| 159 | |
| 160 | // file is a web link for an external file, and a placeholder naming a |
| 161 | // Notion-hosted one: its link would be dead within the hour. |
| 162 | func (mapper *mapper) file(block Block, _ int) { |
| 163 | switch { |
| 164 | case hosted(block.File): |
| 165 | mapper.loss(LossFile) |
| 166 | mapper.placeholder(block.ID, fixed("the file “"), |
| 167 | lit(fileName(block.File)), fixed("” wasn't imported.")) |
| 168 | case block.File != nil && block.File.URL != "": |
| 169 | label := firstOf(plainRuns(block.Caption), block.File.Name, |
| 170 | block.File.URL) |
| 171 | mapper.emit(written{kind: markup.Paragraph, |
| 172 | lines: []line{mapper.webLink(label, block.File.URL)}}) |
| 173 | default: |
| 174 | mapper.loss(LossFile) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // fileName is a file's name, or the last segment of its URL's path. |
| 179 | func fileName(file *File) string { |
| 180 | if file == nil { |
| 181 | return "file" |
| 182 | } |
| 183 | if file.Name != "" { |
| 184 | return file.Name |
| 185 | } |
| 186 | parsed, err := url.Parse(file.URL) |
| 187 | if err != nil { |
| 188 | return "file" |
| 189 | } |
| 190 | return firstOf(parsed.Path[strings.LastIndex(parsed.Path, "/")+1:], |
| 191 | "file") |
| 192 | } |
| 193 | |
| 194 | // embed is a web link, its caption as its text. |
| 195 | func (mapper *mapper) embed(block Block, _ int) { |
| 196 | if block.URL == "" { |
| 197 | mapper.loss(LossBlock + block.Type) |
| 198 | return |
| 199 | } |
| 200 | label := firstOf(plainRuns(block.Caption), block.URL) |
| 201 | mapper.emit(written{kind: markup.Paragraph, |
| 202 | lines: []line{mapper.webLink(label, block.URL)}}) |
| 203 | } |
| 204 | |
| 205 | // table is pipe-table text, one paragraph: a header row, a rule row |
| 206 | // when Notion marks a header, then a line per row (level-up D10). |
| 207 | func (mapper *mapper) table(block Block, _ int) { |
| 208 | width := 0 |
| 209 | if block.Table != nil { |
| 210 | width = block.Table.Width |
| 211 | } |
| 212 | var lines []line |
| 213 | for _, row := range block.Children { |
| 214 | width = max(width, len(row.Cells)) |
| 215 | } |
| 216 | for index, row := range block.Children { |
| 217 | lines = append(lines, mapper.tableRow(row.Cells, width)) |
| 218 | if index == 0 && block.Table != nil && block.Table.ColumnHeader { |
| 219 | lines = append(lines, line{fixed(ruleRow(width))}) |
| 220 | } |
| 221 | } |
| 222 | if len(lines) == 0 { |
| 223 | return |
| 224 | } |
| 225 | mapper.loss(LossTableLayout) |
| 226 | mapper.emit(written{kind: markup.Paragraph, lines: lines}) |
| 227 | } |
| 228 | |
| 229 | // tableRow is one row: each cell's text on one line, a | in it as /. |
| 230 | func (mapper *mapper) tableRow(cells [][]Run, width int) line { |
| 231 | out := line{fixed("|")} |
| 232 | for index := 0; index < width; index++ { |
| 233 | out = append(out, fixed(" ")) |
| 234 | if index < len(cells) { |
| 235 | cell := joinLines(mapper.inline(cells[index], marks{})) |
| 236 | out = append(out, pipes(cell)...) |
| 237 | } |
| 238 | out = append(out, fixed(" |")) |
| 239 | } |
| 240 | return out |
| 241 | } |
| 242 | |
| 243 | // pipes turns | in a cell's literal text into /. |
| 244 | func pipes(cell line) line { |
| 245 | out := append(line{}, cell...) |
| 246 | for index := range out { |
| 247 | if out[index].kind == literal { |
| 248 | out[index].text = strings.ReplaceAll(out[index].text, "|", "/") |
| 249 | } |
| 250 | } |
| 251 | return out |
| 252 | } |
| 253 | |
| 254 | func ruleRow(width int) string { |
| 255 | return "|" + strings.Repeat(" --- |", width) |
| 256 | } |
| 257 | |