forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (40 loc) · 1.35 KB
/
main.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
package main
import (
"fmt"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
md "github.com/arangodb-managed/html-to-markdown"
)
func main() {
html := `Good sountrack <span class="bb_strike"> and cake</span>.`
// -> `Good sountrack ~and cake~.`
/*
We want to add a rule when a `span` tag has a class of `bb_strike`.
Have a look at `plugin/strikethrough.go` to see how it is implemented normally.
*/
strikethrough := md.Rule{
Filter: []string{"span"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
// If the span element has not the classname `bb_strike` return nil.
// That way the next rules will apply. In this case the commonmark rules.
// -> return nil -> next rule applies
if !selec.HasClass("bb_strike") {
return nil
}
// Trim spaces so that the following does NOT happen: `~ and cake~`.
// Because of the space it is not recognized as strikethrough.
// -> trim spaces at begin&end of string when inside strong/italic/...
content = strings.TrimSpace(content)
return md.String("~" + content + "~")
},
}
conv := md.NewConverter("", true, nil)
conv.AddRules(strikethrough)
// -> add 1+ rules to the converter. the last added will be used first.
markdown, err := conv.ConvertString(html)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\nmarkdown:'%s'\n", markdown)
}