highlighting

package
v0.0.0-...-0941746 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: AGPL-3.0, MIT Imports: 15 Imported by: 0

README

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

import (
    "bytes"
    "fmt"
    "github.com/alecthomas/chroma/formatters/html"
    "github.com/yuin/goldmark"
    "github.com/yuin/goldmark/extension"
    "github.com/yuin/goldmark/parser"
    "github.com/yuin/goldmark-highlighting/v2"

)

func main() {
    markdown := goldmark.New(
        goldmark.WithExtensions(
            highlighting.Highlighting,
        ),
    )
    var buf bytes.Buffer
    if err := markdown.Convert([]byte(source), &buf); err != nil {
        panic(err)
    }
    fmt.Print(title)
}
    markdown := goldmark.New(
        goldmark.WithExtensions(
            highlighting.NewHighlighting(
               highlighting.WithStyle("monokai"),
               highlighting.WithFormatOptions(
                   html.WithLineNumbers(),
               ),
            ),
        ),
    )

License

MIT

Author

Yusuke Inuzuka

Documentation

Overview

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).

Index

Constants

This section is empty.

Variables

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

Highlighting is a goldmark.Extender implementation.

Functions

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.

Types

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 {
	html.Config

	// 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 {
	Config
}

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 {
	renderer.Option
	// SetHighlightingOption sets given option to the extension.
	SetHighlightingOption(*Config)
}

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