| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | ) |
| 9 | |
| 10 | // mapper is one page's render. |
| 11 | type mapper struct { |
| 12 | options Options |
| 13 | result Result |
| 14 | out []written |
| 15 | // inColumn is set while a column layout's columns are written: a |
| 16 | // layout inside one is written flat (columns and icons spec §6.2). |
| 17 | inColumn bool |
| 18 | } |
| 19 | |
| 20 | // render maps a page: the parent line, the date line, then its blocks, |
| 21 | // each neutralised, checked and, if it fails the check, written as a |
| 22 | // fenced copy of its text (§11.4). |
| 23 | func render(page Page, options Options) (Result, error) { |
| 24 | page = normalisePage(page) |
| 25 | mapper := &mapper{options: options, |
| 26 | result: Result{Losses: map[string]int{}}} |
| 27 | if options.Parent != "" { |
| 28 | mapper.emit(written{kind: markup.Paragraph, lines: []line{{ |
| 29 | fixed("In: "), {kind: wiki, target: options.Parent}}}}) |
| 30 | } |
| 31 | if options.Date != "" { |
| 32 | mapper.emit(written{kind: markup.Paragraph, |
| 33 | lines: []line{{dateLink(options.Date)}}}) |
| 34 | } |
| 35 | mapper.blocks(page.Blocks) |
| 36 | return mapper.finish() |
| 37 | } |
| 38 | |
| 39 | // loss counts one thing the subset could not say. |
| 40 | func (mapper *mapper) loss(kind string) { mapper.result.Losses[kind]++ } |
| 41 | |
| 42 | // emit adds a block, unless it is empty: an empty block has no Markdown |
| 43 | // form and is written as nothing (level-up §6.4 step 4). |
| 44 | func (mapper *mapper) emit(block written) { |
| 45 | if block.kind != markup.Code && block.kind != markup.Rule && |
| 46 | strings.TrimSpace(block.inline()) == "" { |
| 47 | return |
| 48 | } |
| 49 | mapper.out = append(mapper.out, block) |
| 50 | } |
| 51 | |
| 52 | // placeholder is a quote holding a note about what is not here, and |
| 53 | // records the Notion id behind it (§10.2). |
| 54 | func (mapper *mapper) placeholder(id string, parts ...part) { |
| 55 | if id != "" { |
| 56 | mapper.result.Placeholders = append(mapper.result.Placeholders, id) |
| 57 | } |
| 58 | words := append(line{fixed("From Notion: ")}, parts...) |
| 59 | mapper.emit(written{kind: markup.Quote, lines: []line{words}}) |
| 60 | } |
| 61 | |
| 62 | // finish neutralises and checks every block, and joins them. |
| 63 | func (mapper *mapper) finish() (Result, error) { |
| 64 | var final []written |
| 65 | for _, block := range mapper.out { |
| 66 | if err := mapper.stopped(); err != nil { |
| 67 | return Result{}, err |
| 68 | } |
| 69 | if markup.IsFence(block.kind) { |
| 70 | // A fence line is the mapper's own, never Notion's text: the |
| 71 | // whole body's parse in join holds the layout to it. |
| 72 | final = append(final, block) |
| 73 | continue |
| 74 | } |
| 75 | plain := plainText(block) |
| 76 | mapper.neutralise(&block) |
| 77 | if err := mapper.stopped(); err != nil { |
| 78 | return Result{}, err |
| 79 | } |
| 80 | if checkBlock(block) { |
| 81 | final = append(final, block) |
| 82 | continue |
| 83 | } |
| 84 | copied := fallback(plain) |
| 85 | for _, each := range copied { |
| 86 | if !checkBlock(each) { |
| 87 | return Result{}, ErrRenderFailed |
| 88 | } |
| 89 | } |
| 90 | mapper.result.Fallbacks++ |
| 91 | final = append(final, copied...) |
| 92 | } |
| 93 | body, ok := join(final) |
| 94 | if !ok { |
| 95 | return Result{}, ErrRenderFailed |
| 96 | } |
| 97 | mapper.result.Body = body |
| 98 | if len(mapper.result.Losses) == 0 { |
| 99 | mapper.result.Losses = nil |
| 100 | } |
| 101 | return mapper.result, nil |
| 102 | } |
| 103 | |
| 104 | // join puts the blocks together by markup.Join's seams, and holds the |
| 105 | // whole body to the same blocks: no block may run into the next. |
| 106 | func join(blocks []written) (string, bool) { |
| 107 | parsed := make([]markup.Block, len(blocks)) |
| 108 | for index, block := range blocks { |
| 109 | parsed[index] = markup.Block{Kind: block.kind, |
| 110 | Source: block.source()} |
| 111 | } |
| 112 | body := markup.Join(parsed) |
| 113 | again := markup.Parse(body) |
| 114 | if len(again) != len(parsed) { |
| 115 | return "", false |
| 116 | } |
| 117 | for index := range again { |
| 118 | if again[index].Source != parsed[index].Source || |
| 119 | again[index].Kind != parsed[index].Kind && |
| 120 | (markup.IsFence(again[index].Kind) || |
| 121 | markup.IsFence(parsed[index].Kind)) { |
| 122 | return "", false |
| 123 | } |
| 124 | } |
| 125 | return body, true |
| 126 | } |
| 127 | |
| 128 | // plainText is a block's words, without the syntax the mapper made: what |
| 129 | // the fallback holds. |
| 130 | func plainText(block written) string { |
| 131 | if block.kind == markup.Code { |
| 132 | return block.code |
| 133 | } |
| 134 | texts := make([]string, len(block.lines)) |
| 135 | for index, each := range block.lines { |
| 136 | var out strings.Builder |
| 137 | for _, piece := range each { |
| 138 | switch piece.kind { |
| 139 | case wiki: |
| 140 | out.WriteString(firstOf(piece.text, piece.target)) |
| 141 | case delim: |
| 142 | // Emphasis delimiters are syntax, not words. |
| 143 | if strings.Trim(piece.text, "*") != "" { |
| 144 | out.WriteString(piece.text) |
| 145 | } |
| 146 | default: |
| 147 | out.WriteString(piece.text) |
| 148 | } |
| 149 | } |
| 150 | texts[index] = out.String() |
| 151 | } |
| 152 | return strings.Join(texts, "\n") |
| 153 | } |
| 154 | |
| 155 | // stopped is the render's context ending: a Stop, or the run's |
| 156 | // deadline for one page. A page that takes too long fails alone; it |
| 157 | // never wedges the run (Fable M2). |
| 158 | func (mapper *mapper) stopped() error { |
| 159 | if mapper.options.Context == nil { |
| 160 | return nil |
| 161 | } |
| 162 | if err := mapper.options.Context.Err(); err != nil { |
| 163 | return errors.Join(ErrRenderFailed, err) |
| 164 | } |
| 165 | return nil |
| 166 | } |
| 167 | |