Skip to content

Instantly share code, notes, and snippets.

@joparara
Created March 7, 2024 22:13
Show Gist options
  • Save joparara/f815b020d58b2bc2ab2166a0f98aa9a8 to your computer and use it in GitHub Desktop.
Save joparara/f815b020d58b2bc2ab2166a0f98aa9a8 to your computer and use it in GitHub Desktop.
gotta go - a simple readme markdown output to an html page
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
func main() {
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Error:", err)
return
}
mdfile := "README.md"
text, err := ioutil.ReadFile(mdfile)
if err != nil {
fmt.Println("Error:", err)
return
}
html := blackfriday.Run(text)
sanitizedHTML := bluemonday.UGCPolicy().SanitizeBytes(html)
var title string
if len(os.Args) > 1 {
title = os.Args[1]
} else {
title = "Custom Title"
}
head := "<!doctype html><html><head><title>" + title + "</title>"
head += "<meta charset=\"utf-8\">"
head += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><link rel=\"stylesheet\" href=\"site.css\">"
head += "</head>"
htmlString := string(head) + "<body>" + string(sanitizedHTML) + "</body>"
err = ioutil.WriteFile("output.html", []byte(htmlString), 0644)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("README > output.html")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment