anytomd 1.2.2

Pure Rust library that converts various document formats into Markdown
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#![warn(missing_docs)]
//! # anytomd
//!
//! A pure Rust library that converts various document formats into Markdown,
//! designed for LLM consumption.
//!
//! # Supported Formats
//!
//! | Format | Extensions |
//! |--------|-----------|
//! | DOCX | `.docx` |
//! | PPTX | `.pptx` |
//! | XLSX | `.xlsx` |
//! | XLS | `.xls` |
//! | HTML | `.html`, `.htm` |
//! | CSV | `.csv` |
//! | Jupyter Notebook | `.ipynb` |
//! | JSON | `.json` |
//! | XML | `.xml` |
//! | Images | `.png`, `.jpg`, `.gif`, `.webp`, `.bmp`, `.tiff`, `.svg`, `.heic`, `.avif` |
//! | Code | `.py`, `.rs`, `.js`, `.ts`, `.c`, `.cpp`, `.go`, `.java`, and more |
//! | Plain Text | `.txt`, `.md`, `.rst`, `.log`, `.toml`, `.yaml`, `.ini`, etc. |
//!
//! # Quick Start
//!
//! ```no_run
//! use anytomd::{convert_bytes, ConversionOptions};
//!
//! // Convert raw bytes with an explicit format
//! let options = ConversionOptions::default();
//! let csv_data = b"Name,Age\nAlice,30\nBob,25";
//! let result = convert_bytes(csv_data, "csv", &options).unwrap();
//! println!("{}", result.markdown);
//! ```
//!
//! On native targets, file-based conversion is also available:
//!
//! ```no_run
//! # #[cfg(not(target_arch = "wasm32"))]
//! # {
//! use anytomd::{convert_file, ConversionOptions};
//!
//! let options = ConversionOptions::default();
//! let result = convert_file("document.docx", &options).unwrap();
//! println!("{}", result.markdown);
//! # }
//! ```
//!
//! # Feature Flags
//!
//! | Feature | Description |
//! |---------|-------------|
//! | `async` | Async API: `convert_file_async`, `convert_bytes_async`, `AsyncImageDescriber` |
//! | `async-gemini` | `AsyncGeminiDescriber` for concurrent Gemini API calls |
//! | `wasm` | WebAssembly bindings via `wasm-bindgen` (`convertBytes`, `convertBytesWithOptions`) |
//! | `wasm` + `async-gemini` | Adds `convertBytesWithGemini` for async Gemini-powered conversion in WASM |

pub mod converter;
pub mod detection;
pub mod error;
pub mod markdown;
pub(crate) mod zip_utils;

#[cfg(feature = "async")]
pub use converter::{AsyncConversionOptions, AsyncImageDescriber};
pub use converter::{
    ConversionOptions, ConversionResult, ConversionWarning, Converter, ImageDescriber, WarningCode,
};
pub use error::ConvertError;

/// Built-in Google Gemini image description providers.
///
/// Contains `GeminiDescriber` (sync, native-only — not available on WASM)
/// and `AsyncGeminiDescriber` (behind the `async-gemini` feature).
pub mod gemini {
    #[cfg(not(target_arch = "wasm32"))]
    pub use crate::converter::gemini::GeminiDescriber;

    #[cfg(feature = "async-gemini")]
    pub use crate::converter::gemini::AsyncGeminiDescriber;
}

#[cfg(feature = "wasm")]
mod wasm;

#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;

/// Convert a file at the given path to Markdown.
///
/// The format is auto-detected from magic bytes and file extension.
///
/// Not available on WASM targets — use [`convert_bytes`] instead.
#[cfg(not(target_arch = "wasm32"))]
pub fn convert_file(
    path: impl AsRef<Path>,
    options: &ConversionOptions,
) -> Result<ConversionResult, ConvertError> {
    let path = path.as_ref();
    let size = std::fs::metadata(path)?.len() as usize;
    if size > options.max_input_bytes {
        return Err(ConvertError::InputTooLarge {
            size,
            limit: options.max_input_bytes,
        });
    }

    let data = std::fs::read(path)?;

    if data.len() > options.max_input_bytes {
        return Err(ConvertError::InputTooLarge {
            size: data.len(),
            limit: options.max_input_bytes,
        });
    }

    let format = detection::detect_format(path, &data);

    // For ZIP-based formats, introspect to find the specific type
    let (format, is_zip_magic) = match format {
        Some("zip") => (detection::detect_zip_format(&data), true),
        other => (other, false),
    };

    let extension = match format {
        // Code files: pass through the original extension for language detection
        Some("code") => path.extension().and_then(|e| e.to_str()).unwrap_or("code"),
        Some(fmt) => fmt,
        None if is_zip_magic => {
            // ZIP magic bytes detected but not a known OOXML format — reject
            return Err(ConvertError::UnsupportedFormat {
                extension: "zip".to_string(),
            });
        }
        None => path.extension().and_then(|e| e.to_str()).unwrap_or(""),
    };

    convert_bytes(&data, extension, options)
}

/// Convert raw bytes to Markdown with an explicit format extension.
pub fn convert_bytes(
    data: &[u8],
    extension: &str,
    options: &ConversionOptions,
) -> Result<ConversionResult, ConvertError> {
    let extension_norm = normalize_extension(extension);

    if data.len() > options.max_input_bytes {
        return Err(ConvertError::InputTooLarge {
            size: data.len(),
            limit: options.max_input_bytes,
        });
    }

    if extension_norm == "pdf" {
        return Err(ConvertError::FormatNotSupported {
            extension: "pdf".to_string(),
            reason: "PDF is intentionally unsupported — Gemini, ChatGPT, and Claude \
                     handle PDF natively"
                .to_string(),
        });
    }

    use converter::code::CodeConverter;
    use converter::csv::CsvConverter;
    use converter::docx::DocxConverter;
    use converter::html::HtmlConverter;
    use converter::image::ImageConverter;
    use converter::ipynb::IpynbConverter;
    use converter::json::JsonConverter;
    use converter::plain_text::PlainTextConverter;
    use converter::pptx::PptxConverter;
    use converter::xlsx::XlsxConverter;
    use converter::xml::XmlConverter;

    // Code files: handled before the generic loop because CodeConverter
    // needs the extension for language detection (the Converter trait's
    // convert() method doesn't receive the extension).
    let code_conv = CodeConverter;
    if code_conv.can_convert(&extension_norm, data) {
        let result = code_conv.convert_with_extension(data, &extension_norm, options)?;
        return enforce_strict_mode(result, options.strict);
    }

    let converters: Vec<Box<dyn Converter>> = vec![
        Box::new(DocxConverter),
        Box::new(PptxConverter),
        Box::new(XlsxConverter),
        Box::new(IpynbConverter),
        Box::new(JsonConverter),
        Box::new(XmlConverter),
        Box::new(CsvConverter),
        Box::new(HtmlConverter),
        Box::new(ImageConverter),
        Box::new(PlainTextConverter),
    ];

    for conv in &converters {
        if conv.can_convert(&extension_norm, data) {
            let result = conv.convert(data, options)?;
            return enforce_strict_mode(result, options.strict);
        }
    }

    Err(ConvertError::UnsupportedFormat {
        extension: extension_norm,
    })
}

fn enforce_strict_mode(
    result: ConversionResult,
    strict: bool,
) -> Result<ConversionResult, ConvertError> {
    if !strict || result.warnings.is_empty() {
        return Ok(result);
    }

    let first = &result.warnings[0];
    let loc = first
        .location
        .as_deref()
        .map(|l| format!(" ({l})"))
        .unwrap_or_default();
    Err(ConvertError::MalformedDocument {
        reason: format!(
            "strict mode: encountered warning [{:?}] {}{}",
            first.code, first.message, loc
        ),
    })
}

fn normalize_extension(extension: &str) -> String {
    extension
        .trim()
        .trim_start_matches('.')
        .to_ascii_lowercase()
}

/// Convert a file at the given path to Markdown with async image description.
///
/// The format is auto-detected from magic bytes and file extension.
/// If an `async_image_describer` is set, all image descriptions are resolved
/// concurrently. The caller provides the async runtime.
///
/// Requires the `async` feature. Not available on WASM targets — use
/// [`convert_bytes_async`] instead.
#[cfg(feature = "async")]
#[cfg(not(target_arch = "wasm32"))]
pub async fn convert_file_async(
    path: impl AsRef<Path>,
    options: &converter::AsyncConversionOptions,
) -> Result<ConversionResult, ConvertError> {
    let path = path.as_ref();
    let size = std::fs::metadata(path)?.len() as usize;
    if size > options.base.max_input_bytes {
        return Err(ConvertError::InputTooLarge {
            size,
            limit: options.base.max_input_bytes,
        });
    }

    let data = std::fs::read(path)?;

    if data.len() > options.base.max_input_bytes {
        return Err(ConvertError::InputTooLarge {
            size: data.len(),
            limit: options.base.max_input_bytes,
        });
    }

    let format = detection::detect_format(path, &data);

    let (format, is_zip_magic) = match format {
        Some("zip") => (detection::detect_zip_format(&data), true),
        other => (other, false),
    };

    let extension = match format {
        Some("code") => path.extension().and_then(|e| e.to_str()).unwrap_or("code"),
        Some(fmt) => fmt,
        None if is_zip_magic => {
            return Err(ConvertError::UnsupportedFormat {
                extension: "zip".to_string(),
            });
        }
        None => path.extension().and_then(|e| e.to_str()).unwrap_or(""),
    };

    convert_bytes_async(&data, extension, options).await
}

/// Convert raw bytes to Markdown with async image description.
///
/// For image-bearing formats (docx, pptx, xlsx, image), uses `convert_inner()`
/// for parsing then resolves images concurrently via the async describer.
/// For other formats, falls through to the sync `convert()`.
///
/// Requires the `async` feature.
#[cfg(feature = "async")]
pub async fn convert_bytes_async(
    data: &[u8],
    extension: &str,
    options: &converter::AsyncConversionOptions,
) -> Result<ConversionResult, ConvertError> {
    let extension_norm = normalize_extension(extension);

    if data.len() > options.base.max_input_bytes {
        return Err(ConvertError::InputTooLarge {
            size: data.len(),
            limit: options.base.max_input_bytes,
        });
    }

    if extension_norm == "pdf" {
        return Err(ConvertError::FormatNotSupported {
            extension: "pdf".to_string(),
            reason: "PDF is intentionally unsupported — Gemini, ChatGPT, and Claude \
                     handle PDF natively"
                .to_string(),
        });
    }

    // For image-bearing formats, use convert_inner() + async resolve
    if let Some(ref describer) = options.async_image_describer {
        match extension_norm.as_str() {
            "docx" => {
                let conv = converter::docx::DocxConverter;
                let (mut result, pending) = conv.convert_inner(data, &options.base)?;
                if !pending.infos.is_empty() {
                    converter::ooxml_utils::resolve_image_placeholders_async(
                        &mut result.markdown,
                        &mut result.plain_text,
                        &pending.infos,
                        &pending.bytes,
                        describer.as_ref(),
                        &mut result.warnings,
                    )
                    .await;
                }
                return enforce_strict_mode(result, options.base.strict);
            }
            "pptx" => {
                let conv = converter::pptx::PptxConverter;
                let (mut result, pending) = conv.convert_inner(data, &options.base)?;
                if !pending.infos.is_empty() {
                    converter::ooxml_utils::resolve_image_placeholders_async(
                        &mut result.markdown,
                        &mut result.plain_text,
                        &pending.infos,
                        &pending.bytes,
                        describer.as_ref(),
                        &mut result.warnings,
                    )
                    .await;
                }
                return enforce_strict_mode(result, options.base.strict);
            }
            "xlsx" | "xls" => {
                let conv = converter::xlsx::XlsxConverter;
                let (mut result, pending) = conv.convert_inner(data, &options.base)?;
                if !pending.infos.is_empty() {
                    converter::ooxml_utils::resolve_image_placeholders_async(
                        &mut result.markdown,
                        &mut result.plain_text,
                        &pending.infos,
                        &pending.bytes,
                        describer.as_ref(),
                        &mut result.warnings,
                    )
                    .await;
                }
                return enforce_strict_mode(result, options.base.strict);
            }
            ext if converter::image::ImageConverter.can_convert(ext, data) => {
                let conv = converter::image::ImageConverter;
                let (mut result, pending) = conv.convert_inner(data, &options.base)?;
                if !pending.infos.is_empty() {
                    converter::ooxml_utils::resolve_image_placeholders_async(
                        &mut result.markdown,
                        &mut result.plain_text,
                        &pending.infos,
                        &pending.bytes,
                        describer.as_ref(),
                        &mut result.warnings,
                    )
                    .await;
                }
                return enforce_strict_mode(result, options.base.strict);
            }
            _ => {}
        }
    }

    // Fallback: use sync convert for non-image formats or when no async describer
    convert_bytes(data, &extension_norm, &options.base)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_convert_bytes_input_too_large() {
        let data = vec![0u8; 1024];
        let options = ConversionOptions {
            max_input_bytes: 512,
            ..Default::default()
        };
        let result = convert_bytes(&data, "txt", &options);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            format!("{err}").contains("input too large"),
            "error was: {err}"
        );
    }

    #[test]
    fn test_convert_bytes_at_exact_limit_succeeds() {
        let data = b"Hello, world!";
        let options = ConversionOptions {
            max_input_bytes: data.len(),
            ..Default::default()
        };
        let result = convert_bytes(data, "txt", &options);
        assert!(result.is_ok());
    }

    #[test]
    fn test_convert_bytes_strict_mode_escalates_warning() {
        // Non-UTF8 bytes trigger a recoverable decoding warning in txt converter.
        let data = b"caf\xe9";
        let options = ConversionOptions {
            strict: true,
            ..Default::default()
        };
        let result = convert_bytes(data, "txt", &options);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            format!("{err}").contains("strict mode"),
            "error should mention strict mode: {err}"
        );
    }

    #[test]
    fn test_convert_bytes_non_strict_keeps_warning() {
        let data = b"caf\xe9";
        let result = convert_bytes(data, "txt", &ConversionOptions::default()).unwrap();
        assert!(!result.warnings.is_empty(), "expected decoding warning");
    }

    #[test]
    fn test_convert_bytes_extension_case_insensitive() {
        let result = convert_bytes(b"hello world", " TXT ", &ConversionOptions::default()).unwrap();
        assert!(result.markdown.contains("hello world"));
    }

    #[test]
    fn test_convert_bytes_extension_with_leading_dot() {
        let result = convert_bytes(b"hello world", ".txt", &ConversionOptions::default()).unwrap();
        assert!(result.markdown.contains("hello world"));
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn test_convert_file_non_ooxml_zip_returns_unsupported() {
        // Create a minimal valid ZIP file that is NOT an OOXML format
        let mut buf = std::io::Cursor::new(Vec::new());
        {
            let mut zip_writer = zip::ZipWriter::new(&mut buf);
            let options = zip::write::SimpleFileOptions::default();
            zip_writer.start_file("hello.txt", options).unwrap();
            std::io::Write::write_all(&mut zip_writer, b"hello world").unwrap();
            zip_writer.finish().unwrap();
        }
        let zip_data = buf.into_inner();

        // Write to a temp file with .txt extension
        let dir = std::env::temp_dir().join("anytomd_test_zip_misroute");
        std::fs::create_dir_all(&dir).unwrap();
        let file_path = dir.join("archive.txt");
        std::fs::write(&file_path, &zip_data).unwrap();

        let options = ConversionOptions::default();
        let result = convert_file(&file_path, &options);

        assert!(result.is_err(), "expected UnsupportedFormat error");
        let err = result.unwrap_err();
        assert!(
            format!("{err}").contains("unsupported format"),
            "error was: {err}"
        );

        // Cleanup
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn test_convert_file_input_too_large() {
        // Use existing sample.csv fixture with a tiny limit
        let path = std::path::Path::new("tests/fixtures/sample.csv");
        if !path.exists() {
            return; // Skip if fixture not available
        }
        let file_size = std::fs::metadata(path).unwrap().len() as usize;
        let options = ConversionOptions {
            max_input_bytes: file_size.saturating_sub(1).max(1),
            ..Default::default()
        };
        let result = convert_file(path, &options);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            format!("{err}").contains("input too large"),
            "error was: {err}"
        );
    }

    #[test]
    fn test_convert_bytes_pdf_returns_descriptive_error() {
        let data = b"%PDF-1.7 fake pdf content";
        let options = ConversionOptions::default();
        let result = convert_bytes(data, "pdf", &options);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("pdf"), "error should mention pdf: {msg}");
        assert!(
            msg.contains("intentionally unsupported"),
            "error should explain why: {msg}"
        );
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn test_convert_file_pdf_returns_descriptive_error() {
        // Create a temp file with .pdf extension and PDF magic bytes
        let dir = std::env::temp_dir().join("anytomd_test_pdf_error");
        std::fs::create_dir_all(&dir).unwrap();
        let file_path = dir.join("test.pdf");
        std::fs::write(&file_path, b"%PDF-1.7 fake").unwrap();

        let options = ConversionOptions::default();
        let result = convert_file(&file_path, &options);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("intentionally unsupported"),
            "error should explain why: {msg}"
        );

        // Cleanup
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn test_convert_file_unknown_ext_json_with_long_leading_whitespace() {
        let dir = std::env::temp_dir().join("anytomd_test_json_whitespace_detect");
        std::fs::create_dir_all(&dir).unwrap();
        let file_path = dir.join("payload.dat");
        let mut data = vec![b' '; 40];
        data.extend_from_slice(br#"{"k":1}"#);
        std::fs::write(&file_path, data).unwrap();

        let result = convert_file(&file_path, &ConversionOptions::default()).unwrap();
        assert!(
            result.markdown.contains("\"k\""),
            "expected JSON conversion, markdown was: {}",
            result.markdown
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn test_convert_file_unknown_ext_json_with_utf8_bom() {
        let dir = std::env::temp_dir().join("anytomd_test_json_bom_detect");
        std::fs::create_dir_all(&dir).unwrap();
        let file_path = dir.join("payload.dat");
        let mut data = vec![0xEF, 0xBB, 0xBF];
        data.extend_from_slice(br#"{"k":1}"#);
        std::fs::write(&file_path, data).unwrap();

        let result = convert_file(&file_path, &ConversionOptions::default()).unwrap();
        assert!(
            result.markdown.contains("\"k\""),
            "expected JSON conversion, markdown was: {}",
            result.markdown
        );

        let _ = std::fs::remove_dir_all(&dir);
    }
}