forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonmark.go
216 lines (193 loc) · 5.64 KB
/
commonmark.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package md
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
// "github.com/nehbit/html-to-markdown/escape"
)
var multipleSpacesR = regexp.MustCompile(` +`)
var commonmark = []Rule{
Rule{
Filter: []string{"ul", "ol"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
parent := selec.Parent()
if parent.Is("li") && parent.Children().Last().IsSelection(selec) {
// content = "\n" + content
// panic("ul&li -> parent is li & something")
} else {
content = "\n\n" + content + "\n\n"
}
return &content
},
},
Rule{
Filter: []string{"li"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
parent := selec.Parent()
index := selec.Index()
var prefix string
if parent.Is("ol") {
prefix = strconv.Itoa(index+1) + ". "
} else {
prefix = opt.BulletListMarker + " "
}
// remove leading newlines
content = leadingNewlinesR.ReplaceAllString(content, "")
// replace trailing newlines with just a single one
content = trailingNewlinesR.ReplaceAllString(content, "\n")
// indent
content = indentR.ReplaceAllString(content, "\n ")
return String(prefix + content + "\n")
},
},
Rule{
Filter: []string{"#text"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
text := selec.Text()
if trimmed := strings.TrimSpace(text); trimmed == "" {
return String("")
}
text = tabR.ReplaceAllString(text, " ")
// replace multiple spaces by one space: dont accidentally make
// normal text be indented and thus be a code block.
text = multipleSpacesR.ReplaceAllString(text, " ")
// text = escape.Markdown(text) // disable escapes
return &text
},
},
Rule{
Filter: []string{"p"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
parent := goquery.NodeName(selec.Parent())
if IsInlineElement(parent) || parent == "li" {
content = "\n" + content + "\n"
return &content
}
content = "\n\n" + content + "\n\n"
return &content
},
},
Rule{
Filter: []string{"h1", "h2", "h3", "h4", "h5", "h6"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
node := goquery.NodeName(selec)
level, err := strconv.Atoi(node[1:])
if err != nil {
panic(err)
}
if opt.HeadingStyle == "setext" && level < 3 {
line := "-"
if level == 1 {
line = "="
}
underline := strings.Repeat(line, len(content))
return String("\n\n" + content + "\n" + underline + "\n\n")
}
prefix := strings.Repeat("#", level)
content = strings.Replace(content, "\n", " ", -1)
content = strings.Replace(content, "\r", " ", -1)
text := "\n\n" + prefix + " " + content + "\n\n"
return &text
},
},
Rule{
Filter: []string{"strong", "b"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
trimmed := strings.TrimSpace(content)
if trimmed == "" {
return &trimmed
}
trimmed = opt.StrongDelimiter + trimmed + opt.StrongDelimiter
return &trimmed
},
},
Rule{
Filter: []string{"img"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
alt := selec.AttrOr("alt", "")
src, ok := selec.Attr("src")
if !ok {
return String("")
}
text := fmt.Sprintf("![%s](%s)", alt, src)
return &text
},
},
Rule{
Filter: []string{"a"},
AdvancedReplacement: func(content string, selec *goquery.Selection, opt *Options) (AdvancedResult, bool) {
href, ok := selec.Attr("href")
if !ok {
return AdvancedResult{}, true
}
var title string
if t, ok := selec.Attr("title"); ok {
title = fmt.Sprintf(` "%s"`, t)
}
if opt.LinkStyle == "inlined" {
return AdvancedResult{
Markdown: fmt.Sprintf("[%s](%s%s)", content, href, title),
}, false
}
var replacement string
var reference string
switch opt.LinkReferenceStyle {
case "collapsed":
replacement = "[" + content + "][]"
reference = "[" + content + "]: " + href + title
case "shortcut":
replacement = "[" + content + "]"
reference = "[" + content + "]: " + href + title
default:
id := selec.AttrOr("data-index", "")
replacement = "[" + content + "][" + id + "]"
reference = "[" + id + "]: " + href + title
}
return AdvancedResult{Markdown: replacement, Footer: reference}, false
},
},
Rule{
Filter: []string{"code"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
// TODO: configure delimeter in options?
text := "`" + content + "`"
return &text
},
},
Rule{
Filter: []string{"pre"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
codeElement := selec.Find("code")
language := codeElement.AttrOr("class", "")
language = strings.Replace(language, "language-", "", 1)
code := codeElement.Text()
if codeElement.Length() == 0 {
code = selec.Text()
}
text := "\n\n" + opt.Fence + language + "\n" +
code +
"\n" + opt.Fence + "\n\n"
return &text
},
},
Rule{
Filter: []string{"hr"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
text := "\n\n" + opt.HorizontalRule + "\n\n"
return &text
},
},
Rule{
Filter: []string{"blockquote"},
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
content = strings.TrimSpace(content)
content = multipleNewLinesRegex.ReplaceAllString(content, "\n\n")
var beginningR = regexp.MustCompile(`(?m)^`)
content = beginningR.ReplaceAllString(content, "> ")
text := "\n\n" + content + "\n\n"
return &text
},
},
}