oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package markup
2
3import (
4 "html"
5 "regexp"
6 "strconv"
7 "strings"
8)
9
10// The inline renderer draws a block's text exactly as the editor's schema
11// draws it (spec §5, §6.3): runs of text with marks, hard breaks and
12// atoms, serialised by ProseMirror's DOMSerializer rule. Marks nest in
13// the schema's order (link, em, strong, code), so em is always outside
14// strong, and adjacent runs that share a mark share its element. Gate L4
15// holds this file to editor/schema.mjs's toDOM, byte for byte.
16
17// mark is one inline mark: the key that decides whether two runs share
18// it, and its tags.
19type mark struct{ key, open, close string }
20
21var (
22 emMark = mark{key: "em", open: "<em>", close: "</em>"}
23 strongMark = mark{key: "strong", open: "<strong>", close: "</strong>"}
24 codeMark = mark{key: "code", open: "<code>", close: "</code>"}
25)
26
27// piece is one inline node: text (escaped) with its marks, a hard break
28// with the marks around it, or an atom, which takes no marks.
29type piece struct {
30 html string
31 marks []mark
32 text bool
33}
34
35// trailingBreak is what ProseMirror's view puts at the end of a textblock
36// that is empty or ends in something other than text, so the line has
37// height and a caret can sit there. The server draws it too, so mounting
38// the editor adds nothing (spec §6.2, §6.9).
39const trailingBreak = `<br class="ProseMirror-trailingBreak">`
40
41// EmptyParagraph is the editor's empty paragraph: the one a body ends
42// with when its last block is not a paragraph, so there is somewhere to
43// type (spec §6.2).
44const EmptyParagraph = "<p>" + trailingBreak + "</p>"
45
46var (
47 tripleStar = regexp.MustCompile(`\*\*\*(\S(?:[^*]*?\S)?)\*\*\*`)
48 markTag = regexp.MustCompile(`</?(?:em|strong)>`)
49 lineBreak = regexp.MustCompile(`\n[ \t]*`)
50)
51
52// inline draws a block's inline text.
53func inline(text string, resolver Resolver) string {
54 var pieces []piece
55 spans := Spans(text)
56 alone := len(spans) == 1
57 for _, span := range spans {
58 pieces = append(pieces, spanPieces(span, resolver, alone)...)
59 }
60 return serialise(pieces)
61}
62
63func spanPieces(span Span, resolver Resolver, alone bool) []piece {
64 switch span.Kind {
65 case Image:
66 return []piece{{html: imageHTML(span, resolver, alone)}}
67 case InCode:
68 return []piece{{html: html.EscapeString(span.Text),
69 marks: []mark{codeMark}, text: true}}
70 case WebLink:
71 return []piece{{html: html.EscapeString(span.Text),
72 marks: []mark{linkMark(span)}, text: true}}
73 case WikiLink:
74 return []piece{{html: wikiChip(span, resolver)}}
75 case Embed:
76 return []piece{{html: embedChip(span)}}
77 }
78 return words(span.Text)
79}
80
81// linkMark is a web link's mark. A bare address and a [text](url) to the
82// same address are different marks, as they are in the schema.
83func linkMark(span Span) mark {
84 bare := !strings.HasPrefix(span.Source, "[")
85 return mark{key: "link " + span.URL + " " + strconv.FormatBool(bare),
86 open: `<a href="` + html.EscapeString(span.URL) +
87 `" rel="noopener noreferrer">`,
88 close: "</a>"}
89}
90
91// words is a text run's emphasis and line breaks. The passes run over
92// escaped text, triple stars first, so ***x*** is em around strong
93// (spec §5). Their tags are then read as marks switching on and off, and
94// a line break drops the next line's indent, which the source keeps.
95func words(text string) []piece {
96 tagged := html.EscapeString(text)
97 tagged = tripleStar.ReplaceAllString(tagged,
98 "<em><strong>$1</strong></em>")
99 tagged = strong.ReplaceAllString(tagged, "<strong>$1</strong>")
100 tagged = emphasis.ReplaceAllString(tagged, "<em>$1</em>")
101 var pieces []piece
102 ems, strongs, cursor := 0, 0, 0
103 for _, at := range markTag.FindAllStringIndex(tagged, -1) {
104 pieces = lines(pieces, tagged[cursor:at[0]], marksOf(ems, strongs))
105 step := 1
106 if tagged[at[0]+1] == '/' {
107 step = -1
108 }
109 if strings.HasSuffix(tagged[at[0]:at[1]], "em>") {
110 ems += step
111 } else {
112 strongs += step
113 }
114 cursor = at[1]
115 }
116 return lines(pieces, tagged[cursor:], marksOf(ems, strongs))
117}
118
119func marksOf(ems, strongs int) []mark {
120 var marks []mark
121 if ems > 0 {
122 marks = append(marks, emMark)
123 }
124 if strongs > 0 {
125 marks = append(marks, strongMark)
126 }
127 return marks
128}
129
130// lines appends escaped text, its newlines as hard breaks.
131func lines(pieces []piece, text string, marks []mark) []piece {
132 for index, line := range lineBreak.Split(text, -1) {
133 if index > 0 {
134 pieces = append(pieces, piece{html: "<br>", marks: marks})
135 }
136 if line != "" {
137 pieces = append(pieces, piece{html: line, marks: marks,
138 text: true})
139 }
140 }
141 return pieces
142}
143
144// serialise is DOMSerializer.serializeFragment: the marks a piece shares
145// with the ones open stay open, the rest close, its own open in order.
146// Then the view's trailing break, when the textblock needs one.
147func serialise(pieces []piece) string {
148 var out strings.Builder
149 var active []mark
150 for _, each := range pieces {
151 keep := 0
152 for keep < len(active) && keep < len(each.marks) &&
153 active[keep].key == each.marks[keep].key {
154 keep++
155 }
156 for len(active) > keep {
157 out.WriteString(active[len(active)-1].close)
158 active = active[:len(active)-1]
159 }
160 for _, add := range each.marks[keep:] {
161 out.WriteString(add.open)
162 active = append(active, add)
163 }
164 out.WriteString(each.html)
165 }
166 for len(active) > 0 {
167 out.WriteString(active[len(active)-1].close)
168 active = active[:len(active)-1]
169 }
170 if needsBreak(pieces) {
171 out.WriteString(trailingBreak)
172 }
173 return out.String()
174}
175
176// needsBreak is prosemirror-view's addTextblockHacks rule: an empty
177// textblock, or one whose last child is not text or is text ending in a
178// newline.
179func needsBreak(pieces []piece) bool {
180 if len(pieces) == 0 {
181 return true
182 }
183 last := pieces[len(pieces)-1]
184 return !last.text || strings.HasSuffix(last.html, "\n")
185}
186