-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.go
More file actions
79 lines (72 loc) · 1.98 KB
/
Copy pathmarkdown.go
File metadata and controls
79 lines (72 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"regexp"
"strings"
)
var (
boldRe = regexp.MustCompile(`\*\*(.+?)\*\*`)
codeRe = regexp.MustCompile("`([^`]+)`")
)
// escText escapes body text for HTML content (quotes stay raw, as in the
// reference product pages).
func escText(s string) string {
r := strings.NewReplacer("&", "&", "<", "<", ">", ">")
return r.Replace(s)
}
// inline renders the minimal inline markdown subset (bold, code) on body text.
func inline(s string) string {
s = escText(s)
s = boldRe.ReplaceAllString(s, "<strong>$1</strong>")
s = codeRe.ReplaceAllString(s, "<code>$1</code>")
return s
}
// renderBody converts a product .md body into the product-page inner HTML,
// matching the validated pages: leading "# Title" dropped; "## X" -> <h2>;
// "- " runs -> <ul>; blank-separated paragraphs -> <p>. Blocks are indented
// with six spaces (list items eight) to match the reference output.
func renderBody(body string) string {
lines := strings.Split(body, "\n")
var out []string
var para []string
var list []string
flushPara := func() {
if len(para) == 0 {
return
}
out = append(out, " <p>"+inline(strings.Join(para, " "))+"</p>")
para = nil
}
flushList := func() {
if len(list) == 0 {
return
}
out = append(out, " <ul>")
for _, it := range list {
out = append(out, " <li>"+inline(it)+"</li>")
}
out = append(out, " </ul>")
list = nil
}
flush := func() { flushPara(); flushList() }
for _, raw := range lines {
line := strings.TrimRight(raw, " ")
t := strings.TrimSpace(line)
switch {
case t == "":
flush()
case strings.HasPrefix(t, "## "):
flush()
out = append(out, " <h2>"+inline(strings.TrimSpace(t[3:]))+"</h2>")
case strings.HasPrefix(t, "# "):
flush() // page title comes from frontmatter name; drop body H1
case strings.HasPrefix(t, "- "):
flushPara()
list = append(list, strings.TrimSpace(t[2:]))
default:
flushList()
para = append(para, t)
}
}
flush()
return strings.Join(out, "\n")
}