parser package configures the Goldmark markdown parser with custom extensions and AST transformers for URL rewriting, table of contents generation, and server-side rendering of diagrams and math.
Overview
The parser uses Goldmark as the base markdown processor and adds:- GFM Support: GitHub Flavored Markdown (tables, strikethrough, task lists)
- Syntax Highlighting: Nord theme with Chroma, custom code block wrappers
- Math Rendering: LaTeX passthrough with
$...$,$$...$$,\(...\),\[...\] - Admonitions: Callout boxes for notes, warnings, tips
- URL Transformation: Converts
.mdlinks to.html, handles versioned docs - TOC Generation: Automatic table of contents from headings
- SSR Support: Pre-renders D2 diagrams and LaTeX math on the server
Creating a Parser
baseURL- Base URL for absolute links (empty for relative links)renderer- Native renderer for D2 diagrams and KaTeX mathdiagramCache- Thread-safe cache for rendered diagrams
Key Types
TOCEntry
D2SVGPair
Core Functions
ExtractPlainText
GetTOC
GetSSRHashes
AddSSRHash
LaTeX Math Functions
ExtractMathExpressions
- Block math:
$$...$$,\[...\] - Inline math:
$...$,\(...\)
RenderMathForHTML
URL Transformation
TheurlTransformer handles:
- Markdown to HTML:
page.md→page.html - External links: Add
target="_blank"andrel="noopener noreferrer" - Image optimization:
.jpg,.png→.webp - Version-aware linking: Handles relative paths in versioned documentation
- Lazy loading: Adds
loading="lazy"to images
Version-Aware Linking Rules
Code Block Enhancement
Code blocks are wrapped with custom HTML structure:AST Transformers
Three transformers run on the parsed AST:- urlTransformer (priority 100) - Rewrites links and images
- tocTransformer (priority 200) - Extracts headings for TOC
- ssrTransformer (priority 50) - Pre-renders diagrams and math
Architecture
Configuration
The parser uses these Goldmark extensions:extension.GFM- GitHub Flavored Markdownmeta.Meta- YAML frontmatter parsinghighlighting.NewHighlighting- Syntax highlighting with Nord themepassthrough.New- LaTeX math passthroughadmonitions.Extender- Callout boxes
html.WithUnsafe()- Allows raw HTML in markdownparser.WithAutoHeadingID()- Auto-generates IDs from heading text
Related Packages
renderer- HTML template renderingrenderer/native- D2 and KaTeX native renderingcache- SSR artifact caching