Skip to content
This repository was archived by the owner on Jun 15, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ Or
or

```Go
html2md.AddConvert(func(content string) string {
html2md.AddConvert(60, func(content string) string {
return strings.ToLower(content)
})
```

Converts are executed in order. The builtin converts are 10-50. Choose
your own value to override the builtins or run yours before/after.

# Docs

* [GoDoc](http://godoc.org/github.com/lunny/html2md)
Expand Down
6 changes: 3 additions & 3 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func AttrRegExp(attr string) *regexp.Regexp {

var (
rules = make(map[string]*Rule)
converts = make([]func(string) string, 0)
converts = make(map[int]func(string) string)
)

func AddRule(name string, rule *Rule) {
rules[name] = rule
}

func AddConvert(f func(string) string) {
converts = append(converts, f)
func AddConvert(order int, f func(string) string) {
converts[order] = f
}
23 changes: 15 additions & 8 deletions html2md.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func wrapInlineTag(content, openWrap, closeWrap string) string {
}

func WrapInlineTag(content, openWrap, closeWrap string) string {
return wrapInlineTag(content, openWrap, closeWrap)
return wrapInlineTag(content, openWrap, closeWrap)
}

func init() {
Expand All @@ -334,11 +334,11 @@ func init() {
AddRule("code", Code())
AddRule("a", A())

AddConvert(pre)
AddConvert(ul)
AddConvert(ol)
AddConvert(blockQuote)
AddConvert(cleanUp)
AddConvert(10, pre)
AddConvert(20, ul)
AddConvert(30, ol)
AddConvert(40, blockQuote)
AddConvert(50, cleanUp)
}

func Convert(content string) string {
Expand All @@ -348,8 +348,15 @@ func Convert(content string) string {
}
}

for _, convert := range converts {
content = convert(content)
// Source: https://stackoverflow.com/a/18342865/504018
keys := make([]int, 0)
for k, _ := range converts {
keys = append(keys, k)
}
sort.Ints(keys)

for order := range keys {
content = convert[order](content)
}

return content
Expand Down