fix(typlite): highlight in docx export (#1798)
Some checks are pending
tinymist::ci / Duplicate Actions Detection (push) Waiting to run
tinymist::ci / Check Clippy, Formatting, Completion, Documentation, and Tests (Linux) (push) Waiting to run
tinymist::ci / Check Minimum Rust version and Tests (Windows) (push) Waiting to run
tinymist::ci / E2E Tests (darwin-arm64 on macos-latest) (push) Blocked by required conditions
tinymist::ci / E2E Tests (linux-x64 on ubuntu-22.04) (push) Blocked by required conditions
tinymist::ci / E2E Tests (linux-x64 on ubuntu-latest) (push) Blocked by required conditions
tinymist::ci / E2E Tests (win32-x64 on windows-2019) (push) Blocked by required conditions
tinymist::ci / prepare-build (push) Waiting to run
tinymist::ci / build-vscode (push) Blocked by required conditions
tinymist::ci / build-vscode-others (push) Blocked by required conditions
tinymist::ci / publish-vscode (push) Blocked by required conditions
tinymist::ci / E2E Tests (win32-x64 on windows-latest) (push) Blocked by required conditions
tinymist::ci / build-binary (push) Blocked by required conditions
tinymist::ci / build-vsc-assets (push) Blocked by required conditions
tinymist::gh_pages / build-gh-pages (push) Waiting to run

* fix: highlight in docx export

* fmt
This commit is contained in:
Hong Jiarong 2025-06-06 23:01:33 +08:00 committed by GitHub
parent 51db97ffcc
commit ae99016cd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 78 additions and 8 deletions

View file

@ -0,0 +1 @@
#highlight[highlight]

View file

@ -0,0 +1,17 @@
---
source: crates/typlite/src/tests.rs
expression: "conv(world, ConvKind::Md { for_docs: false })"
input_file: crates/typlite/src/fixtures/integration/highlight.typ
snapshot_kind: text
---
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><m1document><span><m1highlight>highlight</m1highlight></span></m1document></body>
</html>
=====
==highlight==

View file

@ -0,0 +1,22 @@
---
source: crates/typlite/src/tests.rs
expression: "conv(world, ConvKind::LaTeX)"
input_file: crates/typlite/src/fixtures/integration/highlight.typ
snapshot_kind: text
---
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><m1document><span><m1highlight>highlight</m1highlight></span></m1document></body>
</html>
=====
\begin{document}
\colorbox{yellow}{highlight}
\end{document}

View file

@ -0,0 +1,7 @@
---
source: crates/typlite/src/tests.rs
expression: hash
input_file: crates/typlite/src/fixtures/integration/highlight.typ
snapshot_kind: text
---
siphash128_13:6e6d759532b382b4f775e85f9599d60d

View file

@ -7,7 +7,7 @@ use ecow::EcoString;
use std::fs;
use std::io::Cursor;
use crate::common::{CenterNode, FigureNode, FormatWriter};
use crate::common::{CenterNode, FigureNode, FormatWriter, HighlightNode};
use crate::Result;
use super::image_processor::DocxImageProcessor;
@ -199,12 +199,7 @@ impl DocxWriter {
}
Node::HtmlElement(element) => {
// Handle special HTML elements
if element.tag == "mark" {
run = run.style("Highlight");
for child in &element.children {
run = self.process_inline_to_run(run, child)?;
}
} else if element.tag == "img" && element.self_closing {
if element.tag == "img" && element.self_closing {
let is_typst_block = element
.attributes
.iter()
@ -240,8 +235,21 @@ impl DocxWriter {
Node::SoftBreak => {
run = run.add_text(" ");
}
Node::Custom(custom_node) => {
if let Some(highlight_node) = custom_node.as_any().downcast_ref::<HighlightNode>() {
run = run.highlight("yellow");
for child in &highlight_node.content {
run = self.process_inline_to_run(run, child)?;
}
} else {
// Handle other custom inline nodes if needed
println!("Unhandled custom inline node: {:?}", custom_node);
}
}
// Other inline element types
_ => {}
_ => {
println!("other inline element: {:?}", node);
}
}
Ok(run)
@ -455,6 +463,21 @@ impl DocxWriter {
Some(&external_frame.alt_text),
None,
);
} else if let Some(highlight_node) =
custom_node.as_any().downcast_ref::<HighlightNode>()
{
// Handle HighlightNode at block level (convert to paragraph)
let mut para = Paragraph::new();
let mut run = Run::new().highlight("yellow");
for child in &highlight_node.content {
run = self.process_inline_to_run(run, child)?;
}
if !run.children.is_empty() {
para = para.add_run(run);
docx = docx.add_paragraph(para);
}
} else {
// Fallback for unknown custom nodes - ignore or add placeholder
let placeholder = "[Unknown custom content]";