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_v1_attribute build tag to use goldmark/v1 compatible attribute parsing.WithCSSWriter(w io.Writer) is removed. Use WithCSSMap(rc renderer.Context, m map[string]string) instead.WithFormatOptions(opts ...chromahtml.Option) is renamed to WithFormatterOptions(opts ...chromahtml.Option).WithWrapperRenderer(w WrapperRenderer) Option is removed. Use goldmark/v2 NodeRendererDecorator instead.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)
}
MIT
Yusuke Inuzuka
Go
99.8%
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_v1_attribute build tag to use goldmark/v1 compatible attribute parsing.WithCSSWriter(w io.Writer) is removed. Use WithCSSMap(rc renderer.Context, m map[string]string) instead.WithFormatOptions(opts ...chromahtml.Option) is renamed to WithFormatterOptions(opts ...chromahtml.Option).WithWrapperRenderer(w WrapperRenderer) Option is removed. Use goldmark/v2 NodeRendererDecorator instead.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)
}
MIT
Yusuke Inuzuka
Go
99.8%