Documentation
¶
Overview ¶
Package mdpp parses and renders Markdown++ documents.
MDPP keeps Markdown-compatible prose as the base syntax and adds structured nodes for technical writing features such as frontmatter, admonitions, footnotes, math, emoji shortcodes, task lists, and diagram fences. The parser is backed by gotreesitter and returns an AST that can be used by renderers, editors, formatters, diagnostics, and language service features.
Index ¶
- Constants
- func EmojiShortcodes() []string
- func GrammarBlobHandler() http.Handler
- func Render(doc *Document, opts RenderOptions) ([]byte, error)
- func RenderPDF(doc *Document, opts PDFOptions) ([]byte, error)
- func RenderString(source string) string
- func Slugify(s string) string
- type Diagnostic
- type Document
- func MustParse(source []byte) *Document
- func Parse(source []byte) (doc *Document, err error)
- func ParseIncremental(source []byte, prevTree *gotreesitter.Tree, edit gotreesitter.InputEdit) (doc *Document, tree *gotreesitter.Tree, err error)
- func ParseWithTree(source []byte) (doc *Document, tree *gotreesitter.Tree, err error)
- func (d *Document) AST() *Node
- func (d *Document) Diagnostics() []Diagnostic
- func (d *Document) FormatVersion() string
- func (d *Document) Frontmatter() map[string]any
- func (d *Document) Headings() []Heading
- func (d *Document) ReadingTime() time.Duration
- func (d *Document) TableOfContents() []TOCEntry
- func (d *Document) WordCount() int
- type HeaderFooterTemplate
- type Heading
- type Margins
- type MathOption
- type Node
- type NodeType
- type Option
- func WithContainerRenderer(fn func(c *Node, body string) string) Option
- func WithHardWraps(enabled bool) Option
- func WithHeadingIDs(enabled bool) Option
- func WithHighlightCode(enabled bool) Option
- func WithImageResolver(fn func(string) string) Option
- func WithSourcePositions(enabled bool) Option
- func WithUnsafeHTML(enabled bool) Option
- func WithWrapEmoji(enabled bool) Option
- type PDFOptions
- type PaperSize
- type Parser
- type Range
- type RenderOptions
- type Renderer
- type Severity
- type TOCEntry
Constants ¶
const SpecVersion = "0.1"
SpecVersion is the Markdown++ format version implemented by this package.
const Version = "0.1.10"
Version is the Markdown++ engine version.
Variables ¶
This section is empty.
Functions ¶
func EmojiShortcodes ¶ added in v0.1.10
func EmojiShortcodes() []string
EmojiShortcodes returns the known emoji shortcode names in stable order.
func GrammarBlobHandler ¶
GrammarBlobHandler returns an HTTP handler that serves gotreesitter grammar blobs on demand. Mount it at /grammars/ and any browser-side WASM module can fetch language grammars one at a time.
URL pattern: /grammars/{language}.blob (e.g. /grammars/go.blob)
func Render ¶ added in v0.1.10
func Render(doc *Document, opts RenderOptions) ([]byte, error)
Render produces HTML for a parsed document.
func RenderPDF ¶ added in v0.1.10
func RenderPDF(doc *Document, opts PDFOptions) ([]byte, error)
RenderPDF renders a document to PDF using headless Chrome.
func RenderString ¶
RenderString is a package-level convenience that parses and renders Markdown to HTML with default settings.
Types ¶
type Diagnostic ¶ added in v0.1.10
Diagnostic is a recoverable parser finding attached to a source range.
type Document ¶
Document is the top-level result of parsing a Markdown source.
func MustParse ¶ added in v0.1.10
MustParse parses Markdown source and panics only if Parse returns an error.
func ParseIncremental ¶ added in v0.2.5
func ParseIncremental(source []byte, prevTree *gotreesitter.Tree, edit gotreesitter.InputEdit) (doc *Document, tree *gotreesitter.Tree, err error)
ParseIncremental re-parses source by applying edit to prevTree and running the tree-sitter incremental parser. Callers must have produced prevTree from a prior ParseWithTree (or prior ParseIncremental) call against the pre-edit source. ParseIncremental takes ownership of prevTree and returns a fresh *Tree that the caller must eventually Release.
If the edit-applied source no longer satisfies the tree-sitter primary path (e.g. it now matches a container/all-indented/deep-nested fallback) this function falls back to a full parse and the returned Tree may be nil.
func ParseWithTree ¶ added in v0.2.5
func ParseWithTree(source []byte) (doc *Document, tree *gotreesitter.Tree, err error)
ParseWithTree parses source and also returns the underlying tree-sitter Tree if the tree-sitter path was used. The returned *Tree is non-nil only when the primary tree-sitter path (not a fallback) produced the document. Callers that receive a non-nil Tree must eventually call tree.Release() (or feed it back into ParseIncremental, which takes ownership).
func (*Document) Diagnostics ¶ added in v0.1.10
func (d *Document) Diagnostics() []Diagnostic
Diagnostics returns recoverable parse diagnostics.
func (*Document) FormatVersion ¶ added in v0.1.10
FormatVersion returns the frontmatter mdpp version, if declared.
func (*Document) Frontmatter ¶
Frontmatter returns the parsed frontmatter metadata, if any.
func (*Document) Headings ¶
Headings returns all headings in document order with their level, text, and a slugified ID.
func (*Document) ReadingTime ¶
ReadingTime estimates how long it takes to read the document at 200 words per minute. Returns a minimum of 1 minute if the document has any words.
func (*Document) TableOfContents ¶
TableOfContents returns a table of contents derived from all headings in the document.
type HeaderFooterTemplate ¶ added in v0.1.10
HeaderFooterTemplate contains chromedp-compatible header/footer HTML.
type Margins ¶ added in v0.1.10
type Margins struct{ Top, Right, Bottom, Left float64 }
Margins holds PDF page margins in inches.
type MathOption ¶ added in v0.1.10
type MathOption int
MathOption selects how math nodes render.
const ( MathServer MathOption = iota MathRaw MathOmit )
type Node ¶
type Node struct {
Type NodeType
Children []*Node
Literal string
Attrs map[string]string
Range Range
}
Node is a single element in the Markdown AST.
func (*Node) Find ¶ added in v0.1.10
Find returns descendants, including this node, with the requested type.
type NodeType ¶
type NodeType int
NodeType classifies an AST node.
const ( NodeDocument NodeType = iota NodeHeading NodeParagraph NodeCodeBlock NodeBlockquote NodeList NodeListItem NodeTable NodeTableRow NodeTableCell NodeThematicBreak NodeLink NodeImage NodeEmphasis NodeStrong NodeStrikethrough NodeCodeSpan NodeText NodeSoftBreak NodeHardBreak NodeHTMLBlock NodeHTMLInline NodeFootnoteRef NodeFootnoteDef NodeMathInline NodeMathBlock NodeAdmonition NodeDefinitionList NodeDefinitionTerm NodeDefinitionDesc NodeSuperscript NodeSubscript NodeTaskListItem NodeFrontmatter NodeTableOfContents NodeAutoEmbed NodeEmoji NodeDiagram NodeContainerDirective )
type Option ¶
type Option func(*Renderer)
Option configures a Renderer.
func WithContainerRenderer ¶ added in v0.1.10
WithContainerRenderer sets a custom renderer for container directives.
func WithHardWraps ¶
WithHardWraps makes single newlines render as <br> instead of whitespace.
func WithHeadingIDs ¶
WithHeadingIDs enables or disables automatic id attributes on headings.
func WithHighlightCode ¶
WithHighlightCode enables or disables code highlighting placeholders.
func WithImageResolver ¶
WithImageResolver sets a function to resolve image URLs.
func WithSourcePositions ¶ added in v0.1.10
WithSourcePositions emits data-mdpp-source-* attributes on rendered elements.
func WithUnsafeHTML ¶
WithUnsafeHTML enables or disables raw HTML passthrough.
func WithWrapEmoji ¶
WithWrapEmoji wraps emoji output in an accessible <span> with role="img" and aria-label.
type PDFOptions ¶ added in v0.1.10
type PDFOptions struct {
PaperSize PaperSize
PaperWidthInches float64
PaperHeightInches float64
MarginInches Margins
UserCSS string
Background bool
RenderOptions RenderOptions
BrowserURL string
Timeout time.Duration
SettleDelay time.Duration
}
PDFOptions configures RenderPDF.
type Parser ¶ added in v0.2.5
type Parser struct {
// contains filtered or unexported fields
}
Parser holds per-document state that survives across ParseIncremental calls, enabling reuse of expensive work (paragraph inline reparse, container body chunk parses) for unchanged regions.
A Parser is safe for sequential use by a single caller; concurrent Parse calls on the same Parser are not supported. Typical usage is to retain one Parser per open document in an LSP-like context.
func NewParser ¶ added in v0.2.5
func NewParser() *Parser
NewParser returns a fresh Parser with an empty cache and no retained tree.
func (*Parser) ApplyEdit ¶ added in v0.2.5
func (p *Parser) ApplyEdit(edit gotreesitter.InputEdit)
ApplyEdit applies an InputEdit to the retained tree without reparsing. Use this when multiple edits must be applied before a single incremental parse. ParseIncremental already applies the passed edit internally, so use this only for secondary edits that precede a ParseIncremental call.
func (*Parser) Close ¶ added in v0.2.5
func (p *Parser) Close()
Close releases any retained tree-sitter state.
func (*Parser) LastStats ¶ added in v0.2.5
LastStats returns (cacheHits, cacheMisses) recorded by the most recent Parse or ParseIncremental call. Useful for observability and tests.
func (*Parser) Parse ¶ added in v0.2.5
Parse runs a full parse, replacing any retained incremental state.
func (*Parser) ParseIncremental ¶ added in v0.2.5
ParseIncremental applies edit to the retained tree (if any) and runs an incremental parse, reusing cached AST subtrees for unchanged content. If no tree is retained (first parse or prior fallback), it runs a full parse.
type Range ¶ added in v0.1.10
Range identifies a node's byte span in Document.Source. Lines and columns are 1-indexed; columns are byte columns.
type RenderOptions ¶ added in v0.1.10
type RenderOptions struct {
HighlightCode bool
HeadingIDs bool
UnsafeHTML bool
HardWraps bool
WrapEmoji bool
ImageResolver func(src string) string
ContainerRenderer func(c *Node, body string) string
SourcePositions bool
Math MathOption
Sanitize bool
}
RenderOptions is the value-typed rendering API used by the CLI and tools.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer converts Markdown source into HTML.
func NewRenderer ¶
NewRenderer creates a Renderer with the given options.
func (*Renderer) Parse ¶
Parse parses Markdown source into a Document using the package-level parser.
func (*Renderer) RenderString ¶
RenderString parses and renders a Markdown string to HTML.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
mdpp
command
|
|
|
mdpp-lsp
command
|
|
|
Package fmt provides canonical formatting for Markdown++ source.
|
Package fmt provides canonical formatting for Markdown++ source. |
|
internal
|
|
|
Package lint provides diagnostics over Markdown++ documents.
|
Package lint provides diagnostics over Markdown++ documents. |