Documentation
¶
Overview ¶
frontmatter.go strips the leading bytes that are not part of a document's prose: a UTF-8 byte order mark, and a YAML front matter block.
Neither was handled, and both are common in Markdown that came from another tool. A BOM stopped the first "# Heading" from being recognized as a heading at all — the line rendered as literal text and the chapter lost its title. Front matter was rendered verbatim, so "title: …" and "description: …" appeared in the page body, the search index and the PDF.
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 ¶
- Variables
- func HighlightCSSDark(codeTheme string) string
- func HighlightCSSLight(codeTheme string) string
- func NeedsMermaid(html string) bool
- func ProcessOutsideCode(md string, fn func(string) string) string
- func ProcessOutsideCodeSpans(text string, fn func(string) string) string
- type Diagnostic
- type FrontMatter
- type HeadingInfo
- type Parser
- type ParserOption
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.
func ProcessOutsideCode ¶ added in v0.8.0
ProcessOutsideCode applies fn to every part of a Markdown document that is not inside a fenced code block, leaving fenced content byte-for-byte intact.
Any transformation of Markdown source has to respect this boundary: a book that documents a tool will show that tool's own syntax inside fences, and rewriting it there corrupts the very thing the page is trying to display. (Inline `code` spans are a separate, narrower concern — see ProcessOutsideCodeSpans.)
Types ¶
type Diagnostic ¶
Diagnostic represents a document issue found during the build.
type FrontMatter ¶ added in v0.8.0
FrontMatter holds the fields mdpress understands from a front matter block. Unrecognized keys are ignored rather than rejected: front matter is a shared convention, and a file carrying fields for another tool must still build.
func StripLeadingMetadata ¶ added in v0.8.0
func StripLeadingMetadata(source []byte) ([]byte, FrontMatter)
StripLeadingMetadata removes a UTF-8 BOM and a leading YAML front matter block, returning the remaining source and whatever metadata was recognized.
Only a block that starts on the document's very first line counts, per the usual convention — a "---" later in the file is a thematic break.
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 WithAllowHTML ¶ added in v0.8.0
func WithAllowHTML(allow bool) ParserOption
WithAllowHTML controls whether raw HTML in the Markdown source is passed through to the output. It defaults to true: mdpress treats Markdown sources as trusted, and books rely on inline HTML for layout goldmark cannot express. Pass false for content mdpress did not author — raw HTML is not sanitized, so a <script> tag in a chapter runs in every reader's browser.
func WithCodeTheme ¶
func WithCodeTheme(theme string) ParserOption
WithCodeTheme is an option that sets the syntax highlighting theme.