Skip to content

Commit

Permalink
add comments to ConvertX functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Aug 31, 2024
1 parent e304501 commit b3cd870
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"golang.org/x/net/html"
)

// ConvertString converts a html-string to a markdown-string.
//
// Under the hood `html.Parse()` is used to parse the HTML.
func ConvertString(htmlInput string) (string, error) {
conv := converter.NewConverter(
converter.WithPlugins(commonmark.NewCommonmarkPlugin()),
Expand All @@ -14,6 +17,11 @@ func ConvertString(htmlInput string) (string, error) {
return conv.ConvertString(htmlInput)
}

// ConvertNode converts a `*html.Node` to a markdown byte slice.
//
// If you have already parsed an HTML page using the `html.Parse()` function
// from the "golang.org/x/net/html" package then you can pass this node
// directly to the converter.
func ConvertNode(doc *html.Node) ([]byte, error) {
conv := converter.NewConverter(
converter.WithPlugins(commonmark.NewCommonmarkPlugin()),
Expand Down
11 changes: 11 additions & 0 deletions converter/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func (conv *Converter) getError() error {

var errNoRenderHandlers = errors.New("no render handlers are registered. did you forget to register the commonmark plugin?")

// ConvertNode converts a `*html.Node` to a markdown byte slice.
//
// If you have already parsed an HTML page using the `html.Parse()` function
// from the "golang.org/x/net/html" package then you can pass this node
// directly to the converter.
func (conv *Converter) ConvertNode(doc *html.Node, opts ...convertOptionFunc) ([]byte, error) {

if err := conv.getError(); err != nil {
Expand Down Expand Up @@ -98,6 +103,9 @@ func (conv *Converter) ConvertNode(doc *html.Node, opts ...convertOptionFunc) ([
return result, nil
}

// ConvertReader converts the html from the reader to markdown.
//
// Under the hood `html.Parse()` is used to parse the HTML.
func (conv *Converter) ConvertReader(r io.Reader, opts ...convertOptionFunc) ([]byte, error) {
doc, err := html.Parse(r)
if err != nil {
Expand All @@ -107,6 +115,9 @@ func (conv *Converter) ConvertReader(r io.Reader, opts ...convertOptionFunc) ([]
return conv.ConvertNode(doc, opts...)
}

// ConvertString converts a html-string to a markdown-string.
//
// Under the hood `html.Parse()` is used to parse the HTML.
func (conv *Converter) ConvertString(htmlInput string, opts ...convertOptionFunc) (string, error) {
r := strings.NewReader(htmlInput)
output, err := conv.ConvertReader(r, opts...)
Expand Down

0 comments on commit b3cd870

Please sign in to comment.