typst-as-lib 0.15.3

Small wrapper for typst that makes it easier to use it as a templating engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use typst::{
    diag::{FileError, FileResult},
    syntax::{FileId, Source},
};

pub(crate) fn not_found(id: FileId) -> FileError {
    FileError::NotFound(id.vpath().as_rootless_path().to_path_buf())
}

pub(crate) fn bytes_to_source(id: FileId, bytes: &[u8]) -> FileResult<Source> {
    // https://github.com/tfachmann/typst-as-library/blob/dd9a93379b486dc0a2916b956360db84b496822e/src/lib.rs#L78
    let contents = std::str::from_utf8(bytes).map_err(|_| FileError::InvalidUtf8)?;
    let contents = contents.trim_start_matches('\u{feff}');
    Ok(Source::new(id, contents.to_owned()))
}