package module
v2.0.0-...-37449ab Latest Latest

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 15 Imported by: 68



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.


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


package main

import (


	chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
	highlighting "github.com/yuin/goldmark-highlighting/v2"

func main() {
	mdsrc := `
		` + "```" + `
		func main() {
		` + "```" + `

	// Simple usage
	markdown := goldmark.New(
	var buf bytes.Buffer
	if err := markdown.Convert([]byte(mdsrc), &buf); err != nil {
	title := buf.String()

	// Custom configuration
	markdown2 := goldmark.New(
	var buf2 bytes.Buffer
	if err := markdown2.Convert([]byte(mdsrc), &buf2); err != nil {
	title2 := buf2.String()




Yusuke Inuzuka



package highlighting is a extension for the goldmark(http://github.com/yuin/goldmark).

This extension adds syntax-highlighting to the fenced code blocks using chroma(https://github.com/alecthomas/chroma).



This section is empty.


View Source
var Highlighting = &highlighting{
	options: []Option{},

Highlighting is a goldmark.Extender implementation.


func NewHTMLRenderer

func NewHTMLRenderer(opts ...Option) renderer.NodeRenderer

NewHTMLRenderer builds a new HTMLRenderer with given options and returns it.

func NewHighlighting

func NewHighlighting(opts ...Option) goldmark.Extender

NewHighlighting returns a new extension with given options.


type CodeBlockContext

type CodeBlockContext interface {
	// Language returns (language, true) if specified, otherwise (nil, false).
	Language() ([]byte, bool)

	// Highlighted returns true if this code block can be highlighted, otherwise false.
	Highlighted() bool

	// Attributes return attributes of the code block.
	Attributes() ImmutableAttributes

CodeBlockContext holds contextual information of code highlighting.

type CodeBlockOptions

type CodeBlockOptions func(ctx CodeBlockContext) []chromahtml.Option

CodeBlockOptions creates Chroma options per code block.

type Config

type Config struct {

	// Style is a highlighting style.
	// Supported styles are defined under https://github.com/alecthomas/chroma/tree/master/formatters.
	Style string

	// Pass in a custom Chroma style. If this is not nil, the Style string will be ignored
	CustomStyle *chroma.Style

	// If set, will try to guess language if none provided.
	// If the guessing fails, we will fall back to a text lexer.
	// Note that while Chroma's API supports language guessing, the implementation
	// is not there yet, so you will currently always get the basic text lexer.
	GuessLanguage bool

	// FormatOptions is a option related to output formats.
	// See https://github.com/alecthomas/chroma#the-html-formatter for details.
	FormatOptions []chromahtml.Option

	// CSSWriter is an io.Writer that will be used as CSS data output buffer.
	// If WithClasses() is enabled, you can get CSS data corresponds to the style.
	CSSWriter io.Writer

	// CodeBlockOptions allows set Chroma options per code block.
	CodeBlockOptions CodeBlockOptions

	// WrapperRenderer allows you to change wrapper elements.
	WrapperRenderer WrapperRenderer

Config struct holds options for the extension.

func NewConfig

func NewConfig() Config

NewConfig returns a new Config with defaults.

func (*Config) SetOption

func (c *Config) SetOption(name renderer.OptionName, value interface{})

SetOption implements renderer.SetOptioner.

type HTMLRenderer

type HTMLRenderer struct {

HTMLRenderer struct is a renderer.NodeRenderer implementation for the extension.

func (*HTMLRenderer) RegisterFuncs

func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)

RegisterFuncs implements NodeRenderer.RegisterFuncs.

type ImmutableAttributes

type ImmutableAttributes interface {
	// Get returns (value, true) if an attribute associated with given
	// name exists, otherwise (nil, false)
	Get(name []byte) (interface{}, bool)

	// GetString returns (value, true) if an attribute associated with given
	// name exists, otherwise (nil, false)
	GetString(name string) (interface{}, bool)

	// All returns all attributes.
	All() []ast.Attribute

ImmutableAttributes is a read-only interface for ast.Attributes.

type Option

type Option interface {
	// SetHighlightingOption sets given option to the extension.

Option interface is a functional option interface for the extension.

func WithCSSWriter

func WithCSSWriter(w io.Writer) Option

WithCSSWriter is a functional option that sets io.Writer for CSS data.

func WithCodeBlockOptions

func WithCodeBlockOptions(c CodeBlockOptions) Option

WithCodeBlockOptions is a functional option that sets CodeBlockOptions that allows setting Chroma options per code block.

func WithCustomStyle

func WithCustomStyle(style *chroma.Style) Option

WithStyle is a functional option that changes highlighting style.

func WithFormatOptions

func WithFormatOptions(opts ...chromahtml.Option) Option

WithFormatOptions is a functional option that wraps chroma HTML formatter options.

func WithGuessLanguage

func WithGuessLanguage(b bool) Option

WithGuessLanguage is a functional option that toggles language guessing if none provided.

func WithHTMLOptions

func WithHTMLOptions(opts ...html.Option) Option

WithHTMLOptions is functional option that wraps goldmark HTMLRenderer options.

func WithStyle

func WithStyle(style string) Option

WithStyle is a functional option that changes highlighting style.

func WithWrapperRenderer

func WithWrapperRenderer(w WrapperRenderer) Option

WithWrapperRenderer is a functional option that sets WrapperRenderer that renders wrapper elements like div, pre, etc.

type WrapperRenderer

type WrapperRenderer func(w util.BufWriter, context CodeBlockContext, entering bool)

WrapperRenderer renders wrapper elements like div, pre, etc.

Jump to

Keyboard shortcuts

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