| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 7 | ) |
| 8 | |
| 9 | // Bare addresses against generated syntax (Fable L1, L2). Mapa links a |
| 10 | // bare http(s) address by itself, and the link runs over every |
| 11 | // character that is not a space or a bracket: a backtick, a *, a |
| 12 | // U+200B. So a literal address written straight before a code span, an |
| 13 | // emphasis delimiter or a seam's U+200B would swallow it, and the block |
| 14 | // fell back to a plain copy, or the address gained the U+200B. Such an |
| 15 | // address is written as the explicit link [address](address), which |
| 16 | // ends where Notion's did and reads the same. |
| 17 | |
| 18 | // addresses rewrites, in each literal part followed by a generated |
| 19 | // part, a bare address in its last word as an explicit link. |
| 20 | func (mapper *mapper) addresses(each line) line { |
| 21 | var out line |
| 22 | for index, piece := range each { |
| 23 | next := index + 1 |
| 24 | if piece.kind != literal || next >= len(each) || |
| 25 | each[next].kind == literal { |
| 26 | out = append(out, piece) |
| 27 | continue |
| 28 | } |
| 29 | out = append(out, mapper.lastAddress(piece)...) |
| 30 | } |
| 31 | return out |
| 32 | } |
| 33 | |
| 34 | // lastAddress splits a literal around a bare address in its last word, |
| 35 | // if it has one there. |
| 36 | func (mapper *mapper) lastAddress(piece part) []part { |
| 37 | text := piece.text |
| 38 | word := strings.LastIndexAny(text, " \t\n") + 1 |
| 39 | spans := markup.Spans(text[word:]) |
| 40 | offset := word |
| 41 | for _, span := range spans { |
| 42 | rest := text[offset+len(span.Source):] |
| 43 | if !rewritable(span, rest) { |
| 44 | offset += len(span.Source) |
| 45 | continue |
| 46 | } |
| 47 | mapper.result.Neutralised++ |
| 48 | out := []part{} |
| 49 | if offset > 0 { |
| 50 | out = append(out, lit(text[:offset])) |
| 51 | } |
| 52 | out = append(out, part{kind: web, text: span.URL, url: span.URL}) |
| 53 | if rest != "" { |
| 54 | out = append(out, lit(rest)) |
| 55 | } |
| 56 | return out |
| 57 | } |
| 58 | return []part{piece} |
| 59 | } |
| 60 | |
| 61 | // rewritable is a bare address worth writing as [address](address): |
| 62 | // one the autolink would run on from into the generated part after it |
| 63 | // (nothing in rest stops it: no space, no bracket), that Mapa would link |
| 64 | // as written, and that holds no backtick, which backticks() would turn |
| 65 | // into a look-alike inside the link. Anything else is left as it was: |
| 66 | // rewriting an address the autolink stops short of anyway, or one Mapa |
| 67 | // would not link, made the block fall back (Fable round 2, Low 1). |
| 68 | func rewritable(span markup.Span, rest string) bool { |
| 69 | return span.Kind == markup.WebLink && |
| 70 | !strings.HasPrefix(span.Source, "[") && |
| 71 | !strings.ContainsAny(rest, " \t\n<>()[]") && |
| 72 | !strings.Contains(span.URL, "`") && markup.LinkAddress(span.URL) |
| 73 | } |
| 74 | |