-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
readme: improve documentation for plugins
- Loading branch information
1 parent
0f887f8
commit 099f6f2
Showing
12 changed files
with
125 additions
and
29 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/JohannesKaufmann/dom" | ||
"github.com/JohannesKaufmann/html-to-markdown/v2/converter" | ||
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base" | ||
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark" | ||
"golang.org/x/net/html" | ||
) | ||
|
||
func main() { | ||
input := ` | ||
<h2> | ||
<span>Golang</span> | ||
<star-rating count="5">five stars</star-rating> | ||
</h2> | ||
<p>Build simple, secure, <i>scalable</i> systems with Go</p> | ||
` | ||
|
||
conv := converter.NewConverter( | ||
converter.WithPlugins( | ||
base.NewBasePlugin(), | ||
commonmark.NewCommonmarkPlugin(), | ||
), | ||
) | ||
|
||
// Here we a registering a custom *renderer* for <star-rating> and pass in our function. | ||
conv.Register.RendererFor("star-rating", converter.TagTypeInline, renderStarRating, converter.PriorityStandard) | ||
|
||
markdown, err := conv.ConvertString(input) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(markdown) | ||
// ## Golang ⭐️⭐️⭐️⭐️⭐️ | ||
// | ||
// Build simple, secure, *scalable* systems with Go | ||
} | ||
|
||
func renderStarRating(ctx converter.Context, w converter.Writer, node *html.Node) converter.RenderStatus { | ||
// The "github.com/JohannesKaufmann/dom" package provides helper functions | ||
// to interact with the html node, like getting the attribute "count". | ||
rawCount := dom.GetAttributeOr(node, "count", "0") | ||
count, _ := strconv.Atoi(rawCount) | ||
|
||
rating := strings.Repeat("⭐️", count) | ||
|
||
// Write the content | ||
w.WriteString(rating) | ||
|
||
// w.WriteString(" (") | ||
// ctx.RenderChildNodes(ctx, w, node) | ||
// w.WriteString(")") | ||
|
||
// And then return whether it was a *success* | ||
// or if the next renderer should be tried. | ||
return converter.RenderSuccess | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters