escape.gno
3.57 Kb · 94 lines
1package questmaster
2
3import (
4 "strings"
5
6 "gno.land/p/nt/markdown/sanitize/v0"
7)
8
9// Escaping. Every piece of user text that reaches a page (titles, details,
10// reject reasons, work links) goes through one of the helpers below, which
11// wrap p/nt/markdown/sanitize/v0 (the sanitizer that gnoweb's own markdown
12// tests are written against) plus the gnoweb-specific steps described below.
13//
14// Nothing a user types can produce a heading, list, table cell break, new
15// table row, link, image, HTML tag, mention link or math:
16// - esc: single-line slots (headings, link text). Folds line breaks to
17// spaces and backslash-escapes Markdown punctuation and `<`, `&`.
18// - escCell: table cells. As esc, plus `|` and tabs.
19// - escBlock: multi-line text (details, reasons). A blockquote that keeps
20// line breaks; every line is escaped like a table cell.
21// - linkOrText: the work link. Clickable only when it passes sanitize.URL
22// (http/https only; ( ) < > ` | and other unsafe bytes percent-encoded)
23// and nothing unsafe is left in the target; otherwise plain text. Its
24// visible text is always the escaped URL itself, never user-chosen text.
25//
26// gnoweb adds two extensions the sanitizer does not know about, so every
27// helper also runs gnowebSafe on its output:
28// - "@name" and " g1…" (an address after a space) become profile links.
29// "@" becomes "\@" (a Markdown escape that renders as "@"), the space
30// before "g1" becomes the entity " ", and a "g1" at the very start of
31// the text becomes "g1", because the realm itself often puts a space
32// right before user text ("# Quest 1: ", "| ", "> ").
33// - "$…$" and "$$…$$" become math. Every "$" becomes "\$".
34// Each renders as the plain character in gnoweb; checked in the real renderer
35// by scripts/local-flow.sh.
36
37func esc(s string) string { return gnowebSafe(sanitize.InlineText(s)) }
38func escCell(s string) string { return gnowebSafe(sanitize.TableCell(s)) }
39
40// escBlock shows multi-line user text as a blockquote that keeps its line
41// breaks but nothing else: every line is escaped like a table cell (so no
42// link, image, HTML, `|` table or line-leading marker survives), a leading
43// "=" is escaped (no setext heading), and lines end with a hard break.
44func escBlock(s string) string {
45 s = sanitize.NormalizeBreaks(sanitize.StripBidiAndZeroWidth(s))
46 s = strings.ReplaceAll(s, "\u2028", "\n")
47 s = strings.ReplaceAll(s, "\u2029", "\n")
48 s = strings.ReplaceAll(s, "\u0085", "\n")
49 var b strings.Builder
50 blank := true // suppress leading and repeated blank lines
51 for _, line := range strings.Split(s, "\n") {
52 line = strings.TrimSpace(line)
53 if line == "" {
54 if !blank {
55 b.WriteString(">\n")
56 }
57 blank = true
58 continue
59 }
60 line = escCell(line)
61 if strings.HasPrefix(line, "=") {
62 line = "\\" + line
63 }
64 b.WriteString("> " + line + " \n")
65 blank = false
66 }
67 out := strings.TrimSuffix(b.String(), ">\n")
68 if out == "" {
69 return ""
70 }
71 return "\n" + out + "\n"
72}
73
74// gnowebSafe applies the gnoweb-specific steps described above.
75func gnowebSafe(s string) string {
76 s = strings.ReplaceAll(s, "@", `\@`)
77 s = strings.ReplaceAll(s, "$", `\$`)
78 s = strings.ReplaceAll(s, " g1", " g1")
79 if strings.HasPrefix(s, "g1") {
80 s = "g1" + s[2:]
81 }
82 return s
83}
84
85// linkOrText shows a work link. The escrow already guarantees it starts
86// with https:// and has no spaces or control characters.
87func linkOrText(link string) string {
88 text := escCell(link)
89 u := sanitize.URL(link)
90 if u == "" || strings.ContainsAny(u, "()<>[]| \"'`") {
91 return text
92 }
93 return "[" + text + "](" + u + ")"
94}