converter

package
v2.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// StrategyRemoveNode will remove that node in the _PreRender_ phase
	// with a high priority.
	StrategyRemoveNode tagStrategy = "StrategyRemoveNode"

	// StrategyMarkdownLeaf will keep the children of this node as markdown.
	//
	// This is the default for unknown nodes — where there
	// is no registered _Render_ handler.
	StrategyMarkdownLeaf tagStrategy = "StrategyMarkdownLeaf"

	// StrategyMarkdownBlock will keep the children of this node as markdown
	// AND will render newlines.
	//
	// This is the default for html nodes that have
	// a) no registered _Render_ handler AND
	// b) where `dom.NameIsBlockNode()` returns true.
	StrategyMarkdownBlock tagStrategy = "StrategyMarkdownBlock"

	// StrategyHTMLBlock will render the node as HTML using `html.Render()`
	StrategyHTMLBlock tagStrategy = "StrategyHTMLBlock"

	// StrategyHTMLBlockWithMarkdown will render the node as HTML
	// and render the children as markdown.
	StrategyHTMLBlockWithMarkdown tagStrategy = "StrategyHTMLBlockWithMarkdown"
)
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
)

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 WithContext

func WithContext(ctx context.Context) convertOptionFunc

func WithDomain

func WithDomain(domain string) convertOptionFunc

func WithPlugins

func WithPlugins(plugins ...Plugin) converterOption

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

Types

type AssembleAbsoluteURLFunc

type AssembleAbsoluteURLFunc func(elem Element, rawURL string, domain string) string

type Context

type Context interface {
	context.Context

	AssembleAbsoluteURL(ctx Context, elem Element, rawURL string) string

	GetTagStrategy(tagName string) (tagStrategy, bool)

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

	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 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 Element

type Element string
const (
	ElementLink  Element = "ElementLink"
	ElementImage Element = "ElementImage"
)

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 {
	// 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