Skip to main content

oak_typst/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use oak_core::Range;
3
4/// Typst AST root node.
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct TypstRoot {
8    /// The byte range span of this root node in the source text.
9    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
10    pub span: Range<usize>,
11    /// The list of child items in this root node.
12    pub items: Vec<TypstItem>,
13}
14
15impl TypstRoot {
16    /// Creates a new TypstRoot with the given span.
17    pub fn new(span: Range<usize>) -> Self {
18        Self { span, items: Vec::new() }
19    }
20}
21
22/// A syntax item in a Typst document.
23#[derive(Debug, Clone)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub enum TypstItem {
26    /// Plain text content.
27    Text(String),
28    /// Whitespace between items.
29    Space,
30    /// Paragraph break.
31    Parbreak,
32    /// A heading with level and content.
33    Heading(TypstHeading),
34    /// Strong (bold) text content.
35    Strong(TypstRoot),
36    /// Emphasized (italic) text content.
37    Emphasis(TypstRoot),
38    /// Mathematical content.
39    Math(TypstRoot),
40    /// Quoted content.
41    Quote(TypstRoot),
42    /// A list item.
43    ListItem(TypstRoot),
44    /// An enumerated list item.
45    EnumItem(TypstRoot),
46    /// A hyperlink.
47    Link(TypstLink),
48    /// Raw (verbatim) text content.
49    Raw(String),
50    /// A block element.
51    Block(TypstRoot),
52}
53
54/// A heading in a Typst document.
55#[derive(Debug, Clone)]
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57pub struct TypstHeading {
58    /// The heading level (1-6).
59    pub level: usize,
60    /// The heading content.
61    pub content: TypstRoot,
62}
63
64/// A hyperlink in a Typst document.
65#[derive(Debug, Clone)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67pub struct TypstLink {
68    /// The URL of the link.
69    pub url: String,
70    /// Optional link content (display text).
71    pub content: Option<TypstRoot>,
72}