feat: import_context and contexual convert (#1816)

* feat: impl import_context

* dev: fmt, clippy

* fix: update import path handling to use rootless paths and normalize separators

* feat: fix errors for fletcher v0.5.8

---------

Co-authored-by: Myriad-Dreamin <camiyoru@gmail.com>
This commit is contained in:
Hong Jiarong 2025-06-16 16:13:40 +08:00 committed by GitHub
parent 9d9d360db9
commit a24c516148
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 74 additions and 11 deletions

View file

@ -159,6 +159,8 @@ pub struct TypliteFeat {
pub remove_html: bool,
/// The target to convert
pub target: Format,
/// Import context for code examples (e.g., "#import \"/path/to/file.typ\": *")
pub import_context: Option<String>,
}
/// Task builder for converting a typst document to Markdown.

View file

@ -256,16 +256,23 @@ impl HtmlToAstParser {
const PRELUDE: &str = r##"#set page(width: auto, height: auto, margin: (y: 0.45em, rest: 0em), fill: none);
#set text(fill: rgb("#c0caf5")) if sys.inputs.at("x-color-theme", default: none) == "dark";"##;
let import_prefix = if let Some(ref import_ctx) = self.feat.import_context {
format!("{}\n", import_ctx)
} else {
String::new()
};
world
.map_shadow_by_id(
main,
Bytes::from_string(match mode.as_str() {
"code" => eco_format!("{PRELUDE}#{{{src}}}"),
"math" => eco_format!("{PRELUDE}${src}$"),
"markup" => eco_format!("{PRELUDE}#[{}]", src),
"code" => eco_format!("{}{PRELUDE}#{{{src}}}", import_prefix),
"math" => eco_format!("{}{PRELUDE}${src}$", import_prefix),
"markup" => eco_format!("{}{PRELUDE}#[{}]", import_prefix, src),
// todo check mode
// "markup" |
_ => eco_format!("{PRELUDE}#[{}]", src),
_ => eco_format!("{}{PRELUDE}#[{}]", import_prefix, src),
}),
)
.unwrap();