typst

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 11 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 CheckTypstAvailable

func CheckTypstAvailable() error

CheckTypstAvailable is a public function to check Typst availability.

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.

func CreateTypstDir

func CreateTypstDir(baseDir string) (string, error)

CreateTypstDir ensures the Typst working directory exists.

func CurrentDate

func CurrentDate() string

CurrentDate returns the current date as a formatted string.

func GetPageDimensions

func GetPageDimensions(pageSize string) (width, height string)

GetPageDimensions returns page width and height in mm as Typst format strings.

func MakeTypstFont

func MakeTypstFont(cssFontFamily string) string

MakeTypstFont converts a CSS font family string to a Typst font list. The input is sanitized to prevent Typst code injection.

func MakeTypstFontSize

func MakeTypstFontSize(cssFontSize string) string

MakeTypstFontSize converts a CSS font size (e.g., "12pt", "14px") to Typst format. The output is sanitized to prevent Typst code injection.

func PrepareTypstContent

func PrepareTypstContent(content string) string

PrepareTypstContent prepares the Typst content by escaping special characters if needed. This is a placeholder for future enhancements (e.g., escaping Typst-specific syntax).

func RenderTypstDocument

func RenderTypstDocument(data TypstTemplateData) (string, error)

RenderTypstDocument renders the template with the provided data.

func WriteTypstFile

func WriteTypstFile(filePath string, content string) error

WriteTypstFile writes Typst content to a file.

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