forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfluence_code_block.go
41 lines (38 loc) · 1.35 KB
/
confluence_code_block.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
package plugin
import (
"fmt"
"strings"
md "github.com/tomkosm/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)
// ConfluenceCodeBlock converts `<ac:structured-macro>` elements that are used in Atlassian’s Wiki “Confluence”.
// [Contributed by @Skarlso]
func ConfluenceCodeBlock() md.Plugin {
return func(c *md.Converter) []md.Rule {
character := "```"
return []md.Rule{
{
Filter: []string{"ac:structured-macro"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
for _, node := range selec.Nodes {
if node.Data == "ac:structured-macro" {
// node's last child -> <ac:plain-text-body>. We don't want to filter on that
// because we would end up with structured-macro around us.
// ac:plain-text-body's last child is [CDATA which has the actual content we are looking for.
data := strings.TrimPrefix(node.LastChild.LastChild.Data, "[CDATA[")
data = strings.TrimSuffix(data, "]]")
// content, if set, will contain the language that has been set in the field.
var language string
if content != "" {
language = content
}
formatted := fmt.Sprintf("%s%s\n%s\n%s", character, language, data, character)
return md.String(formatted)
}
}
return md.String(character + content + character)
},
},
}
}
}