forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
533 lines (439 loc) · 12.2 KB
/
utils.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package md
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
/*
WARNING: The functions from this file can be used externally
but there is no garanty that they will stay exported.
*/
// CollectText returns the text of the node and all its children
func CollectText(n *html.Node) string {
text := &bytes.Buffer{}
collectText(n, text)
return text.String()
}
func collectText(n *html.Node, buf *bytes.Buffer) {
if n.Type == html.TextNode {
buf.WriteString(n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
collectText(c, buf)
}
}
func getName(node *html.Node) string {
selec := &goquery.Selection{Nodes: []*html.Node{node}}
return goquery.NodeName(selec)
}
// What elements automatically trim their content?
// Don't add another space if the other element is going to add a
// space already.
func isTrimmedElement(name string) bool {
nodes := []string{
"a",
"strong", "b",
"i", "em",
"del", "s", "strike",
"code",
}
for _, node := range nodes {
if name == node {
return true
}
}
return false
}
func getPrevNodeText(node *html.Node) (string, bool) {
if node == nil {
return "", false
}
for ; node != nil; node = node.PrevSibling {
text := CollectText(node)
name := getName(node)
if name == "br" {
return "\n", true
}
// if the content is empty, try our luck with the next node
if strings.TrimSpace(text) == "" {
continue
}
if isTrimmedElement(name) {
text = strings.TrimSpace(text)
}
return text, true
}
return "", false
}
func getNextNodeText(node *html.Node) (string, bool) {
if node == nil {
return "", false
}
for ; node != nil; node = node.NextSibling {
text := CollectText(node)
name := getName(node)
if name == "br" {
return "\n", true
}
// if the content is empty, try our luck with the next node
if strings.TrimSpace(text) == "" {
continue
}
// if you have "a a a", three elements that are trimmed, then only add
// a space to one side, since the other's are also adding a space.
if isTrimmedElement(name) {
text = " "
}
return text, true
}
return "", false
}
// AddSpaceIfNessesary adds spaces to the text based on the neighbors.
// That makes sure that there is always a space to the side, to recognize the delimiter.
func AddSpaceIfNessesary(selec *goquery.Selection, markdown string) string {
if len(selec.Nodes) == 0 {
return markdown
}
rootNode := selec.Nodes[0]
prev, hasPrev := getPrevNodeText(rootNode.PrevSibling)
if hasPrev {
lastChar, size := utf8.DecodeLastRuneInString(prev)
if size > 0 && !unicode.IsSpace(lastChar) {
markdown = " " + markdown
}
}
next, hasNext := getNextNodeText(rootNode.NextSibling)
if hasNext {
firstChar, size := utf8.DecodeRuneInString(next)
if size > 0 && !unicode.IsSpace(firstChar) && !unicode.IsPunct(firstChar) {
markdown = markdown + " "
}
}
return markdown
}
func isLineCodeDelimiter(chars []rune) bool {
if len(chars) < 3 {
return false
}
// TODO: If it starts with 4 (instead of 3) fence characters, we should only end it
// if we see the same amount of ending fence characters.
return chars[0] == '`' && chars[1] == '`' && chars[2] == '`'
}
// TrimpLeadingSpaces removes spaces from the beginning of a line
// but makes sure that list items and code blocks are not affected.
func TrimpLeadingSpaces(text string) string {
var insideCodeBlock bool
lines := strings.Split(text, "\n")
for index := range lines {
chars := []rune(lines[index])
if isLineCodeDelimiter(chars) {
if !insideCodeBlock {
// start the code block
insideCodeBlock = true
} else {
// end the code block
insideCodeBlock = false
}
}
if insideCodeBlock {
// We are inside a code block and don't want to
// disturb that formatting (e.g. python indentation)
continue
}
var spaces int
for i := 0; i < len(chars); i++ {
if unicode.IsSpace(chars[i]) {
if chars[i] == ' ' {
spaces = spaces + 4
} else {
spaces++
}
continue
}
// this seems to be a list item
if chars[i] == '-' {
break
}
// this seems to be a code block
if spaces >= 4 {
break
}
// remove the space characters from the string
chars = chars[i:]
break
}
lines[index] = string(chars)
}
return strings.Join(lines, "\n")
}
// TrimTrailingSpaces removes unnecessary spaces from the end of lines.
func TrimTrailingSpaces(text string) string {
parts := strings.Split(text, "\n")
for i := range parts {
parts[i] = strings.TrimRightFunc(parts[i], func(r rune) bool {
return unicode.IsSpace(r)
})
}
return strings.Join(parts, "\n")
}
// The same as `multipleNewLinesRegex`, but applies to escaped new lines inside a link `\n\`
var multipleNewLinesInLinkRegex = regexp.MustCompile(`(\n\\){1,}`) // `([\n\r\s]\\)`
// EscapeMultiLine deals with multiline content inside a link
func EscapeMultiLine(content string) string {
content = strings.TrimSpace(content)
content = strings.Replace(content, "\n", `\`+"\n", -1)
content = multipleNewLinesInLinkRegex.ReplaceAllString(content, "\n\\")
return content
}
func calculateCodeFenceOccurrences(fenceChar rune, content string) int {
var occurrences []int
var charsTogether int
for _, char := range content {
// we encountered a fence character, now count how many
// are directly afterwards
if char == fenceChar {
charsTogether++
} else if charsTogether != 0 {
occurrences = append(occurrences, charsTogether)
charsTogether = 0
}
}
// if the last element in the content was a fenceChar
if charsTogether != 0 {
occurrences = append(occurrences, charsTogether)
}
return findMax(occurrences)
}
// CalculateCodeFence can be passed the content of a code block and it returns
// how many fence characters (` or ~) should be used.
//
// This is useful if the html content includes the same fence characters
// for example ```
// -> https://stackoverflow.com/a/49268657
func CalculateCodeFence(fenceChar rune, content string) string {
repeat := calculateCodeFenceOccurrences(fenceChar, content)
// the outer fence block always has to have
// at least one character more than any content inside
repeat++
// you have to have at least three fence characters
// to be recognized as a code block
if repeat < 3 {
repeat = 3
}
return strings.Repeat(string(fenceChar), repeat)
}
func findMax(a []int) (max int) {
for i, value := range a {
if i == 0 {
max = a[i]
}
if value > max {
max = value
}
}
return max
}
func getCodeWithoutTags(startNode *html.Node) []byte {
var buf bytes.Buffer
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && (n.Data == "style" || n.Data == "script" || n.Data == "textarea") {
return
}
if n.Type == html.ElementNode && (n.Data == "br" || n.Data == "div") {
buf.WriteString("\n")
}
if n.Type == html.TextNode {
buf.WriteString(n.Data)
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(startNode)
return buf.Bytes()
}
// getCodeContent gets the content of pre/code and unescapes the encoded characters.
// Returns "" if there is an error.
func getCodeContent(selec *goquery.Selection) string {
if len(selec.Nodes) == 0 {
return ""
}
code := getCodeWithoutTags(selec.Nodes[0])
return string(code)
}
// delimiterForEveryLine puts the delimiter not just at the start and end of the string
// but if the text is divided on multiple lines, puts the delimiters on every line with content.
//
// Otherwise the bold/italic delimiters won't be recognized if it contains new line characters.
func delimiterForEveryLine(text string, delimiter string) string {
lines := strings.Split(text, "\n")
for i, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
// Skip empty lines
continue
}
lines[i] = delimiter + line + delimiter
}
return strings.Join(lines, "\n")
}
// isWrapperListItem returns wether the list item has own
// content or is just a wrapper for another list.
// e.g. "<li><ul>..."
func isWrapperListItem(s *goquery.Selection) bool {
directText := s.Contents().Not("ul").Not("ol").Text()
noOwnText := strings.TrimSpace(directText) == ""
childIsList := s.ChildrenFiltered("ul").Length() > 0 || s.ChildrenFiltered("ol").Length() > 0
return noOwnText && childIsList
}
// getListStart returns the integer from which the counting
// for for the list items should start from.
// -> https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol#start
func getListStart(parent *goquery.Selection) int {
val := parent.AttrOr("start", "")
if val == "" {
return 1
}
num, err := strconv.Atoi(val)
if err != nil {
return 1
}
if num < 0 {
return 1
}
return num
}
// getListPrefix returns the appropriate prefix for the list item.
// For example "- ", "* ", "1. ", "01. ", ...
func getListPrefix(opt *Options, s *goquery.Selection) string {
if isWrapperListItem(s) {
return ""
}
parent := s.Parent()
if parent.Is("ul") {
return opt.BulletListMarker + " "
} else if parent.Is("ol") {
start := getListStart(parent)
currentIndex := start + s.Index()
lastIndex := parent.Children().Last().Index() + 1
maxLength := len(strconv.Itoa(lastIndex))
// pad the numbers so that all prefix numbers in the list take up the same space
// `%02d.` -> "01. "
format := `%0` + strconv.Itoa(maxLength) + `d. `
return fmt.Sprintf(format, currentIndex)
}
// If the HTML is malformed and the list element isn't in a ul or ol, return no prefix
return ""
}
// countListParents counts how much space is reserved for the prefixes at all the parent lists.
// This is useful to calculate the correct level of indentation for nested lists.
func countListParents(opt *Options, selec *goquery.Selection) (int, int) {
var values []int
for n := selec.Parent(); n != nil; n = n.Parent() {
if n.Is("li") {
continue
}
if !n.Is("ul") && !n.Is("ol") {
break
}
prefix := n.Children().First().AttrOr(attrListPrefix, "")
values = append(values, len(prefix))
}
// how many spaces are reserved for the prefixes of my siblings
var prefixCount int
// how many spaces are reserved in total for all of the other
// list parents up the tree
var previousPrefixCounts int
for i, val := range values {
if i == 0 {
prefixCount = val
continue
}
previousPrefixCounts += val
}
return prefixCount, previousPrefixCounts
}
// IndentMultiLineListItem makes sure that multiline list items
// are properly indented.
func IndentMultiLineListItem(opt *Options, text string, spaces int) string {
parts := strings.Split(text, "\n")
for i := range parts {
// dont touch the first line since its indented through the prefix
if i == 0 {
continue
}
if isListItem(opt, parts[i]) {
return strings.Join(parts, "\n")
}
indent := strings.Repeat(" ", spaces)
parts[i] = indent + parts[i]
}
return strings.Join(parts, "\n")
}
// isListItem checks wether the line is a markdown list item
func isListItem(opt *Options, line string) bool {
b := []rune(line)
bulletMarker := []rune(opt.BulletListMarker)[0]
var hasNumber bool
var hasMarker bool
var hasSpace bool
for i := 0; i < len(b); i++ {
// A marker followed by a space qualifies as a list item
if hasMarker && hasSpace {
if b[i] == bulletMarker {
// But if another BulletListMarker is found, it
// might be a HorizontalRule
return false
}
if !unicode.IsSpace(b[i]) {
// Now we have some text
return true
}
}
if hasMarker {
if unicode.IsSpace(b[i]) {
hasSpace = true
continue
}
// A marker like "1." that is not immediately followed by a space
// is probably a false positive
return false
}
if b[i] == bulletMarker {
hasMarker = true
continue
}
if hasNumber && b[i] == '.' {
hasMarker = true
continue
}
if unicode.IsDigit(b[i]) {
hasNumber = true
continue
}
if unicode.IsSpace(b[i]) {
continue
}
// If we encouter any other character
// before finding an indicator, its
// not a list item
return false
}
return false
}
// IndexWithText is similar to goquery's Index function but
// returns the index of the current element while
// NOT counting the empty elements beforehand.
func IndexWithText(s *goquery.Selection) int {
return s.PrevAll().FilterFunction(func(i int, s *goquery.Selection) bool {
return strings.TrimSpace(s.Text()) != ""
}).Length()
}