Skip to content

Instantly share code, notes, and snippets.

@jvns

jvns/go.mod Secret

Created October 7, 2024 20:56
Show Gist options
  • Save jvns/9cc3024ff98433ced5e3a2304c5fc5e4 to your computer and use it in GitHub Desktop.
Save jvns/9cc3024ff98433ced5e3a2304c5fc5e4 to your computer and use it in GitHub Desktop.
module github.com/jvns/markdown-compare
go 1.23.1
require (
github.com/russross/blackfriday v1.6.0 // indirect
github.com/yuin/goldmark v1.7.4 // indirect
)
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"github.com/russross/blackfriday"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
func main() {
// Read input from stdin
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println("Error reading input:", err)
return
}
// Convert markdown to HTML using blackfriday
blackfridayOutput := blackfriday.MarkdownBasic(input)
// Convert markdown to HTML using goldmark
var goldmarkOutput bytes.Buffer
// allow html unsafe
// do typography
md := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
if err := md.Convert(input, &goldmarkOutput); err != nil {
fmt.Println("Error converting markdown using goldmark:", err)
return
}
// Print out both results
fmt.Println("Blackfriday HTML Output:")
fmt.Println(string(blackfridayOutput))
fmt.Println("Goldmark HTML Output:")
fmt.Println(goldmarkOutput.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment