Last active
September 1, 2018 02:25
-
-
Save rtfb/08babeb67d37514f5322722797a6bd6a to your computer and use it in GitHub Desktop.
Demonstrates a simple usage of AST in Blackfriday v2
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
package main | |
import ( | |
"bytes" | |
"fmt" | |
bf "gopkg.in/russross/blackfriday.v2" | |
) | |
func main() { | |
md := []byte("Foo\n\n---\n\nBar") | |
html := bf.MarkdownCommon(md) | |
ast := bf.Parse(md, bf.DefaultOptions) | |
var buff bytes.Buffer | |
defaultRenderer := bf.NewHTMLRenderer(bf.HTMLRendererParameters{}) | |
ast.Walk(func(node *bf.Node, entering bool) bf.WalkStatus { | |
if node.Type == bf.HorizontalRule { | |
buff.WriteString(`<div class="separator"></div>`) | |
} else { | |
defaultRenderer.RenderNode(&buff, node, entering) | |
} | |
return bf.GoToNext | |
}) | |
fmt.Printf("md: %q\nhtml: %q\ncustom html: %q\n", md, html, buff.Bytes()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I sorted it out. Here's an example that works with the current blackfriday.v2 using New and Parse: