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// Neutralising (§11.4, D16). Mapa has no escapes, and an import has
10// nobody watching, so literal text that Mapa would read as markup gets
11// one invisible character at the one place that breaks the reading, and
12// a backtick becomes a look-alike. The table runs over the WRITTEN line,
13// so a reading that forms where literal text meets generated syntax is
14// caught too, and each change is counted.
15
16const (
17 // zeroWidth is U+200B ZERO WIDTH SPACE. Go's TrimSpace and \s and
18 // JavaScript's trim all leave it alone, so the parser and the
19 // editor's port agree on it.
20 zeroWidth = "\u200b"
21 // graveLookAlike is U+02CB MODIFIER LETTER GRAVE ACCENT, written in
22 // place of a literal backtick: only a code run makes a code span.
23 graveLookAlike = "\u02cb"
24 // bracketLookAlike is U+FF3B FULLWIDTH LEFT SQUARE BRACKET, written
25 // in place of a literal [ that would open a link over generated
26 // syntax. A U+200B cannot stop that reading: a web link's label runs
27 // to the first ], whatever it holds (a build departure from §11.4's
28 // seam row for [, which put a U+200B there).
29 bracketLookAlike = "\uff3b"
30)
31
32// neutralise applies the table to one block.
33func (mapper *mapper) neutralise(block *written) {
34 if block.kind == markup.Code || block.kind == markup.Rule {
35 return
36 }
37 for index := range block.lines {
38 // A deadline or a Stop reaches inside a block, line by line: a
39 // block of many long lines must not outrun it (Fable round 2).
40 if mapper.stopped() != nil {
41 return
42 }
43 each := mergeLiterals(block.lines[index])
44 each = mapper.addresses(each)
45 each = mapper.backticks(each)
46 each = mapper.brackets(each)
47 each = mapper.seams(each)
48 each = mapper.labels(each)
49 each = mapper.openers(each)
50 block.lines[index] = each
51 }
52 mapper.lineStarts(block)
53 mapper.ruleItems(block)
54 mapper.taskBoxes(block)
55 mapper.markerTail(block)
56}
57
58// backticks turns every literal backtick into its look-alike.
59func (mapper *mapper) backticks(each line) line {
60 for index := range each {
61 if each[index].kind != literal {
62 continue
63 }
64 count := strings.Count(each[index].text, "`")
65 mapper.result.Neutralised += count
66 each[index].text = strings.ReplaceAll(each[index].text, "`",
67 graveLookAlike)
68 }
69 return each
70}
71
72// brackets breaks every [[ in literal text, which also breaks ![[: a
73// U+200B between each [ and a [ straight after it, in one pass.
74func (mapper *mapper) brackets(each line) line {
75 for index := range each {
76 if each[index].kind != literal ||
77 !strings.Contains(each[index].text, "[[") {
78 continue
79 }
80 text := each[index].text
81 var out strings.Builder
82 for at := 0; at < len(text); at++ {
83 out.WriteByte(text[at])
84 if text[at] == '[' && at+1 < len(text) && text[at+1] == '[' {
85 out.WriteString(zeroWidth)
86 mapper.result.Neutralised++
87 }
88 }
89 each[index].text = out.String()
90 }
91 return each
92}
93
94// seams breaks a literal ! written straight before a generated link:
95// ![[Title]] would be an embed, and ![text](url) an image once the
96// images spec is built.
97func (mapper *mapper) seams(each line) line {
98 for index := 0; index+1 < len(each); index++ {
99 next := each[index+1].kind
100 if each[index].kind != literal || (next != wiki && next != web) {
101 continue
102 }
103 text := each[index].text
104 if strings.HasSuffix(text, "!") {
105 each[index].text += zeroWidth
106 mapper.result.Neutralised++
107 }
108 }
109 return each
110}
111
112// labels breaks a literal [label](address) that Mapa would read as a
113// web link, or as an image once the images spec is built: U+200B
114// between ] and (. One scan finds every one: breaking one changes no
115// other's reading.
116func (mapper *mapper) labels(each line) line {
117 text, owners := owned(each)
118 var changes []change
119 for _, at := range literalLinks(text, owners) {
120 changes = append(changes, change{at: at + 1, text: zeroWidth})
121 }
122 mapper.result.Neutralised += len(changes)
123 return apply(each, changes)
124}
125
126// openers turns a literal [ that would open a link, a wiki link or an
127// embed into its look-alike: "[" before a generated [site](url) is a
128// link whose label is "[site"; with a later ]] it is a wiki link. Each
129// pass is linear; a [ replaced changes no later [ reading, so one pass
130// normally finds them all, and the loop is a guard.
131func (mapper *mapper) openers(each line) line {
132 for {
133 text, owners := owned(each)
134 opens := markup.OpensAt(text)
135 var changes []change
136 for at := 0; at < len(text); at++ {
137 if text[at] == '[' && owners[at] == literal && opens(at) {
138 changes = append(changes, change{at: at,
139 text: bracketLookAlike, replace: true})
140 }
141 }
142 if len(changes) == 0 {
143 return each
144 }
145 mapper.result.Neutralised += len(changes)
146 each = apply(each, changes)
147 }
148}
149
150// literalLinks is the index of the ] of every literal "](" that would
151// be read as a link's, in one forward scan: the last [, ] or line end
152// before each, and the end of each address, come from running indexes.
153func literalLinks(text string, owners []partKind) []int {
154 ends := addressEnds(text)
155 var out []int
156 open := -1
157 for at := 0; at+1 < len(text); at++ {
158 char := text[at]
159 if char == ']' && text[at+1] == '(' && owners[at] == literal &&
160 owners[at+1] == literal && open >= 0 && text[open] == '[' &&
161 open+1 != at && linkAddress(text, at, ends) {
162 out = append(out, at)
163 }
164 if char == '[' || char == ']' || char == '\n' {
165 open = at
166 }
167 }
168 return out
169}
170
171// addressEnds is, for each index, the first index at or after it that
172// ends an address: ), a space, a tab or a line end; len(text) if none.
173func addressEnds(text string) []int {
174 ends := make([]int, len(text)+1)
175 ends[len(text)] = len(text)
176 for at := len(text) - 1; at >= 0; at-- {
177 ends[at] = ends[at+1]
178 if strings.IndexByte(")\t\n\f\r ", text[at]) >= 0 {
179 ends[at] = at
180 }
181 }
182 return ends
183}
184
185// linkAddress is whether the "(" after the ] at at opens an address
186// Mapa would link: closed by ), and safe or an image's.
187func linkAddress(text string, at int, ends []int) bool {
188 start := at + 2
189 end := ends[min(start, len(text))]
190 if end <= start || end >= len(text) || text[end] != ')' {
191 return false
192 }
193 address := text[start:end]
194 return markup.LinkAddress(address) || strings.HasPrefix(address, "/i/")
195}
196
197// owned is a line's text and the kind of part each byte belongs to.
198func owned(each line) (string, []partKind) {
199 var text strings.Builder
200 var owners []partKind
201 for _, piece := range each {
202 source := piece.source()
203 text.WriteString(source)
204 for range len(source) {
205 owners = append(owners, piece.kind)
206 }
207 }
208 return text.String(), owners
209}
210
211// lineStarts breaks a block start at the beginning of a line that is
212// not one: every line of a paragraph, every line of an item after its
213// first, and a heading's text that starts with #, which the heading's
214// marker would take.
215func (mapper *mapper) lineStarts(block *written) {
216 for index, each := range block.lines {
217 text := each.text()
218 switch {
219 case block.kind == markup.Heading:
220 if strings.HasPrefix(text, "#") {
221 block.lines[index] = mapper.inserted(each, 0)
222 }
223 case block.kind == markup.Paragraph ||
224 (block.kind == markup.Item && index > 0):
225 // A line that looks like a column fence is broken the same
226 // way (columns and icons spec §6.6, §11.4's new row).
227 if markup.StartsBlock(text) || markup.FenceLike(text) {
228 first := len(text) - len(strings.TrimLeft(text, " \t"))
229 block.lines[index] = mapper.inserted(each, first)
230 }
231 }
232 }
233}
234
235// ruleItems breaks a list item whose written line would read as a rule:
236// "- ---" and "- - -" are rules to the parser, never items (Fable L1).
237func (mapper *mapper) ruleItems(block *written) {
238 if block.kind != markup.Item || len(block.lines) == 0 {
239 return
240 }
241 first := block.source()
242 if newline := strings.IndexByte(first, '\n'); newline >= 0 {
243 first = first[:newline]
244 }
245 if parsed := markup.Parse(first); len(parsed) == 1 &&
246 parsed[0].Kind == markup.Rule {
247 block.lines[0] = mapper.inserted(block.lines[0], 0)
248 }
249}
250
251// taskBoxes breaks a list item's text that would read as a task box.
252func (mapper *mapper) taskBoxes(block *written) {
253 if block.kind != markup.Item || len(block.lines) == 0 {
254 return
255 }
256 if state, _ := markup.TaskBox(block.lines[0].text()); state != "" {
257 block.lines[0] = mapper.inserted(block.lines[0], 0)
258 }
259}
260
261// markerTail breaks a trailing " ^word", which would be read as the
262// block's marker.
263func (mapper *mapper) markerTail(block *written) {
264 last := len(block.lines) - 1
265 if last < 0 {
266 return
267 }
268 source := block.source()
269 tail := source[strings.LastIndex(source, "\n")+1:]
270 at := markup.MarkerAt(tail)
271 if at < 0 {
272 return
273 }
274 text := block.lines[last].text()
275 offset := at - (len(tail) - len(text))
276 block.lines[last] = mapper.inserted(block.lines[last], max(offset, 0))
277}
278
279func (mapper *mapper) inserted(each line, offset int) line {
280 mapper.result.Neutralised++
281 return insert(each, offset, zeroWidth)
282}
283