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 (
4 "strings"
5
6 "amadan.net/oficina/mapa/internal/mapa/markup"
7)
8
9// Rich text (§11.1): runs become lines of parts. Marks are written as
10// the editor writes them (editor/serialize.mjs, emphasis): each stretch
11// of plain text takes its emphasis on its own, em outside strong, with
12// whitespace at a mark's edge outside its delimiters, and a mark never
13// crosses a link, a mention or code.
14
15// marks are the marks a caller forces onto every run: a toggle's
16// summary is bold, a caption italic.
17type marks struct{ bold, italic bool }
18
19// segment is one piece of a line before emphasis is written: marked
20// text, or a part that takes no marks.
21type segment struct {
22 piece part
23 isText bool
24 text string
25 bold, italic bool
26 newline bool
27}
28
29// inline maps runs to lines. Blank lines inside the text are dropped
30// and counted: a blank line ends a Mapa block.
31func (mapper *mapper) inline(runs []Run, forced marks) []line {
32 var segments []segment
33 for _, run := range runs {
34 segments = append(segments, mapper.run(run, forced)...)
35 }
36 var lines []line
37 var current []segment
38 for _, each := range append(segments, segment{newline: true}) {
39 if !each.newline {
40 current = append(current, each)
41 continue
42 }
43 lines = append(lines, emphasise(current))
44 current = nil
45 }
46 return mapper.dropBlank(lines)
47}
48
49// dropBlank trims each line's trailing whitespace, which nothing shows
50// and the store would trim from a body's end, and drops empty lines.
51func (mapper *mapper) dropBlank(lines []line) []line {
52 var kept []line
53 interior := 0
54 for _, each := range lines {
55 each = trimTrailing(each)
56 if strings.TrimSpace(each.text()) == "" {
57 interior++
58 continue
59 }
60 if len(kept) > 0 && interior > 0 {
61 mapper.loss(LossBlankLines)
62 }
63 interior = 0
64 kept = append(kept, each)
65 }
66 return kept
67}
68
69// run maps one run to segments.
70func (mapper *mapper) run(run Run, forced marks) []segment {
71 switch {
72 case run.Type == "mention" && run.Mention != nil:
73 return pieces(mapper.mention(run))
74 case run.Type == "equation":
75 return pieces(mapper.equation(run))
76 }
77 mapper.lostMarks(run)
78 switch {
79 case run.Link != "":
80 return pieces(mapper.link(run.Text, run.Link))
81 case run.Code && strings.Contains(run.Text, "`"):
82 mapper.loss(LossCodeTick)
83 return textSegments(run.Text, marks{})
84 case run.Code:
85 return codeSegments(run.Text)
86 }
87 chosen := marks{bold: run.Bold || forced.bold,
88 italic: run.Italic || forced.italic}
89 if (chosen.bold || chosen.italic) && strings.Contains(run.Text, "*") {
90 // Mapa's emphasis patterns exclude * inside (§11.1).
91 mapper.loss(LossMarkStar)
92 chosen = marks{}
93 }
94 if (chosen.bold || chosen.italic) && linkRunsOn(run.Text) {
95 mapper.loss(LossMarkLink)
96 chosen = marks{}
97 }
98 return textSegments(run.Text, chosen)
99}
100
101// linkRunsOn is whether a bare address in text would run on into a
102// closing delimiter: Mapa's autolink takes a * as part of an address.
103func linkRunsOn(text string) bool {
104 trimmed := strings.TrimRight(text, " \t\n\f\r")
105 offset := 0
106 for _, span := range markup.Spans(trimmed + "*") {
107 offset += len(span.Source)
108 if span.Kind == markup.WebLink && offset > len(trimmed) {
109 return true
110 }
111 }
112 return false
113}
114
115// lostMarks counts the marks the subset cannot say.
116func (mapper *mapper) lostMarks(run Run) {
117 if strings.TrimSpace(run.Text) == "" {
118 return
119 }
120 if run.Strike {
121 mapper.loss(LossStrike)
122 }
123 if run.Underline {
124 mapper.loss(LossUnderline)
125 }
126 if run.Color != "" && run.Color != "default" {
127 mapper.loss(LossColor)
128 }
129}
130
131func pieces(parts []part) []segment {
132 out := make([]segment, len(parts))
133 for index, piece := range parts {
134 out[index] = segment{piece: piece}
135 }
136 return out
137}
138
139// textSegments splits text at its line breaks.
140func textSegments(text string, chosen marks) []segment {
141 var out []segment
142 for index, each := range strings.Split(text, "\n") {
143 if index > 0 {
144 out = append(out, segment{newline: true})
145 }
146 if each != "" {
147 out = append(out, segment{isText: true, text: each,
148 bold: chosen.bold, italic: chosen.italic})
149 }
150 }
151 return out
152}
153
154// codeSegments is inline code, one span per line: a code span never
155// holds a line break.
156func codeSegments(text string) []segment {
157 var out []segment
158 for index, each := range strings.Split(text, "\n") {
159 if index > 0 {
160 out = append(out, segment{newline: true})
161 }
162 switch {
163 case strings.TrimSpace(each) == "":
164 out = append(out, segment{piece: lit(each)})
165 default:
166 out = append(out, segment{piece: part{kind: code, text: each}})
167 }
168 }
169 return out
170}
171
172// equation is an inline equation: its expression as inline code, or as
173// text when it holds a backtick or a line break.
174func (mapper *mapper) equation(run Run) []part {
175 expression := firstOf(run.Expression, run.Text)
176 if expression == "" || strings.ContainsAny(expression, "`\n") {
177 return []part{lit(expression)}
178 }
179 return []part{{kind: code, text: expression}}
180}
181
182// emphasise writes one line's segments: each stretch of text takes its
183// emphasis, and every other part is written as it is.
184func emphasise(segments []segment) line {
185 var out line
186 for index := 0; index < len(segments); {
187 if !segments[index].isText {
188 out = append(out, segments[index].piece)
189 index++
190 continue
191 }
192 end := index
193 for end < len(segments) && segments[end].isText {
194 end++
195 }
196 out = append(out, groups(segments[index:end], true)...)
197 index = end
198 }
199 return out
200}
201
202// groups wraps each stretch of run that shares a mark: em outside,
203// strong inside it.
204func groups(run []segment, emphasis bool) []part {
205 var out []part
206 for index := 0; index < len(run); {
207 end := index
208 for end < len(run) && same(run[end], run[index], emphasis) {
209 end++
210 }
211 var inner []part
212 if emphasis {
213 inner = groups(run[index:end], false)
214 } else {
215 inner = texts(run[index:end])
216 }
217 if has(run[index], emphasis) {
218 inner = wrap(inner, delimiter(emphasis))
219 }
220 out = append(out, inner...)
221 index = end
222 }
223 return out
224}
225
226func same(left, right segment, emphasis bool) bool {
227 return has(left, emphasis) == has(right, emphasis)
228}
229
230func has(each segment, emphasis bool) bool {
231 if emphasis {
232 return each.italic
233 }
234 return each.bold
235}
236
237func delimiter(emphasis bool) string {
238 if emphasis {
239 return "*"
240 }
241 return "**"
242}
243
244func texts(run []segment) []part {
245 var joined strings.Builder
246 for _, each := range run {
247 joined.WriteString(each.text)
248 }
249 return []part{lit(joined.String())}
250}
251
252func firstOf(values ...string) string {
253 for _, value := range values {
254 if value != "" {
255 return value
256 }
257 }
258 return ""
259}
260