-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml2text.go
56 lines (48 loc) · 1.08 KB
/
html2text.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
package md2html
import (
"bytes"
"io"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
gm_html "github.com/yuin/goldmark/renderer/html"
"golang.org/x/net/html"
)
func convertHtmlNodeText(e *html.Node, w io.Writer) error {
if e.Type == html.TextNode {
_, err := io.WriteString(w, e.Data)
return err
}
for c := e.FirstChild; c != nil; c = c.NextSibling {
err := convertHtmlNodeText(c, w)
if err != nil {
return err
}
}
return nil
}
func convertHtmlToText(html_bin []byte, w io.Writer) error {
r := bytes.NewReader(html_bin)
root, err := html.Parse(r)
if err != nil {
_, err := io.WriteString(w, "id")
return err
}
return convertHtmlNodeText(root, w)
}
func convertMdToText(value []byte, w io.Writer) error {
p := goldmark.New(
goldmark.WithExtensions(),
goldmark.WithParserOptions(
parser.WithAttribute(),
),
goldmark.WithRendererOptions(
gm_html.WithXHTML(),
gm_html.WithUnsafe(),
),
)
var html_buf bytes.Buffer
if err := p.Convert(value, &html_buf); err != nil {
return err
}
return convertHtmlToText(html_buf.Bytes(), w)
}