Documentation
¶
Overview ¶
Package parser converts markdown to HTML using a configurable goldmark pipeline. The default Parser enables GFM extensions (tables, strikethrough, task lists, autolinks), syntax highlighting with chroma's github style, client-side Mermaid diagrams, MathJax, GitHub-style callouts, and extended-syntax footnotes.
Every block-level element in the output carries a data-source-line attribute pointing at its 1-indexed line in the source, which downstream consumers can use for scroll-sync or other cursor-aware integrations.
data-source-line ordering ¶
With footnotes enabled, data-source-line values are NOT guaranteed to be non-decreasing in document order. Footnote definitions are collected into a <div class="footnotes"> endnote list rendered last, but each entry keeps the source line where it was defined. Two shapes produce out-of-order values:
- A definition placed mid-document renders after body content that appears later in the source.
- Footnotes are numbered by first-reference order, so when reference order differs from definition order the entries themselves are out of source order — even with every definition at the end of the file.
Consumers that map a cursor line to an element by scanning in document order and stopping at the first larger value must skip the .footnotes subtree, or they will select a footnote instead of the intended block. Outside that subtree the values are non-decreasing.
Skipping the subtree means footnote entries are never selected as scroll targets, so a cursor sitting on a definition line resolves to the nearest preceding body block instead. That is a property of the scan, not of the exclusion: a scan that stops at the first larger value already passes the definition's line before reaching the endnote list, so it behaves the same either way. Callers needing a definition to be reachable should look it up by its line directly rather than relying on the ordered scan.
Minimal usage ¶
The zero-config Parser is suitable for most callers:
p := parser.New()
html, err := p.Render([]byte("# Hello"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", html)
All options ¶
Each feature has a With* toggle. Pass mermaid.RenderModeServer to WithMermaidRenderMode to render Mermaid diagrams to inline <svg> at parse time (requires the mmdc CLI). The default RenderModeClient emits <pre class="mermaid"> placeholders for the browser to render with mermaid.js.
import "go.abhg.dev/goldmark/mermaid"
p := parser.New(
parser.WithGFM(true),
parser.WithSyntaxHighlighting(true),
parser.WithMermaid(true),
parser.WithMermaidRenderMode(mermaid.RenderModeClient),
parser.WithMath(true),
parser.WithCallouts(true),
parser.WithFootnotes(true),
)
Concurrency ¶
Parser.Render is safe for concurrent use. A single Parser may be shared across goroutines.
Note: as a temporary workaround for a known data race in gm-alert-callouts@v0.8.0 (see INV-0003), Render currently serializes goldmark conversion behind a per-Parser mutex. Throughput-sensitive callers that fan out across many goroutines should construct multiple Parser instances until the upstream fix lands.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option configures a Parser.
func WithCallouts ¶
WithCallouts enables or disables GitHub-style callout/alert rendering (> [!NOTE], > [!TIP], > [!IMPORTANT], > [!WARNING], > [!CAUTION]).
func WithFootnotes ¶ added in v0.3.0
WithFootnotes enables or disables extended-syntax footnotes ([^1] references and [^1]: definitions). Definitions are collected into a <div class="footnotes"> endnote list rendered at the end of the output, regardless of where they appear in the source.
func WithGFM ¶
WithGFM enables or disables GitHub Flavored Markdown extensions (tables, strikethrough, task lists, autolinks).
func WithMermaid ¶
WithMermaid enables or disables Mermaid diagram support.
func WithMermaidRenderMode ¶ added in v0.1.12
func WithMermaidRenderMode(mode mermaid.RenderMode) Option
WithMermaidRenderMode sets the Mermaid render mode. The default is mermaid.RenderModeClient, which emits <pre class="mermaid"> blocks for the browser to render with mermaid.js. mermaid.RenderModeServer renders to inline <svg> at parse time (requires the mermaid CLI). Has no effect when WithMermaid(false) is set.
func WithSyntaxHighlighting ¶
WithSyntaxHighlighting enables or disables syntax highlighting on fenced code blocks.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser converts markdown to HTML using goldmark.
func New ¶
New creates a Parser with the given options. By default, GFM extensions, syntax highlighting, Mermaid, math, callouts, and footnotes are all enabled.
Example ¶
package main
import (
"fmt"
"github.com/donaldgifford/mdp/pkg/parser"
)
func main() {
p := parser.New()
html, err := p.Render([]byte("# Hi"))
if err != nil {
fmt.Println(err)
return
}
fmt.Print(string(html))
}
Output: <h1 id="hi" data-source-line="1">Hi</h1>