parser

package
v0.0.0-...-94fc73f Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: BSD-2-Clause Imports: 13 Imported by: 689

Documentation

Overview

Package parser implements a parser for markdown text that generates an AST (abstract syntax tree).

Index

Constants

This section is empty.

Variables

View Source
var EscapeChars = []byte("\\`*_{}[]()#+-.!:|&<>~^$")

'\\' backslash escape

View Source
var Paths = [][]byte{
	[]byte("/"),
	[]byte("./"),
	[]byte("../"),
}
View Source
var URIs = [][]byte{
	[]byte("http://"),
	[]byte("https://"),
	[]byte("ftp://"),
	[]byte("mailto:"),
}

Functions

func IsAlnum

func IsAlnum(c byte) bool

IsAlnum returns true if c is an ASCII digit or letter.

func IsCallout

func IsCallout(data []byte) (id []byte, consumed int)

IsCallout detects a callout in the following format: <<N>> Where N is a integer > 0.

func IsEmpty

func IsEmpty(data []byte) int

IsEmpty returns the length of a blank line at the start of data, or 0 if the line is not blank. It is okay to call on an empty buffer.

func IsLetter

func IsLetter(c byte) bool

IsLetter returns true if c is ascii letter

func IsPunctuation

func IsPunctuation(c byte) bool

IsPunctuation returns true if c is a punctuation symbol.

func IsPunctuation2

func IsPunctuation2(d []byte) bool

func IsSafeURL

func IsSafeURL(url []byte) bool

IsSafeURL returns true if url starts with one of the valid schemes or is a relative path.

func IsSpace

func IsSpace(c byte) bool

IsSpace returns true if c is a white-space character

func LinesUntilEmpty

func LinesUntilEmpty(data []byte) int

LinesUntilEmpty scans lines up to the first empty line.

func NormalizeNewlines

func NormalizeNewlines(d []byte) []byte

Types

type BlockFunc

type BlockFunc func(data []byte) (ast.Node, []byte, int)

BlockFunc allows registration of a parser function. If successful it returns an ast.Node, a buffer that should be parsed as a block, and the number of bytes consumed.

type Extensions

type Extensions int

Extensions is a bitmask of enabled parser extensions.

const (
	NoExtensions           Extensions = 0
	NoIntraEmphasis        Extensions = 1 << iota // Ignore emphasis markers inside words
	Tables                                        // Parse tables
	FencedCode                                    // Parse fenced code blocks
	Autolink                                      // Detect embedded URLs that are not explicitly marked
	Strikethrough                                 // Strikethrough text using ~~test~~
	LaxHTMLBlocks                                 // Loosen up HTML block parsing rules
	SpaceHeadings                                 // Be strict about prefix heading rules
	HardLineBreak                                 // Translate newlines into line breaks
	NonBlockingSpace                              // Translate backslash-space into a non-breaking space
	TabSizeEight                                  // Expand tabs to eight spaces instead of four
	Footnotes                                     // Pandoc-style footnotes
	NoEmptyLineBeforeBlock                        // No need to insert an empty line to start a (code, quote, ordered list, unordered list) block
	HeadingIDs                                    // Specify heading IDs with {#id}
	Titleblock                                    // Titleblock ala pandoc
	AutoHeadingIDs                                // Create the heading ID from the text
	BackslashLineBreak                            // Translate trailing backslashes into line breaks
	DefinitionLists                               // Parse definition lists
	MathJax                                       // Parse MathJax
	OrderedListStart                              // Keep track of the first number used when starting an ordered list.
	Attributes                                    // Block Attributes
	SuperSubscript                                // Super- and subscript support: 2^10^, H~2~O.
	EmptyLinesBreakList                           // 2 empty lines break out of list
	Includes                                      // Support including other files.
	Mmark                                         // Support Mmark syntax, see https://mmark.miek.nl/post/syntax/
	InlineAttributes                              // Parse {: key="value"} after links and images
	MarkdownInHTML                                // Parse markdown inside HTML table cells and similar tags

	CommonExtensions Extensions = NoIntraEmphasis | Tables | FencedCode |
		Autolink | Strikethrough | SpaceHeadings | HeadingIDs |
		BackslashLineBreak | DefinitionLists | MathJax
)

Bit flags representing markdown parsing extensions. Use | (or) to specify multiple extensions.

type Flags

type Flags int

Flags control optional behavior of parser.

const (
	FlagsNone        Flags = 0
	SkipFootnoteList Flags = 1 << iota // Skip adding the footnote list (regardless of whether they are parsed)
	// CommonMark parses closer to the CommonMark spec.
	// A blank line ends a block quote, so
	//
	//	> a
	//
	//	> b
	//
	// is two quotes. The flag is off by default. It was added so existing
	// documents keep the original behavior: those lines are one quote with
	// two paragraphs. Put '>' on the blank line to keep one quote when the
	// flag is on.
	CommonMark
)

Parser configuration options.

type InlineParser

type InlineParser func(p *Parser, data []byte, offset int) (int, ast.Node)

InlineParser parses inline data beginning at offset.

type Options

type Options struct {
	ParserHook    BlockFunc
	ReadIncludeFn ReadIncludeFunc

	Flags Flags // Flags allow customizing parser's behavior
}

Options is a collection of supplementary parameters tweaking the behavior of various parts of the parser.

type Parser

type Parser struct {

	// ReferenceOverride is an optional function callback that is called every
	// time a reference is resolved. It can be set before starting parsing.
	//
	// In Markdown, the link reference syntax can be made to resolve a link to
	// a reference instead of an inline URL, in one of the following ways:
	//
	//  * [link text][refid]
	//  * [refid][]
	//
	// Usually, the refid is defined at the bottom of the Markdown document. If
	// this override function is provided, the refid is passed to the override
	// function first, before consulting the defined refids at the bottom. If
	// the override function indicates an override did not occur, the refids at
	// the bottom will be used to fill in the link details.
	ReferenceOverride ReferenceOverrideFunc

	// IsSafeURLOverride allows overriding the default URL matcher. URL is
	// safe if the overriding function returns true. Can be used to extend
	// the default list of safe URLs.
	IsSafeURLOverride func(url []byte) bool

	Opts Options

	// after parsing, this is AST root of parsed markdown text
	Doc ast.Node

	InsideLink bool
	// contains filtered or unexported fields
}

Parser is a type that holds extensions and the runtime state used by Parse. You cannot use it directly; construct it with New.

func New

func New() *Parser

New creates a markdown parser with CommonExtensions.

func NewWithExtensions

func NewWithExtensions(extension Extensions) *Parser

NewWithExtensions creates a markdown parser with the given extensions.

func (*Parser) AddBlock

func (p *Parser) AddBlock(n ast.Node) ast.Node

func (*Parser) Block

func (p *Parser) Block(data []byte)

func (*Parser) Finalize

func (p *Parser) Finalize(block ast.Node)

func (*Parser) Inline

func (p *Parser) Inline(currBlock ast.Node, data []byte)

Inline parses text within a block. Each function returns the number of consumed chars.

func (*Parser) Parse

func (p *Parser) Parse(input []byte) ast.Node

Parse generates AST (abstract syntax tree) representing markdown document.

The result is a root of the tree whose underlying type is *ast.Document

You can then convert AST to html using html.Renderer, to some other format using a custom renderer or transform the tree.

Parser is not reusable. Create a new Parser for each Parse() call.

func (*Parser) RegisterInline

func (p *Parser) RegisterInline(trigger byte, fn InlineParser) InlineParser

RegisterInline installs fn for a trigger byte and returns the previous parser.

type ReadIncludeFunc

type ReadIncludeFunc func(from, path string, address []byte) []byte

ReadIncludeFunc should read the file under path and return the read bytes, from will be set to the name of the current file being parsed. Initially this will be empty. address is the optional address specifier of which lines of the file to return. If this function is not set no data will be read.

type Reference

type Reference struct {
	// Link is usually the URL the reference points to.
	Link string
	// Title is the alternate text describing the link in more detail.
	Title string
	// Text is the optional text to override the ref with if the syntax used was
	// [refid][]
	Text string
}

Reference represents the details of a link. See the documentation in Options for more details on use-case.

type ReferenceOverrideFunc

type ReferenceOverrideFunc func(reference string) (ref *Reference, overridden bool)

ReferenceOverrideFunc resolves a reference, or declines by returning overridden false so the parser can use its document-defined references.

Jump to

Keyboard shortcuts

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