Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ cargo install --git https://github.com/sou1118/typdiff
## Usage

```
typdiff <OLD> <NEW> [-o <OUTPUT>]
typdiff <OLD> <NEW> [-o <OUTPUT>] [--no-preamble]
```

| Argument | Description |
|---|---|
| `<OLD>` | Path to the old Typst file |
| `<NEW>` | Path to the new Typst file |
| `-o, --output <OUTPUT>` | Output file path (defaults to stdout) |
| `--no-preamble` | Omit the definitions of `diff-added` and `diff-deleted`, so the output can import its own |

### Examples

Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ struct Cli {
/// Output file path (defaults to stdout)
#[arg(short, long)]
output: Option<String>,

/// Omit the definitions of diff-added and diff-deleted from the output
#[arg(long)]
no_preamble: bool,
}

fn main() -> Result<()> {
Expand All @@ -39,7 +43,11 @@ fn main() -> Result<()> {

let diff_results = typdiff::diff::diff(&old_blocks, &new_blocks);

let output = typdiff::render::render(&diff_results);
let output = if cli.no_preamble {
typdiff::render::render_body(&diff_results)
} else {
typdiff::render::render(&diff_results)
};

match cli.output {
Some(path) => {
Expand Down
8 changes: 6 additions & 2 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ const PREAMBLE: &str = r##"#let diff-added(body) = {

/// Render a list of diff results into a compilable Typst source string.
pub fn render(results: &[DiffResult]) -> String {
format!("{PREAMBLE}\n{}", render_body(results))
}

/// Render diff results without the preamble that defines `diff-added` and
/// `diff-deleted`. The caller must make those functions available.
pub fn render_body(results: &[DiffResult]) -> String {
let mut out = String::new();
out.push_str(PREAMBLE);
out.push('\n');

for result in results {
match result {
Expand Down
14 changes: 13 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use typdiff::diff::diff;
use typdiff::parse::parse;
use typdiff::render::render;
use typdiff::render::{render, render_body};

fn run_diff(old: &str, new: &str) -> String {
let old_blocks: Vec<_> = parse(old)
Expand Down Expand Up @@ -209,3 +209,15 @@ fn test_indented_line_comment_does_not_split_paragraph() {
// A whitespace-only line would be treated as a paragraph break by Typst.
assert!(!output.contains("\n \n"));
}

#[test]
fn test_render_body_omits_preamble() {
let old_blocks = parse("Old text.\n");
let new_blocks = parse("New text.\n");
let output = render_body(&diff(&old_blocks, &new_blocks));

// The caller imports its own definitions, which a preamble would shadow.
assert!(!output.contains("#let diff-added"));
assert!(!output.contains("#let diff-deleted"));
assert!(output.contains("#diff-added[New]"));
}