Documentation
¶
Overview ¶
Package typst provides a Markdown-to-Typst converter and Typst PDF generator.
Index ¶
- func ConvertMarginToTypst(margin string, defaultVal string) string
- type Generator
- type GeneratorOption
- func WithAuthor(author string) GeneratorOption
- func WithDescription(description string) GeneratorOption
- func WithFontFamily(family string) GeneratorOption
- func WithFontSize(size string) GeneratorOption
- func WithLanguage(lang string) GeneratorOption
- func WithLineHeight(h float64) GeneratorOption
- func WithMargins(left, right, top, bottom string) GeneratorOption
- func WithPageSize(size string) GeneratorOption
- func WithRootDir(dir string) GeneratorOption
- func WithTimeout(d time.Duration) GeneratorOption
- func WithTitle(title string) GeneratorOption
- func WithVersion(version string) GeneratorOption
- type MarkdownToTypstConverter
- type TypstTemplateData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertMarginToTypst ¶
ConvertMarginToTypst converts a margin value (e.g., "20mm", "1in") to a Typst string. If the value is empty, returns a default value. The value is sanitized to prevent Typst code injection.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator converts HTML/Markdown content to PDF using Typst.
func NewGenerator ¶
func NewGenerator(opts ...GeneratorOption) *Generator
NewGenerator creates a Typst PDF generator.
type GeneratorOption ¶
type GeneratorOption func(*Generator)
GeneratorOption customizes a Typst generator.
func WithAuthor ¶
func WithAuthor(author string) GeneratorOption
WithAuthor sets the document author.
func WithDescription ¶ added in v0.8.0
func WithDescription(description string) GeneratorOption
WithDescription sets the document description, which Typst records as the PDF /Subject entry.
func WithFontFamily ¶
func WithFontFamily(family string) GeneratorOption
WithFontFamily sets the font family.
func WithLanguage ¶
func WithLanguage(lang string) GeneratorOption
WithLanguage sets the document language.
Typst's `text(lang: …)` accepts only an ISO 639 code (2-3 letters), so a BCP-47 tag such as "en-US" or "zh-CN" — what book.yaml carries, and what every mdpress scaffold writes — is reduced to its primary subtag. An unusable value leaves the generator's default in place rather than producing a document Typst refuses to compile.
func WithLineHeight ¶
func WithLineHeight(h float64) GeneratorOption
WithLineHeight sets the line height.
func WithMargins ¶
func WithMargins(left, right, top, bottom string) GeneratorOption
WithMargins sets page margins.
func WithPageSize ¶
func WithPageSize(size string) GeneratorOption
WithPageSize sets the page size (e.g., "A4", "Letter").
func WithRootDir ¶ added in v0.7.12
func WithRootDir(dir string) GeneratorOption
WithRootDir sets the Typst project root directory. When set, the generator writes the .typ file inside this directory and runs "typst compile --root <dir>" so that root-relative image paths (e.g. "/images/x.png") resolve against the book source tree rather than the OS temp directory.
func WithTimeout ¶
func WithTimeout(d time.Duration) GeneratorOption
WithTimeout sets the operation timeout.
func WithVersion ¶
func WithVersion(version string) GeneratorOption
WithVersion sets the document version.
type MarkdownToTypstConverter ¶
type MarkdownToTypstConverter struct {
}
MarkdownToTypstConverter converts Markdown syntax to Typst syntax. Typst syntax is quite close to Markdown, with some key differences:
- `# Heading` → `= Heading`
- `**bold**` → `*bold*`
- `*italic*` → `_italic_`
- “ `code` “ → “ `code` “
- ```code blocks``` → ``` ```code blocks``` ``` (block syntax)
- `` → `#image("img")`
- `[text](url)` → `#link("url")[text]`
func (*MarkdownToTypstConverter) Convert ¶
func (c *MarkdownToTypstConverter) Convert(markdown string) string
Convert takes Markdown text and converts it to Typst markup.
type TypstTemplateData ¶
type TypstTemplateData struct {
Title string
Subtitle string
Author string
Date string
Version string
Language string
Content string // The Typst-formatted body content
PageWidth string // e.g., "210mm" for A4
PageHeight string // e.g., "297mm"
MarginTop string
MarginRight string
MarginBottom string
MarginLeft string
FontFamily string
FontSize string
LineHeight float64
// Description becomes the PDF /Subject entry.
Description string
// BuildTime is the timestamp recorded as the PDF creation date. The zero
// value leaves Typst to stamp the wall clock.
BuildTime time.Time
// FontLine is the fully-rendered Typst "font: (...)" line (including a
// trailing newline) or empty to omit it. It is computed from FontFamily
// by renderTypstDocument so the template never interpolates a raw CSS
// font stack (which is invalid Typst).
FontLine string
// DocumentLine is the fully-rendered Typst "#set document(...)" call
// (including a trailing newline), or empty when there is no metadata to
// record. Typst maps it onto the PDF document information dictionary.
DocumentLine string
}
TypstTemplateData holds the data needed to render a Typst document.