markdown

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package markdown provides a parser for converting Markdown text into an AST. It uses the tokenizer package to scan input and builds a tree of nodes representing the document structure.

Supported Syntax

Currently supported Markdown elements:

  • Headings: # H1, ## H2, ### H3, etc.
  • Blockquotes: > quoted text
  • Paragraphs: Separated by blank lines
  • Tables: | head | head | pipe-style tables with optional header rows
  • Unordered lists: - item, * item, or + item
  • Ordered lists: 1. item or 1) item
  • Emphasis: _italic_ or *italic*
  • Strong: __bold__ or **bold**
  • Strikethrough: ~~deleted~~
  • Code: `code` using backticks
  • Fenced code blocks: ```language ... ```
  • Horizontal rules: ---
  • Links: [text](url) or <url>
  • Images: ![alt](url)

Example

doc := markdown.Parse(strings.NewReader("# Hello _world_"), tokenizer.Pos{})
fmt.Println(doc) // <doc><h1>Hello <em>world</em></h1></doc>

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(r io.Reader, pos tokenizer.Pos) ast.Node

Parse reads Markdown text from r and returns an AST representing the document. The pos parameter specifies the starting position for error reporting. Returns a Document node containing the parsed content.

Types

type Blockquote

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

Blockquote represents a blockquote element in Markdown (> quoted text). It can contain paragraphs and other block elements.

func NewBlockquote

func NewBlockquote() *Blockquote

NewBlockquote creates a new empty Blockquote node.

func (*Blockquote) Append

func (b *Blockquote) Append(child ast.Node)

Append adds a child node to this blockquote.

func (*Blockquote) Children

func (b *Blockquote) Children() []ast.Node

Children returns the child nodes contained within this blockquote.

func (*Blockquote) Kind

func (b *Blockquote) Kind() ast.Kind

Kind returns the AST node kind for this blockquote.

func (*Blockquote) String

func (b *Blockquote) String() string

String returns an HTML representation of the blockquote.

type Code

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

Code is an inline node for code spans. In Markdown, inline code is created with backticks: `code` Code nodes are leaf nodes that contain the literal code text.

func NewCode

func NewCode(value string) *Code

NewCode creates a new code node with the given content. The value represents the code text without the surrounding backticks.

func (*Code) Children

func (c *Code) Children() []ast.Node

Children returns the child nodes (code nodes have no children)

func (*Code) Kind

func (c *Code) Kind() ast.Kind

Kind returns the node kind

func (*Code) String

func (c *Code) String() string

String returns a string representation of the code

func (*Code) Value

func (c *Code) Value() string

Value returns the code content

type CodeBlock

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

CodeBlock is a block node for fenced code blocks. In Markdown, fenced code blocks are created with triple backticks: ```language code here ``` The language is optional and specifies syntax highlighting.

func NewCodeBlock

func NewCodeBlock(language, content string) *CodeBlock

NewCodeBlock creates a new code block node with the given language and content. The language may be empty if not specified.

func (*CodeBlock) Children

func (c *CodeBlock) Children() []ast.Node

Children returns the child nodes (code blocks have no children)

func (*CodeBlock) Content

func (c *CodeBlock) Content() string

Content returns the code content

func (*CodeBlock) Kind

func (c *CodeBlock) Kind() ast.Kind

Kind returns the node kind

func (*CodeBlock) Language

func (c *CodeBlock) Language() string

Language returns the language specifier (may be empty)

func (*CodeBlock) String

func (c *CodeBlock) String() string

String returns a string representation of the code block

type Document

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

Document is the root node of a Markdown AST. It contains all block-level elements (paragraphs, headings, etc.) as children.

func NewDocument

func NewDocument() *Document

NewDocument creates a new empty document node. Use Append() to add child nodes such as paragraphs.

func (*Document) Append

func (d *Document) Append(node ast.Node)

Append adds a child node to the document

func (*Document) Children

func (d *Document) Children() []ast.Node

Children returns the child nodes

func (*Document) Kind

func (d *Document) Kind() ast.Kind

Kind returns the node kind

func (*Document) String

func (d *Document) String() string

String returns a string representation of the document

type Emphasis

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

Emphasis is an inline node for emphasized (italic) text. In Markdown, emphasis is created with single underscores: _italic_ The emphasis node contains child nodes representing the emphasized content.

func NewEmphasis

func NewEmphasis() *Emphasis

NewEmphasis creates a new empty emphasis node. Use Append() to add the content that should be emphasized.

func (*Emphasis) Append

func (e *Emphasis) Append(node ast.Node)

Append adds a child node to the emphasis

func (*Emphasis) Children

func (e *Emphasis) Children() []ast.Node

Children returns the child nodes

func (*Emphasis) Kind

func (e *Emphasis) Kind() ast.Kind

Kind returns the node kind

func (*Emphasis) String

func (e *Emphasis) String() string

String returns a string representation of the emphasis

type Heading

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

Heading is a block-level node for headings (h1-h6). In Markdown, headings are created with hash symbols: # H1, ## H2, etc. The level indicates the heading depth (1-6).

func NewHeading

func NewHeading(level int) *Heading

NewHeading creates a new heading node with the specified level (1-6). Use Append() to add the heading content.

func (*Heading) Append

func (h *Heading) Append(node ast.Node)

Append adds a child node to the heading

func (*Heading) Children

func (h *Heading) Children() []ast.Node

Children returns the child nodes

func (*Heading) Kind

func (h *Heading) Kind() ast.Kind

Kind returns the node kind

func (*Heading) Level

func (h *Heading) Level() int

Level returns the heading level (1-6)

func (*Heading) String

func (h *Heading) String() string

String returns a string representation of the heading

type Image

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

Image represents an image in Markdown (![alt](url)).

func NewImage

func NewImage(alt, url string) *Image

NewImage creates a new Image node with the given alt text and URL.

func (*Image) Alt

func (i *Image) Alt() string

Alt returns the image's alt text.

func (*Image) Children

func (i *Image) Children() []ast.Node

Children returns the child nodes (images have no children).

func (*Image) Kind

func (i *Image) Kind() ast.Kind

Kind returns the AST node kind for this image.

func (*Image) String

func (i *Image) String() string

String returns an HTML representation of the image.

func (*Image) URL

func (i *Image) URL() string

URL returns the image's source URL.

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

Link represents a hyperlink in Markdown ([text](url)).

func NewLink(url string) *Link

NewLink creates a new Link node with the given URL.

func (*Link) Append

func (l *Link) Append(child ast.Node)

Append adds a child node to this link's text content.

func (*Link) Children

func (l *Link) Children() []ast.Node

Children returns the child nodes (link text) contained within this link.

func (*Link) Kind

func (l *Link) Kind() ast.Kind

Kind returns the AST node kind for this link.

func (*Link) String

func (l *Link) String() string

String returns an HTML representation of the link.

func (*Link) URL

func (l *Link) URL() string

URL returns the link's destination URL.

type List

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

List is a block-level node for both ordered and unordered lists. In Markdown, unordered lists are created with dashes, asterisks, or plus signs: - item 1 - item 2 Ordered lists are created with numbers followed by a period or parenthesis: 1. item 1 2. item 2 The list contains ListItem children.

func NewList

func NewList() *List

NewList creates a new empty unordered list node. Use Append() to add ListItem children.

func NewOrderedList

func NewOrderedList() *List

NewOrderedList creates a new empty ordered list node. Use Append() to add ListItem children.

func (*List) Append

func (l *List) Append(node ast.Node)

Append adds a child node to the list (should be ListItem nodes)

func (*List) Children

func (l *List) Children() []ast.Node

Children returns the child nodes (ListItem nodes)

func (*List) Kind

func (l *List) Kind() ast.Kind

Kind returns the node kind

func (*List) Ordered

func (l *List) Ordered() bool

Ordered returns true if this is an ordered list

func (*List) String

func (l *List) String() string

String returns a string representation of the list

type ListItem

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

ListItem is a block-level node representing a single item in a list. In Markdown, list items start with a marker (-, *, +) followed by content. List items can contain inline formatting like emphasis, strong, code, etc.

func NewListItem

func NewListItem() *ListItem

NewListItem creates a new empty list item node. Use Append() to add the item content.

func (*ListItem) Append

func (li *ListItem) Append(node ast.Node)

Append adds a child node to the list item

func (*ListItem) Children

func (li *ListItem) Children() []ast.Node

Children returns the child nodes

func (*ListItem) Kind

func (li *ListItem) Kind() ast.Kind

Kind returns the node kind

func (*ListItem) String

func (li *ListItem) String() string

String returns a string representation of the list item

type Paragraph

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

Paragraph is a block-level node containing inline content. Paragraphs are separated by blank lines in Markdown source. They can contain text, emphasis, strong, code, and other inline elements.

func NewParagraph

func NewParagraph() *Paragraph

NewParagraph creates a new empty paragraph node. Use Append() to add inline content such as text or emphasis.

func (*Paragraph) Append

func (p *Paragraph) Append(node ast.Node)

Append adds a child node to the paragraph

func (*Paragraph) Children

func (p *Paragraph) Children() []ast.Node

Children returns the child nodes

func (*Paragraph) Kind

func (p *Paragraph) Kind() ast.Kind

Kind returns the node kind

func (*Paragraph) String

func (p *Paragraph) String() string

String returns a string representation of the paragraph

type Rule

type Rule struct{}

Rule represents a horizontal rule in Markdown (---, ***, or ___).

func NewRule

func NewRule() *Rule

NewRule creates a new Rule node.

func (*Rule) Children

func (r *Rule) Children() []ast.Node

Children returns nil as rules have no children.

func (*Rule) Kind

func (r *Rule) Kind() ast.Kind

Kind returns the AST node kind for this rule.

func (*Rule) String

func (r *Rule) String() string

String returns an HTML representation of the rule.

type Strikethrough

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

Strikethrough represents strikethrough text in Markdown (~~deleted~~).

func NewStrikethrough

func NewStrikethrough() *Strikethrough

NewStrikethrough creates a new empty Strikethrough node.

func (*Strikethrough) Append

func (s *Strikethrough) Append(child ast.Node)

Append adds a child node to this strikethrough.

func (*Strikethrough) Children

func (s *Strikethrough) Children() []ast.Node

Children returns the child nodes contained within this strikethrough.

func (*Strikethrough) Kind

func (s *Strikethrough) Kind() ast.Kind

Kind returns the AST node kind for this strikethrough.

func (*Strikethrough) String

func (s *Strikethrough) String() string

String returns an HTML representation of the strikethrough.

type Strong

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

Strong is an inline node for strongly emphasized (bold) text. In Markdown, strong emphasis is created with double underscores: __bold__ The strong node contains child nodes representing the bold content.

func NewStrong

func NewStrong() *Strong

NewStrong creates a new empty strong node. Use Append() to add the content that should be bold.

func (*Strong) Append

func (s *Strong) Append(node ast.Node)

Append adds a child node to the strong

func (*Strong) Children

func (s *Strong) Children() []ast.Node

Children returns the child nodes

func (*Strong) Kind

func (s *Strong) Kind() ast.Kind

Kind returns the node kind

func (*Strong) String

func (s *Strong) String() string

String returns a string representation of the strong

type Table added in v0.0.2

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

Table is a block-level node containing an optional header and zero or more rows.

func NewTable added in v0.0.2

func NewTable() *Table

func (*Table) Append added in v0.0.2

func (t *Table) Append(node ast.Node)

func (*Table) Children added in v0.0.2

func (t *Table) Children() []ast.Node

func (*Table) Header added in v0.0.2

func (t *Table) Header() *TableHeader

func (*Table) Kind added in v0.0.2

func (t *Table) Kind() ast.Kind

func (*Table) Rows added in v0.0.2

func (t *Table) Rows() []ast.Node

func (*Table) SetHeader added in v0.0.2

func (t *Table) SetHeader(header *TableHeader)

func (*Table) String added in v0.0.2

func (t *Table) String() string

type TableCell added in v0.0.2

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

TableCell represents a single table cell containing inline nodes.

func NewTableCell added in v0.0.2

func NewTableCell() *TableCell

func (*TableCell) Append added in v0.0.2

func (c *TableCell) Append(node ast.Node)

func (*TableCell) Children added in v0.0.2

func (c *TableCell) Children() []ast.Node

func (*TableCell) Kind added in v0.0.2

func (c *TableCell) Kind() ast.Kind

func (*TableCell) String added in v0.0.2

func (c *TableCell) String() string

type TableHeader added in v0.0.2

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

TableHeader represents the header row of a table.

func NewTableHeader added in v0.0.2

func NewTableHeader() *TableHeader

func NewTableHeaderFromRow added in v0.0.2

func NewTableHeaderFromRow(row *TableRow) *TableHeader

func (*TableHeader) Append added in v0.0.2

func (h *TableHeader) Append(node ast.Node)

func (*TableHeader) Children added in v0.0.2

func (h *TableHeader) Children() []ast.Node

func (*TableHeader) Kind added in v0.0.2

func (h *TableHeader) Kind() ast.Kind

func (*TableHeader) String added in v0.0.2

func (h *TableHeader) String() string

type TableRow added in v0.0.2

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

TableRow represents a body row in a table.

func NewTableRow added in v0.0.2

func NewTableRow() *TableRow

func (*TableRow) Append added in v0.0.2

func (r *TableRow) Append(node ast.Node)

func (*TableRow) Children added in v0.0.2

func (r *TableRow) Children() []ast.Node

func (*TableRow) Kind added in v0.0.2

func (r *TableRow) Kind() ast.Kind

func (*TableRow) String added in v0.0.2

func (r *TableRow) String() string

type Text

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

Text is an inline leaf node containing plain text content. Text nodes have no children and represent literal text in the document.

func NewText

func NewText(value string) *Text

NewText creates a new text node with the given content. The value is stored as-is without any processing.

func (*Text) Children

func (t *Text) Children() []ast.Node

Children returns the child nodes (text nodes have no children)

func (*Text) Kind

func (t *Text) Kind() ast.Kind

Kind returns the node kind

func (*Text) String

func (t *Text) String() string

String returns a string representation of the text

func (*Text) Value

func (t *Text) Value() string

Value returns the text content

Directories

Path Synopsis
Package html provides an HTML renderer for Markdown AST nodes.
Package html provides an HTML renderer for Markdown AST nodes.

Jump to

Keyboard shortcuts

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