renderer

package
v2.0.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package renderer renders the given AST to certain formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config[W any, C any] struct {
	// contains filtered or unexported fields
}

A Config struct holds configuration for Renderer.

type Context

type Context interface {
	// Get returns a value associated with the given key.
	Get(ContextKey) any

	// ComputeIfAbsent computes a value if a value associated with the given key is absent and returns the value.
	ComputeIfAbsent(ContextKey, func() any) any

	// Set sets the given value to the context.
	Set(ContextKey, any)

	// Render renders the given node using the renderer associated with this context.
	// If no rendering function has been set, it is a no-op and returns nil.
	Render(w any, source []byte, n ast.Node) error
}

A Context interface holds information that is necessary to render Markdown text.

func NewContext

func NewContext(opts ...ContextOption) Context

NewContext returns a new rendering Context.

type ContextKey

type ContextKey int

ContextKey is a key that is used to set arbitrary values to the rendering context.

var ContextKeyMax ContextKey

ContextKeyMax is a maximum value of the ContextKey.

func NewContextKey

func NewContextKey() ContextKey

NewContextKey returns a new ContextKey value.

type ContextOption

type ContextOption func(*renderContext)

ContextOption is a functional option for NewContext.

func WithRenderFunc

func WithRenderFunc(f func(any, []byte, ast.Node, Context) error) ContextOption

WithRenderFunc sets the rendering function used by Context.Render.

type EmptyHook

type EmptyHook[W any] struct{}

EmptyHook is a Hook that does nothing.

func (*EmptyHook[W]) PostRender

func (h *EmptyHook[W]) PostRender(_ W, _ []byte, _ ast.Node, _ Context) error

PostRender implements Hook.PostRender.

func (*EmptyHook[W]) PreRender

func (h *EmptyHook[W]) PreRender(_ W, _ []byte, _ ast.Node, _ Context) error

PreRender implements Hook.PreRender.

type Extension

type Extension[C any] interface {
	// RendererOptions returns the options for the Renderer.
	RendererOptions(*C) []Option[C]
}

Extension is an interface that represents an extension for Renderer.

type Helper

type Helper[W any, C any] struct {
	// contains filtered or unexported fields
}

Helper is a helper struct for implementing Renderer.

func NewHelper

func NewHelper[W any, C any](opts ...Option[C]) *Helper[W, C]

NewHelper returns a new RendererHelper with the given RendererSpec.

func (*Helper[W, C]) Config

func (r *Helper[W, C]) Config() *C

Config returns the configuration of this Helper.

func (*Helper[W, C]) Register

func (r *Helper[W, C]) Register(kind ast.NodeKind, n NodeRenderer[W])

Register registers the given NodeRenderer for the given node kind.

func (*Helper[W, C]) Render

func (r *Helper[W, C]) Render(w W, source []byte, n ast.Node) error

Render renders the given AST node to the given writer with the given Renderer.

func (*Helper[W, C]) RenderStringSource

func (r *Helper[W, C]) RenderStringSource(w W, source string, n ast.Node) error

RenderStringSource renders the given AST node to the given writer with the given Renderer and string source.

type Hook

type Hook[W any] interface {
	PreRender(w W, source []byte, n ast.Node, rc Context) error

	PostRender(w W, source []byte, n ast.Node, rc Context) error
}

A Hook interface is used for hooking into the rendering process.

type NodeRenderer

type NodeRenderer[W any] interface {
	Render(w W, source []byte, n ast.Node, entering bool, rc Context) (ast.WalkStatus, error)
}

A NodeRenderer interface is used for rendering a given node.

func NodeRendererFunc

func NodeRendererFunc[W any](f func(w W, source []byte,
	n ast.Node, entering bool, rc Context) (ast.WalkStatus, error)) NodeRenderer[W]

NodeRendererFunc is a function that implements NodeRenderer interface.

type Option

type Option[C any] interface {
	SetFormatOption(*C)
}

Option is a functional option for NewRenderer.

func NewOptionFunc

func NewOptionFunc[C any](f func(*C)) Option[C]

NewOptionFunc returns a new Option that applies the given function to the configuration.

func WithExtensions

func WithExtensions[W any, C any](ext ...Extension[C]) Option[C]

WithExtensions sets the extensions for the Renderer.

func WithHooks

func WithHooks[W any, C any](hooks ...Hook[W]) Option[C]

WithHooks sets the hooks for the Renderer.

func WithNodeRenderer

func WithNodeRenderer[W any, C any](kind ast.NodeKind, nodeRenderer NodeRenderer[W]) Option[C]

WithNodeRenderer sets a node renderer for the given node kind.

func WithNodeRenderers

func WithNodeRenderers[W any, C any](nodeRenderers map[ast.NodeKind]NodeRenderer[W]) Option[C]

WithNodeRenderers sets the node renderers for the Renderer.

type Renderer

type Renderer[W any] interface {
	Render(w W, source []byte, n ast.Node) error
	RenderStringSource(w W, source string, n ast.Node) error
}

A Renderer interface is used for rendering a given AST node to a certain format.

Directories

Path Synopsis
Package html implements renderer that outputs HTMLs.
Package html implements renderer that outputs HTMLs.

Jump to

Keyboard shortcuts

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