-
-
Save tomcam/063430a32e40979736cf78bf172c42d9 to your computer and use it in GitHub Desktop.
goldmark example with app object
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
// Demonstrates the goldmark Markdown to HTML converter. | |
// Creates a simple Markdown source file named "foo.md". | |
// Calls goldmark to convert it to HTML. | |
// Displays results to standard output | |
// $ mkdir ~/g | |
// $ cd ~/g | |
// $ go mod init example.com/g # example.com is OK to use in this quick & dirty example | |
// $ go mod tidy | |
// $ go run g.go | |
package main | |
import ( | |
"bytes" | |
"fmt" | |
"os" | |
"github.com/yuin/goldmark" | |
"github.com/yuin/goldmark/parser" | |
"github.com/yuin/goldmark-meta" | |
"github.com/yuin/goldmark-highlighting" | |
"github.com/yuin/goldmark/extension" | |
"github.com/yuin/goldmark/renderer" | |
"github.com/yuin/goldmark/renderer/html" | |
) | |
const sampleFile = ` | |
# Simple Goldmark demo | |
hello, world. | |
` | |
type App struct { | |
parser goldmark.Markdown | |
parserCtx parser.Context | |
} | |
// newGoldmark returns the a goldmark object with a parser and renderer. | |
func (app *App) newGoldmark() goldmark.Markdown { | |
exts := []goldmark.Extender{ | |
// YAML support | |
meta.Meta, | |
// Support GitHub tables & other extensions | |
extension.Table, | |
extension.GFM, | |
extension.DefinitionList, | |
extension.Footnote, | |
highlighting.NewHighlighting( | |
highlighting.WithStyle("github"), | |
highlighting.WithFormatOptions()), | |
} | |
parserOpts := []parser.Option{ | |
parser.WithAttribute(), | |
parser.WithAutoHeadingID()} | |
renderOpts := []renderer.Option{ | |
// WithUnsafe is required for HTML templates to work properly | |
html.WithUnsafe(), | |
html.WithXHTML(), | |
} | |
return goldmark.New( | |
goldmark.WithExtensions(exts...), | |
goldmark.WithParserOptions(parserOpts...), | |
goldmark.WithRendererOptions(renderOpts...), | |
) | |
} | |
func NewApp() *App { | |
app := App{ | |
} | |
// Belongs elsewhere in production | |
// Parser couldn't be initialized until command line and | |
// other options were processed | |
app.parser = app.newGoldmark() | |
app.parserCtx = parser.NewContext() | |
return &app | |
} | |
func (app *App) mdToHTML(source []byte) ([]byte, error) { | |
var buf bytes.Buffer | |
if err := app.parser.Convert(source, &buf, parser.WithContext(app.parserCtx)); err != nil { | |
return buf.Bytes(), err | |
} | |
return buf.Bytes(), nil | |
} | |
func main() { | |
var app = NewApp() | |
if b, err := app.mdToHTML([]byte(sampleFile)); err != nil { | |
quit(err, 1) | |
} else { | |
fmt.Println(string(b)) | |
} | |
if b, err := simpleMdToHTML([]byte(sampleFile)); err != nil { | |
quit(err, 1) | |
} else { | |
fmt.Println(string(b)) | |
quit(nil, 0) | |
} | |
} | |
func simpleMdToHTML(input []byte) ([]byte, error) { | |
var buf bytes.Buffer | |
if err := goldmark.Convert(input, &buf); err != nil { | |
return []byte(""), err | |
} | |
return buf.Bytes(), nil | |
} | |
func quit(err error, exitCode int) { | |
if err != nil { | |
fmt.Println(err) | |
} | |
os.Exit(exitCode) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment