renderinghtml

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 16 Imported by: 0

README

Rendering HTML

From Markdown to HTML

With syntax highlighting 🎨

func h(w http.ResponseWriter, r *http.Request) {
  title := "It works! 👷"
  md := []byte("# It works!\n```go\nvar k = 4\nprintln(k)\n```\n")
  err := renderinghtml.Markdown2HTMLPage(w, md, title)
  if err != nil {
    slog.Error("render", "err", err)
    w.WriteHeader(http.StatusInternalServerError)
    return
  }
}

If you are seeing this in your web browser, try visiting the test page.


Screenshot

screenshot-1

Features

  • Markdown Rendering
  • Syntax Highlighting
  • Minify CSS/JS/HTML

Code

echo "hello world"
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}
console.log("hello world");
#include <stdio.h>
int main() {
    printf("hello world\n");
    return 0;
}
SELECT * FROM users WHERE id = 1;

Commands

Included is a basic markdown2html command

markdown2html README.md
# Outputs:
# /tmp/markdown2html_README.html
# open browser
xdg-open file:///$(markdown2html README.md)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTheme       = "tokyonight-night"
	DefaultClassPrefix = "u-" // Injected into "./default.template.html" as {{.ClassPrefix}}
)
View Source
var (
	MinifyCSS  = minify.CSS
	MinifyJS   = minify.JS
	MinifySVG  = minify.SVG
	MinifyHTML = minify.HTML
	MinifyXML  = minify.XML
	MinifyJSON = minify.JSON
)

minifiers

View Source
var DefaultCompilePageData = func(out io.Writer, x map[string]any) error {
	t, err := template.New("").
		Funcs(Funcmap).
		Option("missingkey=zero").
		ParseFS(FS, "default.template.html")
	if err != nil {
		return err
	}
	return t.ExecuteTemplate(out, "default.template.html", x)
}
View Source
var Errorf = stackerr.Errorf
View Source
var FS fs.FS = embedded

FS that contains "./default.template.html"

View Source
var Funcmap = template.FuncMap{
	"minifyCSS":   MinifyCSS,
	"minifyJS":    MinifyJS,
	"minifySVG":   MinifySVG,
	"minifyHTML":  MinifyHTML,
	"minifyXML":   MinifyXML,
	"minifyJSON":  MinifyJSON,
	"minifyHTML2": MinifyHTML2,
}
View Source
var HTMLMinifier = mhtml.Minifier{
	KeepDefaultAttrVals: true,
	KeepDocumentTags:    true,
	KeepQuotes:          true,
	KeepWhitespace:      true,
	KeepEndTags:         true,
}
View Source
var Minify = minify.Default

Functions

func CompilePage

func CompilePage(out io.Writer, title, css, content string, headtags []string) error

CompilePage for AFTER rendering. Uses internal embedded template.

Puts the title, css, (and markdown2html pre-rendered body), into a basic html page

func Easy

func Easy(title, content string) string

func Markdown2HTMLPage

func Markdown2HTMLPage(out io.Writer, in []byte, title string) error

func Minifier

func Minifier(ct string, in string) string

func MinifierStream

func MinifierStream(mediatype string, out io.Writer, in io.Reader) (n int64, err error)

func MinifyHTML2

func MinifyHTML2(s string) (string, error)

Types

type Options

type Options struct {
	CompletePage    bool
	WithCSS         bool
	WithClassPrefix string
	Theme           string
	NoLineNumbers   bool
	WithClasses     bool
	DoWrap          bool // messes with line numbers
	NoMinify        bool

	html.Renderer
	parser.Parser
	Context renderer.Context
	Page
}

Options has Options.Init method which must be called befure Options.Markdown2HTMLString or Options.Markdown2HTMLString

see [defaultoption]

func Cfg

func Cfg() *Options

Cfg to modify the default before Init

func New

func New(title string) Options

func (*Options) Init

func (o *Options) Init()

func (Options) Markdown2HTML

func (o Options) Markdown2HTML(out io.Writer, in []byte) error

func (Options) Markdown2HTMLString

func (o Options) Markdown2HTMLString(in []byte) (string, error)

type Page

type Page struct {
	Title    string
	HeadTags []string
}

Directories

Path Synopsis
cmd
markdown2html command
tester command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL