1//! Re-export of the typst crate.
2pub(crate) mod well_known {
3pub use typst::foundations::Bytes;
45pub use typst::utils::LazyHash;
67/// Although this is not good to expose this, we make an alias here to
8 /// let it as a part of typst-ts.
9pub use typst::syntax::FileId as TypstFileId;
1011pub use typst::World as TypstWorld;
1213pub use typst::layout::Abs as TypstAbs;
1415pub use typst::Document as TypstDocumentTrait;
1617pub use typst::layout::PagedDocument as TypstPagedDocument;
1819pub use typst::html::HtmlDocument as TypstHtmlDocument;
2021pub use typst::text::Font as TypstFont;
2223pub use typst::foundations::Dict as TypstDict;
2425pub use typst::foundations::Datetime as TypstDatetime;
2627pub use typst::{diag, foundations, syntax};
28}
2930/// The enum of all well-known Typst documents.
31#[derive(Debug, Clone)]
32pub enum TypstDocument {
33/// The document compiled with `paged` target.
34Paged(Arc<well_known::TypstPagedDocument>),
35/// The document compiled with `html` target.
36Html(Arc<well_known::TypstHtmlDocument>),
37}
3839impl TypstDocument {
40/// Gets the number of pages in the document.
41pub fn num_of_pages(&self) -> u32 {
42match self {
43Self::Paged(doc) => doc.pages.len() as u32,
44Self::Html(_doc) => 1u32,
45 }
46 }
4748/// Gets details about the document.
49pub fn info(&self) -> &typst::model::DocumentInfo {
50match self {
51Self::Paged(doc) => &doc.info,
52Self::Html(doc) => &doc.info,
53 }
54 }
5556/// Gets the introspector that can be queried for elements and their
57 /// positions.
58pub fn introspector(&self) -> &typst::introspection::Introspector {
59match self {
60Self::Paged(doc) => &doc.introspector,
61Self::Html(doc) => &doc.introspector,
62 }
63 }
64}
6566impl From<Arc<well_known::TypstPagedDocument>> for TypstDocument {
67fn from(doc: Arc<well_known::TypstPagedDocument>) -> Self {
68Self::Paged(doc)
69 }
70}
7172impl From<Arc<well_known::TypstHtmlDocument>> for TypstDocument {
73fn from(doc: Arc<well_known::TypstHtmlDocument>) -> Self {
74Self::Html(doc)
75 }
76}
7778impl<'a> TryFrom<&'a TypstDocument> for &'a Arc<well_known::TypstPagedDocument> {
79type Error = crate::Error;
8081fn try_from(doc: &'a TypstDocument) -> Result<Self, Self::Error> {
82match doc {
83 TypstDocument::Paged(doc) => Ok(doc),
84 TypstDocument::Html(_doc) => {
85crate::bail!("The document is compiled with `html` target, not `paged`.")
86 }
87 }
88 }
89}
9091impl<'a> TryFrom<&'a TypstDocument> for &'a Arc<well_known::TypstHtmlDocument> {
92type Error = crate::Error;
9394fn try_from(doc: &'a TypstDocument) -> Result<Self, Self::Error> {
95match doc {
96 TypstDocument::Paged(_doc) => {
97crate::bail!("The document is compiled with `paged` target, not `html`.")
98 }
99 TypstDocument::Html(doc) => Ok(doc),
100 }
101 }
102}
103104use std::sync::Arc;
105106pub use well_known::*;
107108/// The prelude of the Typst module.
109pub mod prelude {
110pub use comemo::Prehashed;
111pub use ecow::{eco_format, eco_vec, EcoString, EcoVec};
112}