markdown/highlighting

README
Edited: Sunday 22 June 2025

goldmark-highlighting

goldmark-highlighting is an extension for the goldmark
that adds syntax-highlighting to the fenced code blocks.

goldmark-highlighting uses chroma as a
syntax highlighter.

Installation

go get github.com/yuin/goldmark-highlighting/v2

Usage

 1package main
 2
 3import (
 4	"bytes"
 5	"fmt"
 6
 7	"github.com/yuin/goldmark"
 8
 9	chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
10	highlighting "github.com/yuin/goldmark-highlighting/v2"
11)
12
13func main() {
14	mdsrc := `
15		Title
16		=======
17		` + "```" + `
18		func main() {
19		    fmt.Println("ok")
20		}
21		` + "```" + `
22	`
23
24	// Simple usage
25	markdown := goldmark.New(
26		goldmark.WithExtensions(
27			highlighting.Highlighting,
28		),
29	)
30	var buf bytes.Buffer
31	if err := markdown.Convert([]byte(mdsrc), &buf); err != nil {
32		panic(err)
33	}
34	title := buf.String()
35	fmt.Print(title)
36
37	// Custom configuration
38	markdown2 := goldmark.New(
39		goldmark.WithExtensions(
40			highlighting.NewHighlighting(
41				highlighting.WithStyle("monokai"),
42				highlighting.WithFormatOptions(
43					chromahtml.WithLineNumbers(true),
44				),
45			),
46		),
47	)
48	var buf2 bytes.Buffer
49	if err := markdown2.Convert([]byte(mdsrc), &buf2); err != nil {
50		panic(err)
51	}
52	title2 := buf2.String()
53	fmt.Print(title2)
54}
55

License

MIT

Author

Yusuke Inuzuka