rawhtml

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2024 License: GPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package rawhtml implements improvements on top of Goldmark's native inline HTML support.

For context, Goldmark's inline HTML parser breaks apart inline HTML elements so that opening and closing tags are treated as separate nodes:

foo <a href="bar">bar</a> baz

Becomes the following nodes:

Text{"foo "}
RawHTML{"<a href="bar">"}
Text{"bar"}
RawHTML{"</a>"}
Text{" baz"}

This makes it difficult to parse and manipulate inline HTML elements.

To help work around this, rawhtml augments the parser context with information about open-close tags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseHTMLFragmentBodies

func ParseHTMLFragmentBodies(r io.Reader) ([]*html.Node, error)

ParseHTMLFragmentBodies parses fragments of HTML from the given reader. It returns the top-level nodes of the HTML fragments -- unwrapping the <html><head></head> tags, yielding the <body>s. That is, the direct children of each returned node hold the contents of the <body> tag.

Types

type Extender

type Extender struct{}

Extender installs the rawhtml extension into a Goldmark Markdown.

func (*Extender) Extend

func (e *Extender) Extend(md goldmark.Markdown)

Extend extends the given Goldmark Markdown with the rawhtml extension.

This implements the goldmark.Extender interface.

type Pair

type Pair struct {
	Open, Close *ast.RawHTML
}

Pair is a pair of opening and closing raw HTML nodes.

func PairFromHTML

func PairFromHTML(n *html.Node, src []byte) (*Pair, []byte, error)

PairFromHTML formats the given HTML node as a Pair. The node must be an HTML tag with separate open and close tags, and must not have any children. Text needed to render the node is appended to src. Returns an error if the HTML node does not have separate opening and closing tags.

func (*Pair) ParseHTML

func (p *Pair) ParseHTML(src []byte) (*html.Node, error)

ParseHTML parses the underlying HTML node for this pair.

type Pairs

type Pairs []Pair

Pairs is a collection of HTML [Pair]s.

func GetPairs

func GetPairs(ctx parser.Context) Pairs

GetPairs returns the Pairs stored in the given parser.Context. If no Pairs are stored, it returns an empty Pairs.

type Transformer

type Transformer struct{}

Transformer searches for raw HTML nodes in the Goldmark document and adds information about pairs to the parser context.

It uses a fairly simple algorithm: For every open tag, scan until the matching close tag.

func (*Transformer) Transform

func (t *Transformer) Transform(doc *ast.Document, reader text.Reader, pc parser.Context)

Transform transforms a Markdown document.

Jump to

Keyboard shortcuts

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