oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package notionmd
2
3import "strings"
4
5// Line endings (Fable M1). Mapa stores "\n" alone, and markup.Parse
6// turns "\r\n" and "\r" into it, so a body written with a "\r" in it
7// never parses back to the blocks the mapper meant, and the whole page
8// failed render_failed. Notion's text may hold either; every text the
9// mapper reads is normalised once, before anything is written.
10
11var lineEndings = strings.NewReplacer("\r\n", "\n", "\r", "\n")
12
13// oneLine is a name that is written inside one line (a database's or a
14// file's name in a placeholder, a person's, a callout's emoji): any line
15// break in it is a space. A break there split the placeholder's quote,
16// and the page failed (Fable round 2, Low 2).
17var oneLine = strings.NewReplacer("\r\n", " ", "\r", " ", "\n", " ")
18
19func normalisePage(page Page) Page {
20 page.Title = normaliseRuns(page.Title)
21 page.Blocks = normaliseBlocks(page.Blocks)
22 return page
23}
24
25func normaliseBlocks(blocks []Block) []Block {
26 if len(blocks) == 0 {
27 return blocks
28 }
29 out := make([]Block, len(blocks))
30 for index, block := range blocks {
31 block.Text = normaliseRuns(block.Text)
32 block.Caption = normaliseRuns(block.Caption)
33 block.Title = oneLine.Replace(block.Title)
34 block.Emoji = strings.TrimSpace(oneLine.Replace(block.Emoji))
35 if block.File != nil {
36 file := *block.File
37 file.Name = oneLine.Replace(file.Name)
38 block.File = &file
39 }
40 block.Expression = lineEndings.Replace(block.Expression)
41 cells := make([][]Run, len(block.Cells))
42 for cell, runs := range block.Cells {
43 cells[cell] = normaliseRuns(runs)
44 }
45 if block.Cells != nil {
46 block.Cells = cells
47 }
48 block.Children = normaliseBlocks(block.Children)
49 out[index] = block
50 }
51 return out
52}
53
54func normaliseRuns(runs []Run) []Run {
55 if len(runs) == 0 {
56 return runs
57 }
58 out := make([]Run, len(runs))
59 for index, run := range runs {
60 run.Text = lineEndings.Replace(run.Text)
61 if run.Mention != nil {
62 mention := *run.Mention
63 mention.Name = oneLine.Replace(mention.Name)
64 run.Mention = &mention
65 }
66 run.Expression = lineEndings.Replace(run.Expression)
67 out[index] = run
68 }
69 return out
70}
71