forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (52 loc) · 1.64 KB
/
main.go
File metadata and controls
63 lines (52 loc) · 1.64 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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/JohannesKaufmann/dom"
"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base"
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark"
"golang.org/x/net/html"
)
func main() {
input := `
<h2>
<span>Golang</span>
<star-rating count="5">five stars</star-rating>
</h2>
<p>Build simple, secure, <i>scalable</i> systems with Go</p>
`
conv := converter.NewConverter(
converter.WithPlugins(
base.NewBasePlugin(),
commonmark.NewCommonmarkPlugin(),
),
)
// Here we a registering a custom *renderer* for <star-rating> and pass in our function.
conv.Register.RendererFor("star-rating", converter.TagTypeInline, renderStarRating, converter.PriorityStandard)
markdown, err := conv.ConvertString(input)
if err != nil {
log.Fatal(err)
}
fmt.Println(markdown)
// ## Golang ⭐️⭐️⭐️⭐️⭐️
//
// Build simple, secure, *scalable* systems with Go
}
func renderStarRating(ctx converter.Context, w converter.Writer, node *html.Node) converter.RenderStatus {
// The "github.com/JohannesKaufmann/dom" package provides helper functions
// to interact with the html node, like getting the attribute "count".
rawCount := dom.GetAttributeOr(node, "count", "0")
count, _ := strconv.Atoi(rawCount)
rating := strings.Repeat("⭐️", count)
// Write the content
w.WriteString(rating)
// w.WriteString(" (")
// ctx.RenderChildNodes(ctx, w, node)
// w.WriteString(")")
// And then return whether it was a *success*
// or if the next renderer should be tried.
return converter.RenderSuccess
}