typst

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 14 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(markdownContent string, outputPath string) error

Generate converts Markdown content to a PDF file via Typst.

func (*Generator) GenerateFromFile

func (g *Generator) GenerateFromFile(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 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.

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 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
}

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