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 提供 Markdown 解析和 HTML 转换功能。 基于 goldmark 库,支持 GFM 扩展、代码高亮、脚注等特性。
核心类型:
- Parser: Markdown 解析器,调用 Parse() 返回 HTML 和标题列表
- HeadingInfo: 标题信息(级别、文本、ID),用于目录生成
使用示例:
p := markdown.NewParser(markdown.WithCodeTheme("monokai"))
html, headings, err := p.Parse(source)
Package markdown 提供 Markdown 解析和 HTML 转换功能。 基于 goldmark 库,支持 GFM 扩展、代码高亮、脚注等特性。
postprocess.go performs post-processing on HTML emitted by goldmark. Includes: GFM Alert conversion ([!NOTE] etc.) and Mermaid code block conversion.
Index ¶
- func HasMath(md string) bool
- func KaTeXScript() string
- func KaTeXScriptForEpub() string
- func MermaidScript() string
- func NeedsKaTeX(html string) bool
- func NeedsMermaid(html string) bool
- func PostProcess(html string) string
- type Diagnostic
- type HeadingInfo
- type Parser
- type ParserOption
- type ProcessingResult
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasMath ¶ added in v0.3.0
HasMath reports whether the Markdown source contains any math formula syntax. Used as a quick check to decide whether math processing is needed.
func KaTeXScript ¶ added in v0.3.0
func KaTeXScript() string
KaTeXScript returns the HTML tags (link + scripts) needed to load KaTeX and its auto-render extension. The auto-render extension scans the document for $...$ and $$...$$ delimiters and renders them with KaTeX. Only include this when the HTML contains math elements (see NeedsKaTeX).
func KaTeXScriptForEpub ¶ added in v0.3.0
func KaTeXScriptForEpub() string
KaTeXScriptForEpub returns XHTML-compatible KaTeX script tags for use inside EPUB XHTML documents. Some EPUB readers (e.g. Apple Books) support JavaScript, so KaTeX can render math formulas in those readers.
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 NeedsKaTeX ¶ added in v0.3.0
NeedsKaTeX reports whether the HTML contains any math formula elements produced by the math preprocessor.
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 表示构建期间发现的文档问题。
func CollectDiagnostics ¶
func CollectDiagnostics(document ast.Node, source []byte) []Diagnostic
CollectDiagnostics 收集 Markdown 文档中的结构化 warning。
type HeadingInfo ¶
type HeadingInfo struct {
Level int // 标题等级 (1-6)
Text string // 标题文本内容
ID string // 标题 ID,用于交叉引用
Line int // 标题所在行
Column int // 标题所在列
}
HeadingInfo 标题信息结构体,用于目录生成
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser Markdown 解析器
func NewParser ¶
func NewParser(opts ...ParserOption) *Parser
NewParser 创建并返回一个新的 Markdown 解析器实例
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) Parse ¶
func (p *Parser) Parse(source []byte) (string, []HeadingInfo, error)
Parse 解析 Markdown 源代码,返回 HTML 和标题信息
func (*Parser) ParseWithDiagnostics ¶
func (p *Parser) ParseWithDiagnostics(source []byte) (string, []HeadingInfo, []Diagnostic, error)
ParseWithDiagnostics 解析 Markdown,并返回构建期 warning。
type ProcessingResult ¶
type ProcessingResult struct {
FilePath string
HTML string
Headings []HeadingInfo
Error error
}
ProcessingResult holds the result of processing a single Markdown document.