Last active
May 31, 2022 21:21
-
-
Save ckxng/58cdce00ff29d9e816894bd5bc42df8f to your computer and use it in GitHub Desktop.
golang markdown-cli
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 ( | |
"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