converter

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	EscapeModeDisabled escapeMode = "disabled"
	EscapeModeSmart    escapeMode = "smart"
)
View Source
const (
	// PriorityEarly means that the handler will be run **early** in the process.
	// To run it even earlier you need to subtract from this number.
	PriorityEarly = 100

	// PriorityStandard is for handlers that don't need to be run in a particular order.
	PriorityStandard = 500

	// PriorityLate means that the handler will be run **late** in the process.
	// To run it even later you need to add to this number.
	PriorityLate = 1000
)
View Source
const (
	TagTypeBlock  tagType = "block"
	TagTypeInline tagType = "inline"

	// TagTypeRemove will remove that node in the _PreRender_ phase with a high priority.
	TagTypeRemove tagType = "remove"
)

Variables

This section is empty.

Functions

func GetDomain

func GetDomain(ctx context.Context) string

func GetState

func GetState[V any](ctx context.Context, key string) V

func ParseAndEncodeQuery

func ParseAndEncodeQuery(rawQuery string) string

func SetState

func SetState[V any](ctx context.Context, key string, val V)

func UpdateState

func UpdateState[V any](ctx context.Context, key string, fn func(V) V)

func WithEscapeMode

func WithEscapeMode(mode escapeMode) converterOption

WithEscapeMode changes the strictness of the "escaping".

Some characters have a special meaning in markdown. For example, the character "*" can be used for lists, emphasis and dividers. By placing a backlash before that character (e.g. "\*") you can "escape" it. Then the character will render as a raw "*" without the "markdown meaning" applied.

Learn more in the documentation

"disabled" or "smart"

default: "smart"

func WithPlugins

func WithPlugins(plugins ...Plugin) converterOption

WithPlugins can be used to add additional functionality to the converter.

Types

type AssembleAbsoluteURLFunc

type AssembleAbsoluteURLFunc func(tagName string, rawURL string, domain string) string

type Context

type Context interface {
	context.Context

	AssembleAbsoluteURL(ctx Context, tagName string, rawURL string) string

	GetTagType(tagName string) (tagType, bool)

	RenderNodes(ctx Context, w Writer, nodes ...*html.Node)
	RenderChildNodes(ctx Context, w Writer, n *html.Node)

	EscapeContent(content []byte) []byte
	UnEscapeContent(content []byte) []byte

	WithValue(key any, val any) Context
}

Context extends the normal context.Context with some additional methods useful for the process of converting.

type ConvertOptionFunc added in v2.2.0

type ConvertOptionFunc func(o *convertOption)

func WithContext

func WithContext(ctx context.Context) ConvertOptionFunc

func WithDomain

func WithDomain(domain string) ConvertOptionFunc

WithDomain provides a base `domain` to the converter and to the `AssembleAbsoluteURL` function.

If a *relative* url is encountered (in an image or link) then the `domain` is used to convert it to a *absolute* url.

type Converter

type Converter struct {
	Register register
	// contains filtered or unexported fields
}

func NewConverter

func NewConverter(opts ...converterOption) *Converter

func (*Converter) ConvertNode

func (conv *Converter) ConvertNode(doc *html.Node, opts ...ConvertOptionFunc) ([]byte, error)

ConvertNode converts a `*html.Node` to a markdown byte slice.

If you have already parsed an HTML page using the `html.Parse()` function from the "golang.org/x/net/html" package then you can pass this node directly to the converter.

func (*Converter) ConvertReader

func (conv *Converter) ConvertReader(r io.Reader, opts ...ConvertOptionFunc) ([]byte, error)

ConvertReader converts the html from the reader to markdown.

Under the hood `html.Parse()` is used to parse the HTML.

func (*Converter) ConvertString

func (conv *Converter) ConvertString(htmlInput string, opts ...ConvertOptionFunc) (string, error)

ConvertString converts a html-string to a markdown-string.

Under the hood `html.Parse()` is used to parse the HTML.

type GetStateFunc

type GetStateFunc func(key string) any

type HandlePostRenderFunc

type HandlePostRenderFunc func(ctx Context, content []byte) []byte

type HandlePreRenderFunc

type HandlePreRenderFunc func(ctx Context, doc *html.Node)

type HandleRenderFunc

type HandleRenderFunc func(ctx Context, w Writer, n *html.Node) RenderStatus

type HandleTextTransformFunc

type HandleTextTransformFunc func(ctx Context, content string) string

type HandleUnEscapeFunc

type HandleUnEscapeFunc func(chars []byte, index int) int

type Plugin

type Plugin interface {
	// The public name of the plugin, e.g. "strikethrough"
	Name() string

	// Init is called to initialize the plugin. It can be used to
	// *validate* the arguments and *register* the rules.
	Init(conv *Converter) error
}

Plugin can be used to extends functionality beyond what is offered by commonmark.

type RenderStatus

type RenderStatus int
const (
	RenderTryNext RenderStatus = iota
	RenderSuccess
)

type SetStateFunc

type SetStateFunc func(key string, val any)

type UpdateStateFunc

type UpdateStateFunc func(key string, fn func(any) any)

type Writer

type Writer interface {
	Write(p []byte) (n int, err error)
	WriteByte(c byte) error
	WriteRune(r rune) (n int, err error)
	WriteString(s string) (n int, err error)
}

Writer is an interface that only conforms to the Write* methods of bytes.Buffer

Jump to

Keyboard shortcuts

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