parser

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Entity

func Entity(s string) (n int, text string)

Entity reads the entity or numeric character reference s starts with, as & or #, returning its length and the text it stands for, or 0 when s starts with none that Markdown reads.

func NormalizeLabel

func NormalizeLabel(s string) string

NormalizeLabel normalizes a label as CommonMark matches link references and footnotes: whitespace collapsed and case folded.

func PlainText

func PlainText(in []Inline) string

PlainText returns the text of inlines without their markup: the text of links and emphasis, code as its text, line breaks as spaces.

Types

type Align

type Align uint8

Align is how a table's column aligns its cells.

const (
	AlignNone Align = iota
	AlignLeft
	AlignCenter
	AlignRight
)

type Block

type Block struct {
	Kind Kind
	// Parent is the index of the block holding it, -1 for the root.
	Parent int
	// Next is the index after the block and its descendants.
	Next int
	// Start and End are the offsets of the block's bytes in the source,
	// from its first character to the end of its last line.
	Start, End int
	// Open reports that the block is still open at the end of the
	// source, so that more text can continue it: a fence without its
	// closing fence, or the paragraph being written.
	Open bool

	// Level is a heading's, from 1 to 6.
	Level int
	// Ordered, Number (an ordered list's first), Marker (*, - or + for
	// a bullet list, . or ) after an ordered list's numbers) and Tight
	// (no blank lines between its items and their blocks) describe a
	// list.
	Ordered bool
	Number  int
	Marker  byte
	Tight   bool
	// Task is an item's check box.
	Task Task
	// Fenced tells a fenced code block from an indented one.
	Fenced bool
	// Header tells a table's header row.
	Header bool
	// Align is the alignment of a table's columns.
	Align []Align
	// contains filtered or unexported fields
}

Block is a block of a document: blocks are in document order, a block's children right after it, up to its Next.

func (*Block) Healed

func (b *Block) Healed() bool

Healed reports whether the block's inlines were completed as the tail of a streaming document: their closers are not in the source, and change as it grows.

func (*Block) Info

func (b *Block) Info() string

Info returns a fenced code block's info string, unescaped, its language first (see Language).

func (*Block) Label

func (b *Block) Label() string

Label returns a footnote definition's label.

func (*Block) Language

func (b *Block) Language() string

Language returns the language of a fenced code block, the first word of its info string.

func (*Block) Revision

func (b *Block) Revision() uint32

Revision changes when the block's inlines are parsed anew, as when a link reference defined later in the document resolves its links: views keep what they made of a block while it stays the same.

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document is Markdown parsed, which takes text appended to it, or edits, and parses only what they change.

func New

func New(ext Extensions) *Document

New returns an empty document reading ext.

func Parse

func Parse(src string) *Document

Parse parses src with the Default extensions.

func (*Document) Append

func (d *Document) Append(text string)

Append adds text to the end of the document, parsing again only the outermost block still open before it, or nothing but the new lines when no block is.

func (*Document) Block

func (d *Document) Block(i int) *Block

Block returns block i, 0 being the root. The block stays valid until the document changes.

func (*Document) Children

func (d *Document) Children(i int) iter.Seq[int]

Children returns the indices of the children of block i.

func (*Document) Edit

func (d *Document) Edit(from, to int, text string)

Edit replaces the source's bytes [from, to) with text. Parsing restarts at the top-level block before the change and goes on until a top-level block starts where one started before it, from which the blocks parsed before are kept.

func (*Document) Extensions

func (d *Document) Extensions() Extensions

Extensions returns what the document reads beyond CommonMark.

func (*Document) Footnote

func (d *Document) Footnote(label string) (int, bool)

Footnote returns the FootnoteDefinition block of a label, as a FootnoteRef's Text holds it.

func (*Document) HTML

func (d *Document) HTML() string

HTML returns the document as HTML, as cmark-gfm renders it, raw HTML included as written.

func (*Document) Inlines

func (d *Document) Inlines(i int) []Inline

Inlines returns the inlines of block i, a paragraph, a heading or a table cell, parsing them the first time.

func (*Document) Len

func (d *Document) Len() int

Len returns the number of blocks, the root's included.

func (*Document) Line

func (d *Document) Line(i, k int) (string, int)

Line returns line k of a code, HTML or math block, without its newline, and the offset in the source of its first byte.

func (*Document) LineCount

func (d *Document) LineCount(i int) int

LineCount returns the number of lines of a code, HTML or math block.

func (*Document) Lines

func (d *Document) Lines(i int) string

Lines returns the content of a code, HTML or math block, each line ending with a newline.

func (*Document) Set

func (d *Document) Set(src string)

Set makes src the text of the document, parsing only what changed since the last: text appended to it, or else the blocks around the change (see Edit).

func (*Document) SetStreaming

func (d *Document) SetStreaming(on bool)

SetStreaming sets whether the document is text still being written, as a language model's reply: its end then shows what it will most likely become rather than the half-written syntax it is. The last paragraph closes its emphasis, code spans, strikethrough and math, a link whose destination is being written is a link (Inline.Pending), a tag being written is left out, and lines that could still turn into something else are held back: a line of only a few markers, as - or ==, which would make the paragraph above a heading, and a table's header until its delimiter row comes. Finished blocks are never changed. Turn it off once the text is complete, to parse its end exactly as CommonMark does.

func (*Document) Source

func (d *Document) Source() string

Source returns the text of the document.

func (*Document) Streaming

func (d *Document) Streaming() bool

Streaming reports whether the document completes unclosed syntax at its end (see SetStreaming).

func (*Document) String

func (d *Document) String() string

String returns the source of the document.

type Extensions

type Extensions uint32

Extensions are the syntax a Document reads beyond CommonMark.

const (
	// ExtTables are GitHub's tables, of cells between pipes.
	ExtTables Extensions = 1 << iota
	// ExtTaskLists are list items starting with [ ] or [x].
	ExtTaskLists
	// ExtStrikethrough is text between one or two tildes.
	ExtStrikethrough
	// ExtAutolinks are links without angle brackets: www.example.com,
	// https://example.com and someone@example.com.
	ExtAutolinks
	// ExtFootnotes are references as [^1], defined by [^1]: lines.
	ExtFootnotes
	// ExtMath is TeX between $$ and $$, \[ and \], or \( and \), in blocks
	// of their own or within text.
	ExtMath
	// ExtDollarMath is TeX between single dollar signs, as $x^2$, except
	// where the dollars read as amounts of money: not after an opening
	// dollar followed by a space or a digit, nor before a closing one
	// following a space or followed by a digit. It needs ExtMath.
	ExtDollarMath

	// GFM is GitHub Flavored Markdown.
	GFM = ExtTables | ExtTaskLists | ExtStrikethrough | ExtAutolinks
	// Default is GitHub Flavored Markdown with footnotes and math, as
	// language models write it.
	Default = GFM | ExtFootnotes | ExtMath
)

type Inline

type Inline struct {
	Kind InlineKind
	// Text is the literal of Text, Code, HTML and Math, and the label of
	// a FootnoteRef.
	Text string
	// Dest and Title are a link's or an image's.
	Dest, Title string
	// Start and End are the offsets of the inline's bytes in the source.
	Start, End int
	// Next is the index after the inline and its children.
	Next int
	// Auto tells a link written as its address, as <https://…> or
	// www.example.com.
	Auto bool
	// Display tells math shown on its own lines, between $$ or \[ \].
	Display bool
	// Pending tells, while the document is streaming, a link or an image
	// whose destination is still being written.
	Pending bool
}

Inline is an inline of a block's content: inlines are in document order, an inline's children right after it, up to its Next.

type InlineKind

type InlineKind uint8

InlineKind is what an inline is.

const (
	// Text is literal text, in Text.
	Text InlineKind = iota
	// SoftBreak is a line ending within a paragraph, shown as a space.
	SoftBreak
	// LineBreak is a hard line break.
	LineBreak
	// Code is a code span, its text in Text.
	Code
	Emphasis
	Strong
	Strikethrough
	// Link holds the inlines of its text, to Dest, titled Title.
	Link
	// Image holds the inlines of its description, from Dest.
	Image
	// HTML is raw HTML, in Text.
	HTML
	// Math is TeX, in Text, Display or within the line.
	Math
	// FootnoteRef refers to the footnote labelled Text.
	FootnoteRef
)

Kinds of inlines.

func (InlineKind) String

func (k InlineKind) String() string

type Kind

type Kind uint8

Kind is what a block is.

const (
	// Root holds the top-level blocks: block 0 of a document.
	Root Kind = iota
	Paragraph
	// Heading has a Level from 1 to 6.
	Heading
	ThematicBreak
	// CodeBlock is fenced or indented code, whose Info string names the
	// language of a fence.
	CodeBlock
	// HTMLBlock is raw HTML, whose lines are its content.
	HTMLBlock
	// Quote is a block quote, holding blocks.
	Quote
	// List holds items, Ordered or not, Tight or not.
	List
	// Item is an item of a list, holding blocks, with a Task state.
	Item
	// Table holds rows, the first its Header, whose cells align as Align
	// says.
	Table
	TableRow
	// TableCell holds inline content.
	TableCell
	// FootnoteDefinition holds the blocks of a footnote, its Label the
	// footnote's.
	FootnoteDefinition
	// MathBlock is display math, whose lines are its TeX.
	MathBlock
)

Kinds of blocks.

func (Kind) String

func (k Kind) String() string

type Task

type Task uint8

Task is the check box of a task list item.

const (
	NoTask Task = iota
	Unchecked
	Checked
)

Jump to

Keyboard shortcuts

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