deck

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: BSD-3-Clause Imports: 27 Imported by: 0

Documentation

Overview

Package deck parses a Marp slide deck: Markdown split into slides, with Marpit's directives, image syntax, fragmented lists, speaker notes and scoped styles, plus this tool's two additions — Go template interpolation (package interp) and slide kinds (see Kind).

The behaviour follows the Marpit and Marp Core sources rather than their documentation where the two differ; the tests pin each case against output produced by marp-cli 4.5.1 (Marp Core 4.4.0).

Index

Constants

View Source
const KindDirective = "kind"

KindDirective is this tool's own local directive: it names a kind, a preset of local directives (see Kind).

Variables

View Source
var KindMathBlock = gast.NewNodeKind("MathBlock")

KindMathBlock is the node kind of MathBlock.

View Source
var KindMathInline = gast.NewNodeKind("MathInline")

KindMathInline is the node kind of MathInline.

Functions

func ScopeAttr

func ScopeAttr(s *Slide) string

ScopeAttr returns the attribute a slide's scoped styles select on.

Types

type Background

type Background struct {
	URL    string
	Alt    string
	Size   string
	Filter string
}

Background is a background image.

type Deck

type Deck struct {
	// Global holds the global directives, last value written winning.
	Global Global
	// Slides are the slides in order.
	Slides []*Slide
	// Styles are the deck's global CSS additions in order: the style
	// directive first, then every unscoped <style> element.
	Styles []string
	// Front is the front matter decoded with YAML's core schema, for
	// templates and tools; directives come from Global and Slide.Dirs.
	Front map[string]any
	// Kinds are the kinds the deck could use.
	Kinds Kinds
	// contains filtered or unexported fields
}

Deck is a parsed slide deck.

func Parse

func Parse(src []byte, opts Options) (*Deck, error)

Parse parses a deck.

func (*Deck) Markdown

func (d *Deck) Markdown() (string, error)

Markdown returns the deck as plain Marp Markdown: templates expanded, page numbers written out, and kinds replaced by the directives they stand for, so that marp-cli or any Marp tool renders the same slides.

Kinds are removed from the front matter and from comments, then the result is parsed again the way Marpit would see it; wherever a slide's directives differ from what this deck gives it, a spot-directive comment at the start of the slide restores them.

func (*Deck) Render

func (d *Deck) Render(opts RenderOptions) []Rendered

Render renders every slide.

type Dirs

type Dirs struct {
	// contains filtered or unexported fields
}

Dirs is an ordered set of directive values. Marpit writes them out as data-* attributes and CSS custom properties in the order they were set.

func (Dirs) Clone

func (d Dirs) Clone() Dirs

Clone copies d.

func (*Dirs) Delete

func (d *Dirs) Delete(name string)

Delete removes a directive.

func (*Dirs) Get

func (d *Dirs) Get(name string) string

Get returns a directive's value.

func (*Dirs) Has

func (d *Dirs) Has(name string) bool

Has reports whether a directive is set, even to the empty string.

func (*Dirs) Keys

func (d *Dirs) Keys() []string

Keys returns the directive names in the order they were first set.

func (*Dirs) Merge

func (d *Dirs) Merge(o Dirs)

Merge sets every directive of o on d, in o's order.

func (*Dirs) Set

func (d *Dirs) Set(name, value string)

Set sets a directive, keeping its first position.

type Frame

type Frame int

Frame is how a slide is wrapped.

const (
	// FrameSVG wraps each slide in <svg><foreignObject>, Marp Core's
	// inline SVG mode: the slide scales with the viewport, and split
	// backgrounds are separate layers.
	FrameSVG Frame = iota
	// FramePlain writes bare <section> elements (Marpit without inline
	// SVG): split and multiple backgrounds collapse to the last image.
	FramePlain
	// FrameBox renders the inline SVG layers as absolutely positioned
	// <div>s, for layout engines without <foreignObject>.
	FrameBox
)

type Global

type Global struct {
	Dirs Dirs
	// HeadingDivider lists the heading levels that start a new slide.
	HeadingDivider []int
}

Global holds the global directives.

func (Global) Get

func (g Global) Get(name string) string

Get returns a global directive.

func (Global) Theme

func (g Global) Theme() string

Theme returns the theme directive.

type HTMLPolicy

type HTMLPolicy int

HTMLPolicy says what raw HTML in the Markdown may do.

const (
	// HTMLAllowlist keeps the elements and attributes Marp Core considers
	// safe and escapes the rest, as Marp Core does by default.
	HTMLAllowlist HTMLPolicy = iota
	// HTMLAll keeps all raw HTML (marp --html).
	HTMLAll
	// HTMLNone escapes all raw HTML.
	HTMLNone
)

type Heading

type Heading struct {
	Level int
	Text  string
	ID    string
}

Heading is a heading of a slide.

type Kind

type Kind struct {
	Name string
	// Extends names a kind whose directives this one starts from.
	Extends string
	// Dirs are the kind's own directives, already normalized.
	Dirs Dirs
}

A Kind is a named preset of local directives: the header, the footer, whether the slide is numbered, its class, its background. A slide states what it is ("kind: title") instead of repeating how it looks.

"kind: name" applies the preset like a local directive, to the slide and the ones after it; "_kind: name" applies it to one slide, like a spot directive. Switching kinds first clears what the previous kind set, so a class or a footer never leaks from one kind into the next. Directives written by hand always win over the kind, and, being the author's own, carry over to the following slides as in Marp, kind switches included.

type Kinds

type Kinds map[string]*Kind

Kinds is a set of kinds by name.

func BuiltinKinds

func BuiltinKinds() Kinds

BuiltinKinds returns the kinds every deck starts with. A deck, a configuration file or a kinds file can redefine any of them.

func LoadKindsFile

func LoadKindsFile(path string) (Kinds, error)

LoadKindsFile reads kinds from a YAML file: either a mapping of kinds, or a mapping with a kinds key (so a configuration file can serve as one).

func ParseKinds

func ParseKinds(v any) (Kinds, error)

ParseKinds reads kind definitions from a decoded YAML mapping of name to directives.

func ParseKindsYAML

func ParseKindsYAML(src []byte, name string) (Kinds, error)

ParseKindsYAML reads kinds from YAML source; name is for errors.

func (Kinds) Merge

func (k Kinds) Merge(o Kinds) Kinds

Merge returns k overridden by o.

func (Kinds) Names

func (k Kinds) Names() []string

Names returns the kind names, sorted.

func (Kinds) Resolve

func (k Kinds) Resolve(name string) (Dirs, error)

Resolve returns a kind's directives with its extends chain applied.

type MathBlock added in v0.2.0

type MathBlock struct {
	gast.BaseBlock
	Tex string
}

MathBlock is a display formula; Tex is its source.

func (*MathBlock) Dump added in v0.2.0

func (n *MathBlock) Dump(source []byte, level int)

Dump implements ast.Node.

func (*MathBlock) IsRaw added in v0.2.0

func (n *MathBlock) IsRaw() bool

IsRaw implements ast.Node.

func (*MathBlock) Kind added in v0.2.0

func (n *MathBlock) Kind() gast.NodeKind

Kind implements ast.Node.

type MathInline added in v0.2.0

type MathInline struct {
	gast.BaseInline
	Tex string
}

MathInline is an inline formula; Tex is its source.

func (*MathInline) Dump added in v0.2.0

func (n *MathInline) Dump(source []byte, level int)

Dump implements ast.Node.

func (*MathInline) Kind added in v0.2.0

func (n *MathInline) Kind() gast.NodeKind

Kind implements ast.Node.

type Options

type Options struct {
	// Data is template data; it overrides the front matter's keys and its
	// data mapping.
	Data map[string]any
	// Kinds add to (and override) the built-in kinds; the deck's own
	// kinds front-matter key overrides these in turn.
	Kinds Kinds
	// NoTemplate turns template interpolation off.
	NoTemplate bool
	// BaseDir resolves relative paths: included files and kinds files.
	BaseDir string
	// StrictYAML turns off loose YAML parsing of directives (Marp Core
	// turns it on; plain Marpit leaves it off).
	StrictYAML bool
	// Now is the template clock; nil means time.Now.
	Now func() time.Time
	// Name names the deck in error messages.
	Name string
}

Options configures Parse.

type RenderOptions

type RenderOptions struct {
	Frame Frame
	HTML  HTMLPolicy
	// Lang is the lang attribute when no lang directive sets one.
	Lang string
	// Width and Height are the slide size in pixels, from the theme.
	Width, Height int
	// Highlight, when set, renders a fenced code block's content; ok false
	// falls back to plain escaped code.
	Highlight func(code, lang string) (htmlOut string, ok bool)
	// GeneratedContent, when set, writes elements that stand for the
	// ::after pseudo-element Marpit paginates with, for layout engines
	// without generated content: <span data-slides-pseudo="after">.
	GeneratedContent bool
	// Math, when set, typesets a formula ($…$ inline, $$…$$ display) to
	// HTML; nil leaves formulas as the text they were written as.
	Math func(tex string, display bool) (string, error)
	// AfterContent is the theme's section::after content expression (see
	// theme.Packed.After); the element writes its value. Empty means the
	// page number alone, Marpit's scaffold.
	AfterContent string
}

RenderOptions configures Render.

type Rendered

type Rendered struct {
	Slide *Slide
	// HTML is the slide's markup, frame included.
	HTML string
}

Rendered is a rendered slide.

type Slide

type Slide struct {
	// Index is the slide's position, from 0.
	Index int
	// Kind is the kind the slide was given, "" if none.
	Kind string
	// Dirs are the local and spot directives in effect on the slide.
	Dirs Dirs
	// Page is the page number as Marpit counts it: slides whose paginate
	// directive is skip or hold do not advance it.
	Page int
	// Total is the last page number of the deck.
	Total int
	// Paginated reports whether the page number is shown.
	Paginated bool
	// Notes are the speaker notes: the slide's HTML comments that are not
	// directives.
	Notes []string
	// Fragments counts the fragmented list items.
	Fragments int
	// Backgrounds are the slide's background images, in order.
	Backgrounds []Background
	// BackgroundSplit is "left" or "right" when the backgrounds take one
	// side of the slide, and BackgroundSplitSize the width they take.
	BackgroundSplit, BackgroundSplitSize string
	// BackgroundDirection is "horizontal" (the default) or "vertical".
	BackgroundDirection string
	// ScopedStyles are the slide's <style scoped> contents.
	ScopedStyles []string
	// Headings are the slide's headings, for outlines.
	Headings []Heading
	// contains filtered or unexported fields
}

Slide is one slide.

func (*Slide) Paginate

func (s *Slide) Paginate() string

Paginate returns the paginate directive in effect: "true", "false", "hold" or "skip".

Jump to

Keyboard shortcuts

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