markdown

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: UNKNOWN not legal advice Imports: 0 Imported by: 0

Documentation

Overview

This file was copied from https://github.com/yuin/goldmark-highlighting

package highlighting is an 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

View Source
const MaxDepth = 6

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 Toc

type Toc struct {
	Items []*TocItem
}

func TocInspect

func TocInspect(n ast.Node, src []byte, opts TocOptions) (*Toc, error)

type TocItem

type TocItem struct {
	// Title of this item in the table of contents.
	//
	// This may be blank for items that don't refer to a heading, and only
	// have sub-items.
	Title []byte

	// ID is the identifier for the heading that this item refers to. This
	// is the fragment portion of the link without the "#".
	//
	// This may be blank if the item doesn't have an id assigned to it, or
	// if it doesn't have a title.
	//
	// Enable AutoHeadingID in your parser if you expected these to be set
	// but they weren't.
	ID []byte

	// Items references children of this item.
	//
	// For a heading at level 3, Items, contains the headings at level 4
	// under that section.
	Items []*TocItem
}

func (TocItem) Anchor

func (i TocItem) Anchor() string

type TocOptions

type TocOptions struct {
	MinDepth, MaxDepth int
}

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