| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // The fixture kit: compact builders for the tree, and the resolver and |
| 9 | // image step a fixture names. Fixtures are written from Notion's |
| 10 | // documentation and the import spec's tables; each is committed as |
| 11 | // notionmd.Page JSON beside its golden body (golden_test.go). |
| 12 | |
| 13 | // fixture is one case: a page, and what the run would answer for it. |
| 14 | type fixture struct { |
| 15 | Name string `json:"name"` |
| 16 | Page Page `json:"page"` |
| 17 | Parent string `json:"parent,omitempty"` |
| 18 | Date string `json:"date,omitempty"` |
| 19 | // Pages is what the resolver answers, by Notion id. |
| 20 | Pages map[string]answer `json:"pages,omitempty"` |
| 21 | // Images is the image step's line, by block id. |
| 22 | Images map[string]string `json:"images,omitempty"` |
| 23 | } |
| 24 | |
| 25 | // answer is one resolver answer: a title and a state's name. |
| 26 | type answer struct { |
| 27 | Title string `json:"title,omitempty"` |
| 28 | State string `json:"state"` |
| 29 | } |
| 30 | |
| 31 | var stateNames = map[string]LinkState{"live": Live, "dead": Dead, |
| 32 | "row": Row, "database": Database, "unshared": Unshared} |
| 33 | |
| 34 | // fixtureResolver answers from a fixture's Pages. |
| 35 | type fixtureResolver map[string]answer |
| 36 | |
| 37 | func (pages fixtureResolver) Page(id string) (string, LinkState) { |
| 38 | found, ok := pages[id] |
| 39 | if !ok { |
| 40 | return "", Unshared |
| 41 | } |
| 42 | return found.Title, stateNames[found.State] |
| 43 | } |
| 44 | |
| 45 | // fixtureImages answers from a fixture's Images. |
| 46 | type fixtureImages map[string]string |
| 47 | |
| 48 | func (lines fixtureImages) Image(id string) string { return lines[id] } |
| 49 | |
| 50 | // teamZone is the team's zone every fixture renders in. |
| 51 | var teamZone = mustZone("Europe/Dublin") |
| 52 | |
| 53 | func mustZone(name string) *time.Location { |
| 54 | zone, err := time.LoadLocation(name) |
| 55 | if err != nil { |
| 56 | panic(err) |
| 57 | } |
| 58 | return zone |
| 59 | } |
| 60 | |
| 61 | // options are a fixture's render options. |
| 62 | func (each fixture) options() Options { |
| 63 | return Options{Resolve: fixtureResolver(each.Pages), |
| 64 | Images: fixtureImages(each.Images), Zone: teamZone, |
| 65 | Parent: each.Parent, Date: each.Date} |
| 66 | } |
| 67 | |
| 68 | // page is a fixture of blocks alone. |
| 69 | func page(name string, blocks ...Block) fixture { |
| 70 | return fixture{Name: name, Page: Page{ID: "page-" + name, |
| 71 | Blocks: blocks}} |
| 72 | } |
| 73 | |
| 74 | // live adds resolver answers: id, title pairs, all live. |
| 75 | func (each fixture) live(pairs ...string) fixture { |
| 76 | if each.Pages == nil { |
| 77 | each.Pages = map[string]answer{} |
| 78 | } |
| 79 | for index := 0; index+1 < len(pairs); index += 2 { |
| 80 | each.Pages[pairs[index]] = answer{Title: pairs[index+1], |
| 81 | State: "live"} |
| 82 | } |
| 83 | return each |
| 84 | } |
| 85 | |
| 86 | // state adds one resolver answer that is not live. |
| 87 | func (each fixture) state(id, name string) fixture { |
| 88 | if each.Pages == nil { |
| 89 | each.Pages = map[string]answer{} |
| 90 | } |
| 91 | each.Pages[id] = answer{State: name} |
| 92 | return each |
| 93 | } |
| 94 | |
| 95 | func txt(text string) Run { return Run{Type: "text", Text: text} } |
| 96 | |
| 97 | func bold(text string) Run { |
| 98 | return Run{Type: "text", Text: text, Bold: true} |
| 99 | } |
| 100 | |
| 101 | func italic(text string) Run { |
| 102 | return Run{Type: "text", Text: text, Italic: true} |
| 103 | } |
| 104 | |
| 105 | func both(text string) Run { |
| 106 | return Run{Type: "text", Text: text, Bold: true, Italic: true} |
| 107 | } |
| 108 | |
| 109 | func codeRun(text string) Run { |
| 110 | return Run{Type: "text", Text: text, Code: true} |
| 111 | } |
| 112 | |
| 113 | func linked(text, address string) Run { |
| 114 | return Run{Type: "text", Text: text, Link: address, Href: address} |
| 115 | } |
| 116 | |
| 117 | func mention(kind, id, shown string) Run { |
| 118 | return Run{Type: "mention", Text: shown, |
| 119 | Mention: &Mention{Type: kind, ID: id}} |
| 120 | } |
| 121 | |
| 122 | func dated(start, end, zone string) Run { |
| 123 | return Run{Type: "mention", Text: start, Mention: &Mention{ |
| 124 | Type: "date", Start: start, End: end, TimeZone: zone}} |
| 125 | } |
| 126 | |
| 127 | // blk is a known block of kind with text. |
| 128 | func blk(kind string, runs ...Run) Block { |
| 129 | return Block{Type: kind, Known: true, Text: runs} |
| 130 | } |
| 131 | |
| 132 | func para(runs ...Run) Block { return blk("paragraph", runs...) } |
| 133 | |
| 134 | // with gives a block children. |
| 135 | func with(block Block, children ...Block) Block { |
| 136 | block.Children = children |
| 137 | return block |
| 138 | } |
| 139 | |
| 140 | // named gives a block an id. |
| 141 | func named(block Block, id string) Block { |
| 142 | block.ID = id |
| 143 | return block |
| 144 | } |
| 145 | |
| 146 | func todo(checked bool, runs ...Run) Block { |
| 147 | block := blk("to_do", runs...) |
| 148 | block.Checked = checked |
| 149 | return block |
| 150 | } |
| 151 | |
| 152 | func codeOf(language, text string) Block { |
| 153 | block := blk("code", txt(text)) |
| 154 | block.Language = language |
| 155 | return block |
| 156 | } |
| 157 | |
| 158 | func fileOf(kind, source, address, name string) Block { |
| 159 | return Block{Type: kind, Known: true, |
| 160 | File: &File{Type: source, URL: address, Name: name}} |
| 161 | } |
| 162 | |
| 163 | // hexID is a Notion id, dashed, from a short seed. |
| 164 | func hexID(seed string) string { |
| 165 | compact := strings.Repeat("0", 32-len(seed)) + seed |
| 166 | return compact[:8] + "-" + compact[8:12] + "-" + compact[12:16] + "-" + |
| 167 | compact[16:20] + "-" + compact[20:] |
| 168 | } |
| 169 | |