forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovefrontmatter.go
90 lines (73 loc) · 2.13 KB
/
movefrontmatter.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
package plugin
import (
"strings"
md "github.com/tomkosm/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)
const moveFrontmatterAttr = "movefrontmatter"
// EXPERIMENTALMoveFrontMatter moves a frontmatter block at the beginning
// of the document to the top of the generated markdown block, without touching (and escaping) it.
func EXPERIMENTALMoveFrontMatter(delimiters ...rune) md.Plugin {
return func(c *md.Converter) []md.Rule {
if len(delimiters) == 0 {
delimiters = []rune{'+', '$', '-', '%'}
}
var delimitersList []string
for _, c := range delimiters {
delimitersList = append(delimitersList, strings.Repeat(string(c), 3))
}
isDelimiter := func(line string) bool {
for _, delimiter := range delimitersList {
if strings.HasPrefix(line, delimiter) {
return true
}
}
return false
}
c.Before(func(selec *goquery.Selection) {
selec.Find("body").Contents().EachWithBreak(func(i int, s *goquery.Selection) bool {
text := s.Text()
// skip empty strings
if strings.TrimSpace(text) == "" {
return true
}
var frontmatter string
var html string = text // if there is no frontmatter, keep the text
lines := strings.Split(text, "\n")
for i := 0; i < len(lines); i++ {
if isDelimiter(lines[i]) {
if i == 0 {
continue
}
// split the frontmatter
f := lines[:i+1]
frontmatter = strings.Join(f, "\n")
// and the html content AFTER the frontmatter
h := lines[i+1:]
html = strings.Join(h, "\n")
break
}
}
s.SetAttr(moveFrontmatterAttr, frontmatter)
s.SetText(html)
// the front matter must be the first thing in the file. So we break out of the loop
return false
})
})
return []md.Rule{
{
Filter: []string{"#text"},
AdvancedReplacement: func(content string, selec *goquery.Selection, opt *md.Options) (md.AdvancedResult, bool) {
frontmatter, exists := selec.Attr(moveFrontmatterAttr)
if !exists {
return md.AdvancedResult{}, true
}
return md.AdvancedResult{
Header: frontmatter,
Markdown: content,
}, false
},
},
}
}
}