config

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config loads and validates mdpress configuration. It reads book metadata, chapter definitions, style settings, and output options from book.yaml.

discover.go implements zero-config project discovery. When neither book.yaml nor SUMMARY.md exists, mdpress scans .md files, sorts them, and derives chapter metadata automatically.

summary.go parses chapter structure from SUMMARY.md. SUMMARY.md uses Markdown link lists to define chapter order in a GitBook-compatible format.

格式示例:

# Summary

* [前言](preface.md)
* [第一章](chapter01/README.md)
  * [1.1 小节](chapter01/section01.md)
* [第二章](chapter02/README.md)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BookConfig

type BookConfig struct {
	Book     BookMeta     `yaml:"book"`
	Chapters []ChapterDef `yaml:"chapters"`
	Style    StyleConfig  `yaml:"style"`
	Output   OutputConfig `yaml:"output"`
	// Plugins lists the plugins to run during the build, in declaration order.
	Plugins []PluginConfig `yaml:"plugins"`

	// These fields are auto-detected by Load instead of being set directly in YAML.
	GlossaryFile string `yaml:"-"` // Path to GLOSSARY.md, if present.
	LangsFile    string `yaml:"-"` // Path to LANGS.md, if present.
	// contains filtered or unexported fields
}

BookConfig is the top-level configuration for a book.

func DefaultConfig

func DefaultConfig() *BookConfig

DefaultConfig returns a config populated with reasonable defaults.

func Discover

func Discover(dir string) (*BookConfig, error)

Discover auto-discovers project configuration in a directory. Priority: book.yaml > SUMMARY.md > Markdown file scanning.

func Load

func Load(path string) (*BookConfig, error)

Load reads a config file from disk. If chapters are empty, it attempts to load them from SUMMARY.md in the same directory. It also auto-detects GLOSSARY.md and LANGS.md.

func (*BookConfig) BaseDir

func (c *BookConfig) BaseDir() string

BaseDir returns the directory containing the config file.

func (*BookConfig) ResolvePath

func (c *BookConfig) ResolvePath(p string) string

ResolvePath resolves a path relative to the config directory.

func (*BookConfig) SetBaseDir

func (c *BookConfig) SetBaseDir(dir string)

SetBaseDir overrides the base directory used to resolve relative paths. It is primarily useful for tests and for constructing configs in memory.

func (*BookConfig) Validate

func (c *BookConfig) Validate() error

Validate checks the configuration for completeness and validity.

type BookMeta

type BookMeta struct {
	Title       string    `yaml:"title"`
	Subtitle    string    `yaml:"subtitle"`
	Author      string    `yaml:"author"`
	Version     string    `yaml:"version"`
	Language    string    `yaml:"language"`
	Description string    `yaml:"description"`
	Cover       CoverMeta `yaml:"cover"`
}

BookMeta contains book metadata.

type ChapterDef

type ChapterDef struct {
	Title    string       `yaml:"title"`
	File     string       `yaml:"file"`
	Sections []ChapterDef `yaml:"sections"`
}

ChapterDef defines a chapter and its nested sections.

func FlattenChapters

func FlattenChapters(chapters []ChapterDef) []ChapterDef

FlattenChapters expands nested chapter definitions into a flat list. This is the canonical implementation; callers should use this instead of maintaining their own flattening logic.

func ParseSummary

func ParseSummary(path string) ([]ChapterDef, error)

ParseSummary parses chapter definitions from SUMMARY.md. Nesting is expressed with indentation: two spaces or one tab per level.

type CoverMeta

type CoverMeta struct {
	Image      string `yaml:"image"`
	Background string `yaml:"background"` // Background color, for example "#1a1a2e".
}

CoverMeta stores cover configuration.

type DiscoverError

type DiscoverError struct {
	Dir string
	Msg string
}

DiscoverError describes auto-discovery failures.

func (*DiscoverError) Error

func (e *DiscoverError) Error() string

type HeaderFooterStyle

type HeaderFooterStyle struct {
	Left   string `yaml:"left"`
	Center string `yaml:"center"`
	Right  string `yaml:"right"`
}

HeaderFooterStyle stores header and footer text templates.

type MarginConfig

type MarginConfig struct {
	Top    float64 `yaml:"top"`
	Bottom float64 `yaml:"bottom"`
	Left   float64 `yaml:"left"`
	Right  float64 `yaml:"right"`
}

MarginConfig stores page margins in millimeters.

type OutputConfig

type OutputConfig struct {
	Filename   string   `yaml:"filename"`
	TOC        bool     `yaml:"toc"`
	Cover      bool     `yaml:"cover"`
	Header     bool     `yaml:"header"`
	Footer     bool     `yaml:"footer"`
	Formats    []string `yaml:"formats"`     // Output formats: pdf, html, epub, site (default ["pdf"]).
	PDFTimeout int      `yaml:"pdf_timeout"` // PDF generation timeout in seconds (default 120).
}

OutputConfig stores output-related settings.

type PluginConfig added in v0.3.0

type PluginConfig struct {
	// Name is the unique plugin identifier (lowercase, hyphen-separated).
	Name string `yaml:"name"`
	// Path is the path to the plugin executable, relative to book.yaml.
	Path string `yaml:"path"`
	// Config contains arbitrary key-value pairs passed to the plugin.
	Config map[string]interface{} `yaml:"config"`
}

PluginConfig describes a single plugin entry in book.yaml.

Example:

plugins:
  - name: word-count
    path: ./plugins/word-count
    config:
      warn_threshold: 500

type StyleConfig

type StyleConfig struct {
	Theme      string            `yaml:"theme"`
	PageSize   string            `yaml:"page_size"`
	FontFamily string            `yaml:"font_family"`
	FontSize   string            `yaml:"font_size"`
	CodeTheme  string            `yaml:"code_theme"`
	LineHeight float64           `yaml:"line_height"`
	Margin     MarginConfig      `yaml:"margin"`
	Header     HeaderFooterStyle `yaml:"header"`
	Footer     HeaderFooterStyle `yaml:"footer"`
	CustomCSS  string            `yaml:"custom_css"`
}

StyleConfig stores style-related settings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL