html

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: 9 Imported by: 0

Documentation

Overview

Package html implements renderer that outputs HTMLs.

Index

Constants

This section is empty.

Variables

View Source
var BlockquoteAttributeFilter = GlobalAttributeFilter.ExtendString(`cite`)

BlockquoteAttributeFilter defines attribute names which blockquote elements can have.

View Source
var CodeAttributeFilter = GlobalAttributeFilter

CodeAttributeFilter defines attribute names which code elements can have.

View Source
var DefaultWriter = NewWriter()

DefaultWriter is a default instance of the Writer.

View Source
var EmphasisAttributeFilter = GlobalAttributeFilter

EmphasisAttributeFilter defines attribute names which emphasis elements can have.

View Source
var GlobalAttributeFilter = util.NewBytesFilterString(`accesskey,autocapitalize,autofocus,class,contenteditable,dir,draggable,enterkeyhint,hidden,id,inert,inputmode,is,itemid,itemprop,itemref,itemscope,itemtype,lang,part,role,slot,spellcheck,style,tabindex,title,translate`) // nolint:lll

GlobalAttributeFilter defines attribute names which any elements can have.

View Source
var HeadingAttributeFilter = GlobalAttributeFilter

HeadingAttributeFilter defines attribute names which heading elements can have.

View Source
var ImageAttributeFilter = GlobalAttributeFilter.ExtendString(`align,border,crossorigin,decoding,height,importance,intrinsicsize,ismap,loading,referrerpolicy,sizes,srcset,usemap,width`) // nolint: lll

ImageAttributeFilter defines attribute names which image elements can have.

View Source
var LinkAttributeFilter = GlobalAttributeFilter.ExtendString(`download,href,lang,media,ping,referrerpolicy,rel,shape,target`) // nolint:lll

LinkAttributeFilter defines attribute names which link elements can have.

View Source
var ListAttributeFilter = GlobalAttributeFilter.ExtendString(`start,reversed,type`)

ListAttributeFilter defines attribute names which list elements can have.

View Source
var ListItemAttributeFilter = GlobalAttributeFilter.ExtendString(`value`)

ListItemAttributeFilter defines attribute names which list item elements can have.

View Source
var ParagraphAttributeFilter = GlobalAttributeFilter

ParagraphAttributeFilter defines attribute names which paragraph elements can have.

View Source
var StrongAttributeFilter = GlobalAttributeFilter

StrongAttributeFilter defines attribute names which strong elements can have.

View Source
var ThematicAttributeFilter = GlobalAttributeFilter.ExtendString(`align,color,noshade,size,width`)

ThematicAttributeFilter defines attribute names which hr elements can have.

Functions

func IsDangerousURL

func IsDangerousURL(url []byte) bool

IsDangerousURL returns true if the given url seems a potentially dangerous url, otherwise false.

func IsInTightBlock

func IsInTightBlock(n ast.Node) bool

IsInTightBlock returns true if the paragraph is a direct child of a ListItem whose parent List is tight.

func RenderAttributes

func RenderAttributes(writer io.Writer, node ast.Node, filter util.BytesFilter)

RenderAttributes renders given node's attributes. You can specify attribute names to render by the filter. If filter is nil, RenderAttributes renders all attributes.

func WithEscapedSpace

func WithEscapedSpace() interface {
	WriterOption
	Option
}

WithEscapedSpace is a WriterOption indicates that a '\' escaped half-space(0x20) should not be rendered.

Types

type Config

type Config struct {
	renderer.Config[io.Writer, Config]
	EscapedSpace        bool
	HardWraps           bool
	EastAsianLineBreaks EastAsianLineBreaks
	XHTML               bool
	Unsafe              bool
	Paragraph           ParagraphConfig
	// contains filtered or unexported fields
}

A Config struct has configurations for the HTML based renderers.

func (Config) Default

func (c Config) Default() Config

Default returns a Config with default values.

func (*Config) Writer

func (c *Config) Writer() Writer

Writer returns a Writer.

This method is not thread-safe.

type EastAsianLineBreaks

type EastAsianLineBreaks int

A EastAsianLineBreaks is a style of east asian line breaks.

const (
	//EastAsianLineBreaksNone renders line breaks as it is.
	EastAsianLineBreaksNone EastAsianLineBreaks = iota

	// EastAsianLineBreaksSimple follows east_asian_line_breaks in Pandoc.
	EastAsianLineBreaksSimple
	// EastAsianLineBreaksCSS3Draft follows CSS text level3 "Segment Break Transformation Rules" with some enhancements.
	EastAsianLineBreaksCSS3Draft
)

type EmptyHook

type EmptyHook = renderer.EmptyHook[io.Writer]

EmptyHook is a Hook that does nothing.

type Extension

type Extension = renderer.Extension[Config]

Extension is an extension that extends HTML based renderers.

func NewCommonMark

func NewCommonMark(opts ...Option) Extension

NewCommonMark returns a new Extension that renders CommonMark compliant HTML.

type Hook

type Hook = renderer.Hook[io.Writer]

Hook is a hook that hooks before rendering a node.

type IsInTightBlockFunc

type IsInTightBlockFunc func(n ast.Node) bool

IsInTightBlockFunc determines whether a paragraph node is inside a tight block and should be rendered without <p> tags.

type NodeRenderer

type NodeRenderer = renderer.NodeRenderer[io.Writer]

NodeRenderer is a renderer that renders AST nodes as (X)HTML.

func NodeRendererFunc

func NodeRendererFunc(f func(w io.Writer, source []byte,
	n ast.Node, entering bool, rctx renderer.Context) (ast.WalkStatus, error)) NodeRenderer

NodeRendererFunc is a function that renders AST nodes as (X)HTML.

type Option

type Option = renderer.Option[Config]

Option is a functional option for HTML based renderers.

func WithEastAsianLineBreaks

func WithEastAsianLineBreaks(e EastAsianLineBreaks) Option

WithEastAsianLineBreaks is a functional option that indicates whether softline breaks between east asian wide characters should be ignored.

func WithExtensions

func WithExtensions(extensions ...Extension) Option

WithExtensions adds extensions.

func WithHardWraps

func WithHardWraps() Option

WithHardWraps is a functional option that indicates whether softline breaks should be rendered as '<br>'.

func WithHooks

func WithHooks(hooks ...Hook) Option

WithHooks adds render hooks.

func WithIsInTightBlock

func WithIsInTightBlock(fn IsInTightBlockFunc) Option

WithIsInTightBlock is a functional option that sets a custom function to determine whether a paragraph node is inside a tight block and should be rendered without <p> tags.

func WithNodeRenderer

func WithNodeRenderer(kind ast.NodeKind, nodeRenderer NodeRenderer) Option

WithNodeRenderer sets a node renderer for the given node kind.

func WithNodeRenderers

func WithNodeRenderers(nodeRenderers map[ast.NodeKind]NodeRenderer) Option

WithNodeRenderers sets a node renderer for the given node kind.

func WithUnsafe

func WithUnsafe() Option

WithUnsafe is a functional option that renders dangerous contents (raw htmls and potentially dangerous links) as it is.

func WithXHTML

func WithXHTML() Option

WithXHTML is a functional option indicates that nodes should be rendered in xhtml instead of HTML5.

type ParagraphConfig

type ParagraphConfig struct {
	// IsInTightBlockFunc determines whether a paragraph should be rendered
	// without <p> tags because it is inside a tight block.
	IsInTightBlockFunc IsInTightBlockFunc
}

A ParagraphConfig struct has configurations for paragraph rendering.

type Renderer

type Renderer = renderer.Renderer[io.Writer]

Renderer is a renderer that renders AST nodes as (X)HTML.

func New

func New(opts ...Option) Renderer

New returns a new Renderer with given options.

type Writer

type Writer interface {
	// WriteText writes the given source to writer with resolving references and unescaping
	// backslash escaped characters.
	WriteText(writer io.Writer, source []byte)

	// RawWriteText writes the given source to writer without resolving references and
	// unescaping backslash escaped characters.
	RawWriteText(writer io.Writer, source []byte)

	// WriteHTML writes the given html.
	// WriteHTML replaces null bytes with the replacement character and writes the result to writer.
	WriteHTML(writer io.Writer, html []byte)
}

A Writer interface writes textual contents to a writer.

func NewWriter

func NewWriter(opts ...WriterOption) Writer

NewWriter returns a new Writer.

type WriterOption

type WriterOption interface {
	SetWriterOption(*writerConfig)
}

A WriterOption interface sets options for HTML based writers.

Jump to

Keyboard shortcuts

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