Documentation
¶
Overview ¶
highlight_css.go generates class-based chroma stylesheets for syntax highlighting. The parser renders code blocks with CSS classes instead of inline styles (see parser.go), so each output format embeds these light and dark stylesheets and code stays readable in both color modes.
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
Code blocks are highlighted with CSS classes (chroma), so renderers must embed the stylesheets from HighlightCSSLight/HighlightCSSDark for token colors to appear; the dark stylesheet only applies under DarkModeSelectors.
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 ¶
var DarkModeSelectors = []string{`html[data-theme="dark"]`, `html.dark`}
DarkModeSelectors lists the root selectors under which dark-mode highlight rules apply. They must stay in sync with the renderers: the standalone HTML renderer marks dark mode with data-theme="dark" on <html>, and the site template toggles the "dark" class on <html>. Renderer CSS that needs to override highlight rules in dark mode should use the same prefixes so specificity stays predictable.
Functions ¶
func HighlightCSSDark ¶ added in v0.7.14
HighlightCSSDark returns the dark-mode syntax-highlighting stylesheet for the given chroma style name, generated from its dark counterpart style with every rule prefixed by DarkModeSelectors so it only applies in dark mode.
A catch-all token rule is prepended: chroma styles only emit rules for the token classes they color, so a token styled by the LIGHT stylesheet but not by the dark one (e.g. github styles Name ink-dark, github-dark leaves it unstyled) would otherwise keep its light ink on the dark background. The catch-all (prefix .chroma span) outranks the light token rules (0,2,0) but yields to the dark style's own class rules (0,3,1).
func HighlightCSSLight ¶ added in v0.7.14
HighlightCSSLight returns the light-mode syntax-highlighting stylesheet for the given chroma style name (theme.CodeTheme), scoped to .chroma. Unknown style names fall back to defaultCodeTheme.
func NeedsMermaid ¶
NeedsMermaid reports whether the HTML contains any Mermaid diagram elements.
Types ¶
type Diagnostic ¶
Diagnostic represents a document issue found during the build.
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) 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.