Documentation
¶
Overview ¶
Package markdown provides shared utilities for Markdown parsing and tokenization. This package is intended for use by the various md-tools binaries.
Index ¶
- func CodeSpanLen(runes []rune, i int) int
- func CollapseSpaces(text string) string
- func ExcludeRanges(content string, contentStart int, ranges []ByteRange) string
- func IsFootnoteContinuation(line string) bool
- func IsFootnoteDefinition(line string) bool
- func IsHorizontalRule(line string) bool
- func IsLinkRefDefinition(line string) bool
- func IsListItem(line string) bool
- func IsTableRow(line string) bool
- func LeadingIndent(lines []string) string
- func LooksLikeFrontmatterProperty(line string) bool
- func SplitFrontmatter(content string) (frontmatter, body string)
- func Transform(content string, h Handlers) string
- func TransformBlockquote(lines []string, flush func([]string) []string) []string
- type ByteRange
- type Handlers
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CodeSpanLen ¶ added in v1.2.1
CodeSpanLen returns the rune length of a backtick code span beginning at i, closed by a run of the same number of backticks, or 0 if none opens there or it is never closed. GFM preserves the interior of a code span verbatim, so callers copy runes[i : i+CodeSpanLen(runes, i)] without modification.
func CollapseSpaces ¶ added in v1.2.1
CollapseSpaces collapses every run of whitespace to a single space and trims leading and trailing whitespace — the paragraph-reflow normalization the tools share — except that the interior of an inline code span is copied verbatim, since GFM treats the spaces between backticks as significant.
func ExcludeRanges ¶
ExcludeRanges returns content with any overlapping ranges removed. contentStart is the byte offset where content begins in the original source. Ranges are specified in terms of the original source byte positions.
func IsFootnoteContinuation ¶
IsFootnoteContinuation returns true if the line continues the body of a preceding footnote definition: a non-blank line that does not itself start another block-level construct.
func IsFootnoteDefinition ¶
IsFootnoteDefinition returns true if the line starts a footnote definition. Footnote definitions have the form [^label]: ...
func IsHorizontalRule ¶
IsHorizontalRule returns true if the line is a horizontal rule. Horizontal rules are three or more -, *, or _ characters with optional spaces.
func IsLinkRefDefinition ¶
IsLinkRefDefinition returns true if the line is a link reference definition. Link reference definitions have the form [label]: URL This excludes footnote definitions.
func IsListItem ¶
IsListItem returns true if the line is a list item. Supports unordered lists (-, *, +) and ordered lists (1., 2., etc).
func IsTableRow ¶
IsTableRow returns true if the line is a GFM table row. GFM table rows start with a pipe character.
func LeadingIndent ¶ added in v1.2.1
LeadingIndent returns the run of leading spaces and tabs on the first line, or "" when there are no lines or it has no indent. It lets a paragraph handler re-apply an indent that joining would otherwise drop, keeping an indented continuation paragraph attached to its list item.
func LooksLikeFrontmatterProperty ¶
LooksLikeFrontmatterProperty returns true if the line appears to be a YAML frontmatter property (contains a colon not at the start).
func SplitFrontmatter ¶
SplitFrontmatter separates a leading YAML frontmatter block from the document body. When there is no frontmatter, frontmatter is empty and body is content; otherwise the two rejoin as frontmatter + "\n" + body.
func Transform ¶
Transform applies a Markdown-aware transformation to content, routing each block-level construct to the appropriate handler or emitting it unchanged. Frontmatter, code blocks, headers, list items (with continuations), table rows, and horizontal rules are passed through; paragraphs and blockquotes are delegated to h.
func TransformBlockquote ¶
TransformBlockquote applies a blockquote-aware transformation to consecutive blockquote lines. The flush function receives accumulated content lines with the "> " prefix stripped, and must return the transformed output lines with the prefix added back. GFM alert headers and table rows are emitted as-is without passing through flush.
Types ¶
type Handlers ¶
type Handlers struct {
// Paragraph is called with the collected lines of a regular paragraph and
// returns the transformed lines.
Paragraph func(lines []string) []string
// Blockquote is called with consecutive blockquote lines ("> " prefix
// intact) and returns the transformed lines.
Blockquote func(lines []string) []string
// Footnote is called with a footnote definition's lines (the "[^label]:"
// line plus any continuation lines) and returns the transformed lines.
// When nil, footnote definitions are emitted verbatim.
Footnote func(lines []string) []string
}
Handlers defines the tool-specific behavior for paragraph and blockquote processing in a Markdown document transformation.