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.
- goldmark-highlighting/v3 uses goldmark/v2, chroma/v3, json/v2
- minimum Go version: 1.27
- you can build goldmark-highlighting/v3 with
goldmark_v1_attributebuild tag to use goldmark/v1 compatible attribute parsing.
- For goldmark/v1, please use goldmark-highlighting/v2.
WithCSSWriter(w io.Writer)is removed. UseWithCSSMap(rc renderer.Context, m map[string]string)instead.WithFormatOptions(opts ...chromahtml.Option)is renamed toWithFormatterOptions(opts ...chromahtml.Option).WithWrapperRenderer(w WrapperRenderer) Optionis removed. Use goldmark/v2NodeRendererDecoratorinstead.
go get github.com/yuin/goldmark-highlighting/v3
package main
import (
"bytes"
"fmt"
"github.com/yuin/goldmark/v2"
chromahtml "github.com/alecthomas/chroma/v3/formatters/html"
highlighting "github.com/yuin/goldmark-highlighting/v3"
)
func main() {
mdsrc := `
Title
=======
` + "```go {hl_lines=2}" + `
func main() {
fmt.Println("ok")
}
` + "```" + `
`
// Simple usage
p := parser.New(parser.WithExtensions(highlighting.Parser))
r := html.New(html.WithExtensions(highlighting.HTMLRenderer))
node := p.Parse([]byte(mdsrc))
var buf bytes.Buffer
if err := r.Render([]byte(mdsrc), &buf, node); err != nil {
panic(err)
}
title := buf.String()
fmt.Print(title)
// Custom configuration
r := html.New(html.WithExtensions(
highlighting.NewHTMLRenderer(
highlighting.WithStyle("monokai"),
highlighting.WithFormatterOptions(
chromahtml.WithClasses(true),
chromahtml.WithLineNumbers(false),
),
highlighting.WithCodeBlockFormatterOptions(
func(n *ast.CodeBlock, source []byte, _ renderer.Context) []chromahtml.Option {
if language, ok := n.Language(source); ok && language == "go" {
return []chromahtml.Option{
chromahtml.WithLineNumbers(true),
}
}
return nil
}),
),
),
)
var buf2 bytes.Buffer
if err := r.Render([]byte(mdsrc), &buf2, node); err != nil {
panic(err)
}
title2 := buf2.String()
fmt.Print(title2)
}This extension is implemented using NodeRendererDecorator. You can skip this extension and use other code block extensions by using the WithExcludeLanguages or WithExcludeFunc options.
r := html.New(html.WithExtensions(highlighting.NewHTMLRenderer(
highlighting.WithExcludeLanguages("mermaid"), // exclude mermaid code blocks( to render mermaid code as diagrams)
)))MIT
Yusuke Inuzuka