mdparse

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package mdparse turns a line buffer into a contiguous sequence of line-range blocks backed by the goldmark AST, plus an Obsidian-style wikilink inline extension. It is the bridge between the raw document model (internal/doc) and the renderer/editor: the renderer walks each block's goldmark Node to style inline text, and the editor uses the line ranges to decide which block the cursor sits in. It imports no TUI library and no other internal packages.

Index

Constants

This section is empty.

Variables

View Source
var HighlightExt goldmark.Extender = &highlightExtender{}

HighlightExt installs the ==highlight== inline parser.

View Source
var KindHighlight = ast.NewNodeKind("Highlight")

KindHighlight is the NodeKind for Highlight nodes.

View Source
var KindWikiLink = ast.NewNodeKind("WikiLink")

KindWikiLink is the NodeKind for WikiLink nodes.

View Source
var WikiLinkExt goldmark.Extender = &wikiLinkExtender{}

WikiLinkExt is the goldmark extender that installs the wikilink inline parser. It runs at priority 199, immediately ahead of the standard link parser (200).

Functions

func LinkAt added in v0.2.0

func LinkAt(line string, col int) (dest string, ok bool)

LinkAt reports the markdown link destination if the rune column col falls within a [label](dest) span on the given raw line. col is measured in runes. Prefer WikiLinkAt first when both could apply — [[x]] is never a md link.

func Markdown

func Markdown() goldmark.Markdown

Markdown returns the shared, configured goldmark instance used by Parse. The renderer can reuse it so inline parsing (including wikilinks) matches exactly.

func WikiLinkAt

func WikiLinkAt(line string, col int) (target string, ok bool)

WikiLinkAt reports the wikilink target if the rune column col falls within a [[...]] span (inclusive of the surrounding brackets) on the given raw line. col is measured in runes. It is regex-based and independent of the AST, so it works directly on the raw text under the editor cursor.

Types

type Block

type Block struct {
	Kind       Kind
	Start, End int
	Node       ast.Node
}

Block is a contiguous, inclusive range of raw line indices [Start, End] with its classified Kind and (for goldmark-derived blocks) the backing AST node. Node is nil for Blank and Frontmatter blocks.

type Highlight added in v0.4.0

type Highlight struct {
	ast.BaseInline
}

Highlight is an Obsidian-style ==marked== inline span.

func (*Highlight) Dump added in v0.4.0

func (n *Highlight) Dump(source []byte, level int)

Dump implements ast.Node for debugging.

func (*Highlight) Kind added in v0.4.0

func (n *Highlight) Kind() ast.NodeKind

Kind implements ast.Node.

type Kind

type Kind int

Kind classifies a block for rendering and editing decisions.

const (
	Blank Kind = iota
	Heading
	Paragraph
	CodeFence
	List
	Table
	Blockquote
	ThematicBreak
	Frontmatter
	Other // HTML block and any other unmapped content.
	// IndentedCode is a 4-space (or tab) indented code block. It is kept distinct
	// from CodeFence because it has no “` delimiter lines and no info string.
	// Appended last so existing Kind values (Blank..Other) keep their integers.
	IndentedCode
)

type Result

type Result struct {
	Blocks []Block
	Source []byte
}

Result is the output of Parse: the ordered blocks plus the exact bytes that were parsed (all lines joined with "\n"). Source is what the renderer must use to resolve AST text segments.

func Parse

func Parse(lines []string) Result

Parse segments lines into contiguous blocks. The returned Blocks always cover every line index in [0, len(lines)-1] with no gaps and no overlap — the invariant the editor's virtualization depends on.

goldmark parses the FULL source, so every Block.Node's inline segments align to Result.Source by construction (the render consumer slices those segments straight out of Source). Frontmatter is detected separately (line 0 "---" with a later "---" closing it) and the blocks goldmark produced within that line range are collapsed into a single Frontmatter block (Node nil).

type WikiLink struct {
	ast.BaseInline
	Target  string
	Alias   string
	Segment text.Segment
}

WikiLink is an inline AST node representing an Obsidian-style wikilink, either [[target]] or [[target|alias]]. Segment is the byte range of the target text within the parsed source, so the renderer can slice it directly out of Result.Source (it always aligns because goldmark parses the full source).

func (*WikiLink) Dump

func (n *WikiLink) Dump(source []byte, level int)

Dump implements ast.Node for debugging.

func (*WikiLink) Kind

func (n *WikiLink) Kind() ast.NodeKind

Kind implements ast.Node.

func (*WikiLink) Label

func (n *WikiLink) Label() string

Label returns the text that should be displayed for the link: the alias when present, otherwise the target.

Jump to

Keyboard shortcuts

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