typst

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package typst provides a Markdown-to-Typst converter and Typst PDF generator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMarginToTypst

func ConvertMarginToTypst(margin string, defaultVal string) string

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.

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context, markdownContent string, outputPath string) error

Generate converts Markdown content to a PDF file via Typst. Canceling ctx kills the typst process and returns without writing a PDF; this generator implements the same shape as internal/pdf so `build --format typst` is as interruptible as `--format pdf`.

func (*Generator) GenerateFromFile

func (g *Generator) GenerateFromFile(ctx context.Context, markdownFilePath string, outputPath string) error

GenerateFromFile reads a Markdown file and generates a PDF.

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 WithFontSize

func WithFontSize(size string) GeneratorOption

WithFontSize sets the font size.

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 WithTitle

func WithTitle(title string) GeneratorOption

WithTitle sets the document title.

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)
  • `![alt](img)` → `#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.

Jump to

Keyboard shortcuts

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