markdown

package module
v0.0.0-...-42b8d32 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Package markdown is a Commonmark-compliant Markdown parser and HTML generator. It does not have many bells and whistles, but it does expose the parsed syntax in an easy-to-use form.

Work in progress.

TODO:

  • documentation
  • make Format always print valid markdown, even when the tree was constructed manually and may not correspond to something Parse would return.
  • footnote support
  • possibly math support
  • would it be simpler to have a lexer generated from regexps?

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(b Block) string

func ToHTML

func ToHTML(b Block) string

func ToText

func ToText(b Block) string

Types

type AutoLink struct {
	Text string
	URL  string
}

An AutoLink is an Inline representing an autolink, which is an absolute URL or email address inside < > brackets.

func (*AutoLink) Inline

func (*AutoLink) Inline()

type Block

type Block interface {
	Block()
	Pos() Position
	// contains filtered or unexported methods
}

Block is implemented by:

CodeBlock
Document
Empty
HTMLBlock
Heading
Item
List
Paragraph
Quote
Text
ThematicBreak

type Code

type Code struct {
	Text string
}

A Code is an Inline that represents a code span.

func (*Code) Inline

func (*Code) Inline()

type CodeBlock

type CodeBlock struct {
	Position
	Fence string   // fence to use
	Info  string   // info following open fence
	Text  []string // lines of code block
}

A CodeBlock is a Block representing an indented code block or fenced code block, usually displayed in <pre><code> tags.

When printing a CodeBlock as Markdown, the Fence field is used as a starting hint but is made longer as needed if the suggested fence text appears in Text.

func (*CodeBlock) Block

func (*CodeBlock) Block()

type Del

type Del struct {
	Marker string
	Inner  Inlines
}

A Deleted is an Inline that represents deleted (strikethrough) text, a GitHub-flavored Markdown extension.

func (*Del) Inline

func (*Del) Inline()

type Document

type Document struct {
	Position
	Blocks []Block
	Links  map[string]*Link
}

func (*Document) Block

func (*Document) Block()

type Emoji

type Emoji struct {
	Name string // emoji :name:, including colons
	Text string // Unicode for emoji sequence
}

An Emoji is an Inline that represents an emoji, like :smiley:, an apparently undocumented but widely used GitHub Markdown extension.

func (*Emoji) Inline

func (*Emoji) Inline()

type Emph

type Emph struct {
	Marker string
	Inner  Inlines
}

An Emph is an Inline representing emphasis (italic text).

func (*Emph) Inline

func (*Emph) Inline()

type Empty

type Empty struct {
	Position
}

An Empty is a Block representing no block at all. The parser never returns a parse tree containing an Empty, but it can be useful during syntax editing. It does not render as anything at all.

func (*Empty) Block

func (*Empty) Block()

type Escaped

type Escaped struct {
	Plain // single character text (omitting the escaping backslash)
}

An Escaped is an Inline that represents a backslash escaped symbol.

type Footnote

type Footnote struct {
	Position
	Label  string
	Blocks []Block
}
type FootnoteLink struct {
	Label    string
	Footnote *Footnote
}

func (*FootnoteLink) Inline

func (*FootnoteLink) Inline()

type HTMLBlock

type HTMLBlock struct {
	Position
	// TODO should these be 'Text string'?
	Text []string // lines, without trailing newlines
}

An HTMLBlock is a Block representing an HTML block.

func (*HTMLBlock) Block

func (*HTMLBlock) Block()

type HTMLTag

type HTMLTag struct {
	Text string // TODO rename to HTML?
}

An HTMLTag is an Inline representing a raw HTML tag.

func (*HTMLTag) Inline

func (*HTMLTag) Inline()

type HardBreak

type HardBreak struct{}

A HardBreak is an Inline representing a hard line break (<br> tag).

func (*HardBreak) Inline

func (*HardBreak) Inline()

type Heading

type Heading struct {
	Position

	// Level is the heading level: 1 through 6.
	// Other values are clamped to the valid range.
	Level int

	// Text is the text of the heading.
	Text *Text

	// ID is the HTML id attribute.
	// The parser populates this field if [Parser.HeadingID] is true
	// and the heading ends with text like "{#id}".
	ID string
}

A Heading is a Block representing an ATX heading or Setext heading, usually displayed with the <h1> through <h6> tags.

func (*Heading) Block

func (*Heading) Block()

type Image

type Image struct {
	Inner     Inlines
	URL       string
	Title     string
	TitleChar byte
}

An Image is an Inline representing an image (<a> tag).

func (*Image) Inline

func (*Image) Inline()

type Inline

type Inline interface {
	Inline()
	// contains filtered or unexported methods
}

An Inline is an inline Markdown element, one of Plain, Escaped, Code, Strong, Emph, Del, Link, AutoLink, Image, SoftBreak, HardBreak, HTMLTag, Emoji, and Task.

type Inlines

type Inlines []Inline

An Inlines is an Inline that represents a concatenation of Inlines.

func (Inlines) Inline

func (Inlines) Inline()

type Item

type Item struct {
	Position

	// Blocks is the item content.
	Blocks []Block
}

An Item is a Block representing a list item.

func (*Item) Block

func (*Item) Block()
type Link struct {
	Inner     Inlines
	URL       string
	Title     string
	TitleChar byte // ', " or )
}

A Link is an Inline representing a link (<a> tag).

func (*Link) Inline

func (*Link) Inline()

type List

type List struct {
	Position

	// Bullet is the bullet character used in the list: '-', '+', or '*'.
	// For an ordered list, Bullet is the character following the number: '.' or ')'.
	Bullet rune

	// Start is the number of the first item in an ordered list.
	Start int

	// Loose indicates whether the list is loose.
	// (See the [List] doc comment for details.)
	Loose bool

	// Items is the list's items.
	// TODO: Should this be []*Item or Blocks?
	Items []Block // always *Item
}

A List is a Block representing a list, either an unordered (bullet) list or an ordered (numbered) list.

Lists can be loose or tight, which controls the spacing between list items. In Markdown, a list is loose when there is a blank line between any two list items, or when any list item directly contains two blocks that are separated by a blank line. (Note that because paragraphs must be separated by blank lines, any multi-paragraph item necessarily creates a loose list.) When rendering HTML, loose list items are formatted in the usual way. For tight lists, a list item consisting of a single paragraph omits the <p>...</p> tags around the paragraph text.

func (*List) Block

func (*List) Block()

func (*List) Ordered

func (l *List) Ordered() bool

Ordered reports whether the list is ordered (numbered).

type Paragraph

type Paragraph struct {
	Position
	Text *Text
}

A Paragraph is a Block representing a paragraph. Except when they appear as top-level blocks in an item of a tight list, paragraphs render in <p>...</p> tags.

func (*Paragraph) Block

func (*Paragraph) Block()

type Parser

type Parser struct {
	// HeadingID determines whether the parser accepts
	// the {#hdr} syntax for an HTML id="hdr" attribute on headings.
	// For example, if HeadingIDs is true then the Markdown
	//    ## Overview {#overview}
	// will render as the HTML
	//    <h2 id="overview">Overview</h2>
	HeadingID bool

	// Strikethrough determines whether the parser accepts
	// ~abc~ and ~~abc~~ as strikethrough syntax, producing
	// <del>abc</del> in HTML.
	Strikethrough bool

	// TaskList determines whether the parser accepts
	// “task list items” as defined in GitHub Flavored Markdown.
	// When a list item begins with the plain text [ ] or [x]
	// that turns into an unchecked or checked check box.
	TaskList bool

	// TODO
	AutoLinkText       bool
	AutoLinkAssumeHTTP bool

	// TODO
	Table bool

	// TODO
	Emoji bool

	// TODO
	SmartDot   bool
	SmartDash  bool
	SmartQuote bool

	// TODO
	Footnote bool
}

A Parser is a Markdown parser. The exported fields in the struct can be filled in before calling Parser.Parse in order to customize the details of the parsing process. A Parser is safe for concurrent use by multiple goroutines.

func (*Parser) Parse

func (p *Parser) Parse(text string) *Document

type Plain

type Plain struct {
	Text string
}

A Plain is an Inline that represents [plain textual content].

func (*Plain) Inline

func (*Plain) Inline()

type Position

type Position struct {
	StartLine int
	EndLine   int
}

func (Position) Pos

func (p Position) Pos() Position

type Quote

type Quote struct {
	Position
	Blocks []Block // content of quote
}

A Quote is a Block representing a block quote.

func (*Quote) Block

func (*Quote) Block()

type SoftBreak

type SoftBreak struct{}

A SoftBreak is an Inline representing a soft line break (newline character).

func (*SoftBreak) Inline

func (*SoftBreak) Inline()

type Strong

type Strong struct {
	Marker string
	Inner  Inlines
}

A Strong is an Inline that represents strong emphasis (bold text).

func (*Strong) Inline

func (*Strong) Inline()

type Table

type Table struct {
	Position
	Header []*Text   // header row (slice of columns)
	Align  []string  // alignment for columns: "left", "center", "right"; "" for unset
	Rows   [][]*Text // data rows (slices of columns, not necessarily all same width)
}

A Table is a Block representing a table, a GitHub-flavored Markdown extension.

func (*Table) Block

func (*Table) Block()

type Task

type Task struct {
	Checked bool
}

A Task is an Inline for a task list item marker (a checkbox), a GitHub-flavored Markdown extension.

func (*Task) Inline

func (*Task) Inline()

type Text

type Text struct {
	Position
	Inline Inlines
}

func (*Text) Block

func (*Text) Block()

TODO: This is only a Block for tight lists. Maybe keep the Paragraphs for those?

type ThematicBreak

type ThematicBreak struct {
	Position
}

A ThematicBreak is a Block representing a thematic break, usually displayed as a horizontal rule (<hr> tag).

func (*ThematicBreak) Block

func (*ThematicBreak) Block()

Directories

Path Synopsis
Md2html converts Markdown to HTML.
Md2html converts Markdown to HTML.
Mdfmt reformats Markdown data.
Mdfmt reformats Markdown data.

Jump to

Keyboard shortcuts

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