markdown

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package markdown provides utilities for parsing and querying markdown documents

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateLineColumn

func CalculateLineColumn(content []byte, offset int) (line int, column int)

CalculateLineColumn calculates both line and column number from byte offset

func CalculateLineNumber

func CalculateLineNumber(content []byte, offset int) int

CalculateLineNumber calculates line number from byte offset

func CreateHeadingStructure

func CreateHeadingStructure(headings []string, baseLevel int) []byte

CreateHeadingStructure creates missing heading hierarchy

func ExtractHeadingText

func ExtractHeadingText(heading *ast.Heading, content []byte) string

ExtractHeadingText extracts text from a heading node

func FindInsertionPoint

func FindInsertionPoint(content []byte, targetOffset int, prepend bool) int

FindInsertionPoint finds the best byte offset for inserting content at a specific location

func FindSubtreeEnd

func FindSubtreeEnd(heading *ast.Heading, content []byte) int

FindSubtreeEnd finds where a subtree ends (before next same-level heading) This is now public to allow external testing and usage

func GetNodeOffset

func GetNodeOffset(node ast.Node, content []byte) int

GetNodeOffset gets the byte offset of a node in the content

func ParseDocument

func ParseDocument(content []byte) ast.Node

ParseDocument parses markdown content and returns the AST document

func PathMatches

func PathMatches(actualPath []string, targetSegments []string, skipLevels int) bool

PathMatches checks if a path matches the given segments using contains matching

func TestCalculateLineNumberAdvanced

func TestCalculateLineNumberAdvanced(t *testing.T)

func TestCreateHeadingStructure

func TestCreateHeadingStructure(t *testing.T)

func TestFindAllHeadings

func TestFindAllHeadings(t *testing.T)

func TestFindSubtreeAdvanced

func TestFindSubtreeAdvanced(t *testing.T)

func TestParsePathAdvanced

func TestParsePathAdvanced(t *testing.T)

func TestPathMatches

func TestPathMatches(t *testing.T)

func TestTransformHeadingLevels

func TestTransformHeadingLevels(t *testing.T)

func TransformHeadingLevels

func TransformHeadingLevels(content []byte, levelDiff int) []byte

TransformHeadingLevels adjusts heading levels in markdown content

func ValidateOffset

func ValidateOffset(content []byte, offset int) int

ValidateOffset ensures an offset is within valid bounds for the given content

Types

type HeadingInfo

type HeadingInfo struct {
	Text   string   // Heading text
	Level  int      // Heading level (1-6)
	Path   []string // Full path to this heading
	Offset int      // Byte offset in document
}

HeadingInfo represents information about a heading in the document

func FindAllHeadings

func FindAllHeadings(doc ast.Node, content []byte) []HeadingInfo

FindAllHeadings returns all headings in the document with their paths

type HeadingPath

type HeadingPath struct {
	File       string   // "inbox.md" - source file name
	Segments   []string // ["meeting", "attendees"] - path segments for navigation
	SkipLevels int      // Number of leading slashes (for unusual document structures)
}

HeadingPath represents a parsed path selector for navigating markdown headings

func ParsePath

func ParsePath(pathStr string) (*HeadingPath, error)

ParsePath parses a path selector like "file.md#path/to/heading"

type LineHeadingMap

type LineHeadingMap map[int]string

LineHeadingMap represents the mapping from line numbers to heading paths

func FindNearestHeadingsForLines

func FindNearestHeadingsForLines(content []byte, targetLines []int) (LineHeadingMap, error)

FindNearestHeadingsForLines efficiently finds the nearest dominant heading for multiple line numbers by parsing the document once and tracking heading context as we go. It stops parsing once all target lines have been resolved.

type OffsetRange

type OffsetRange struct {
	Start int
	End   int
}

OffsetRange represents a range of bytes in content

func (OffsetRange) Extract

func (r OffsetRange) Extract(content []byte) []byte

Extract returns the content within the range

func (OffsetRange) IsValid

func (r OffsetRange) IsValid(content []byte) bool

IsValid checks if the offset range is valid for the given content

func (OffsetRange) Length

func (r OffsetRange) Length() int

Length returns the length of the range

type Subtree

type Subtree struct {
	Heading     string // Original heading text
	Level       int    // Original heading level (1-6)
	Content     []byte // Full subtree content (markdown)
	StartOffset int    // Byte position in source
	EndOffset   int    // Byte position in source
}

Subtree represents a complete markdown subtree (heading + all nested content)

func FindSubtree

func FindSubtree(doc ast.Node, content []byte, path *HeadingPath) (*Subtree, error)

FindSubtree finds a subtree matching the given path selector

Jump to

Keyboard shortcuts

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