Documentation
¶
Overview ¶
math.go implements pre/post processing for math formulas.
Problem: goldmark follows CommonMark spec where `_` inside words may be treated as emphasis delimiters, so `$x_1^2$` becomes `$x<em>1</em>^2$`, breaking the formula structure.
Solution: Before goldmark processes the Markdown source, replace $$...$$ and $...$ with placeholder tokens (e.g. MDPMATHBLOCK000000) that contain no Markdown special characters. After goldmark renders HTML, replace the placeholders back with HTML span elements that KaTeX auto-render can find.
Package markdown provides Markdown parsing and HTML conversion. Built on the goldmark library, it supports GFM extensions, syntax highlighting, footnotes, and more.
Core types:
- Parser: Markdown parser; call Parse() to get HTML and a heading list
- HeadingInfo: Heading metadata (level, text, ID), used for TOC generation
Usage example:
p := markdown.NewParser(markdown.WithCodeTheme("monokai"))
html, headings, err := p.Parse(source)
Package markdown provides Markdown parsing and HTML conversion. Built on the goldmark library, it supports GFM extensions, syntax highlighting, footnotes, and more.
postprocess.go performs post-processing on HTML emitted by goldmark. Includes: GFM Alert conversion ([!NOTE] etc.) and Mermaid code block conversion.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MermaidScript ¶
func MermaidScript() string
MermaidScript returns the <script> tags needed to load and initialize Mermaid. Only include this when the HTML contains .mermaid elements.
func NeedsMermaid ¶
NeedsMermaid reports whether the HTML contains any Mermaid diagram elements.
func PostProcess ¶
PostProcess applies all post-processing transforms to goldmark-rendered HTML.
Types ¶
type Diagnostic ¶
Diagnostic represents a document issue found during the build.
func CollectDiagnostics ¶
func CollectDiagnostics(document ast.Node, source []byte) []Diagnostic
CollectDiagnostics collects structured warnings from a Markdown document.
type HeadingInfo ¶
type HeadingInfo struct {
Level int // Heading level (1-6)
Text string // Heading text content
ID string // Heading ID, used for cross-references
Line int // Line number of the heading
Column int // Column number of the heading
}
HeadingInfo holds heading metadata, used for TOC generation.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is the Markdown parser.
func NewParser ¶
func NewParser(opts ...ParserOption) *Parser
NewParser creates and returns a new Markdown parser instance.
Example ¶
package main
import (
"fmt"
"github.com/yeasy/mdpress/internal/markdown"
)
func main() {
parser := markdown.NewParser()
html, headings, err := parser.Parse([]byte("# Hello\n\nWorld"))
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("HTML length:", len(html) > 0)
fmt.Println("Headings:", len(headings))
}
Output: HTML length: true Headings: 1
func (*Parser) GetHeadings ¶
func (p *Parser) GetHeadings() []HeadingInfo
GetHeadings returns all currently collected heading information.
func (*Parser) Parse ¶
func (p *Parser) Parse(source []byte) (string, []HeadingInfo, error)
Parse parses Markdown source and returns HTML and heading information.
func (*Parser) ParseWithDiagnostics ¶
func (p *Parser) ParseWithDiagnostics(source []byte) (string, []HeadingInfo, []Diagnostic, error)
ParseWithDiagnostics parses Markdown and also returns build-time warnings.
func (*Parser) SetCodeTheme ¶
SetCodeTheme sets the syntax highlighting theme.
type ParserOption ¶
type ParserOption func(*Parser)
ParserOption is a functional option type.
func WithCodeTheme ¶
func WithCodeTheme(theme string) ParserOption
WithCodeTheme is an option that sets the syntax highlighting theme.