Skip to content

Instantly share code, notes, and snippets.

@ckxng
Last active May 31, 2022 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckxng/58cdce00ff29d9e816894bd5bc42df8f to your computer and use it in GitHub Desktop.
Save ckxng/58cdce00ff29d9e816894bd5bc42df8f to your computer and use it in GitHub Desktop.
golang markdown-cli
package main
import (
"io/ioutil"
"bytes"
"fmt"
"os"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
func main() {
// find filename from args and show usage
if(len(os.Args) != 2) {
fmt.Println("Usage: markdown filename.md")
os.Exit(1)
}
// read file
source, err := ioutil.ReadFile(os.Args[1]);
if err != nil {
panic(err)
}
// parse file
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
extension.Typographer,
extension.DefinitionList,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
),
)
var buf bytes.Buffer
if err := md.Convert(source, &buf); err != nil {
panic(err)
}
// print parsed file
fmt.Print(buf.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment