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 ¶
- Variables
- func NewHTMLRenderer(opts ...Option) renderer.NodeRenderer
- func NewHighlighting(opts ...Option) goldmark.Extender
- type CodeBlockContext
- type CodeBlockOptions
- type Config
- type HTMLRenderer
- type ImmutableAttributes
- type Option
- func WithCSSWriter(w io.Writer) Option
- func WithCodeBlockOptions(c CodeBlockOptions) Option
- func WithCustomStyle(style *chroma.Style) Option
- func WithFormatOptions(opts ...chromahtml.Option) Option
- func WithGuessLanguage(b bool) Option
- func WithHTMLOptions(opts ...html.Option) Option
- func WithStyle(style string) Option
- func WithWrapperRenderer(w WrapperRenderer) Option
- type WrapperRenderer
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 (*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 ¶
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 ¶
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 ¶
WithGuessLanguage is a functional option that toggles language guessing if none provided.
func WithHTMLOptions ¶
WithHTMLOptions is functional option that wraps goldmark HTMLRenderer options.
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.