markdown-ai-cite-remove 0.3.0

High-performance removal of AI-generated citations and annotations from Markdown text
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
![markdown-ai-banner-primary](https://github.com/user-attachments/assets/7974c422-2ad1-442b-8c08-c82026100341)

<br />

# Rust Crate: Markdown AI Citation Removal

[![Crates.io](https://img.shields.io/crates/v/markdown-ai-cite-remove.svg)](https://crates.io/crates/markdown-ai-cite-remove)
[![Documentation](https://docs.rs/markdown-ai-cite-remove/badge.svg)](https://docs.rs/markdown-ai-cite-remove)
[![License](https://img.shields.io/crates/l/markdown-ai-cite-remove.svg)](LICENSE)
[![Tests](https://img.shields.io/badge/tests-58%20passing-brightgreen)](tests/)

<br />

> *"Five years of AI evolution and flirting with AGI. Zero libraries to remove `[1][2][3]` from markdown ๐Ÿ™„. Remember me well when they take over."*

<br />

**Remove AI-generated citations and annotations from Markdown text at the speed of Rust**

High-performance Rust library for removing citations from ChatGPT, Claude, Perplexity, and other AI markdown responses. Removes inline citations `[1][2]`, reference links `[1]: https://...`, and bibliography sections with 100% accuracy.

<br />

> ๐Ÿ“š [CLI Guide]docs/guides/CLI_GUIDE.md โ€ข [Benchmarking Guide]docs/performance/BENCHMARKING.md โ€ข [FAQ]docs/guides/FAQ.md โ€ข [Documentation Index]docs/DOCUMENTATION_INDEX.md

<br />
<br />

---

## โšก Performance-First

- **100+ MB/s** throughput on typical documents
- **Zero-copy** processing where possible
- **Regex-optimized** with lazy compilation
- **Thread-safe** stateless design

<br />

---

## ๐ŸŽฏ Use Cases

<br />

### Real-World Applications

<br />

#### **1. Blog Publishing Pipeline**

```rust
// Remove citations from AI-generated blog posts before publishing
use markdown_ai_cite_remove::clean;

let ai_draft = fetch_from_chatgpt();
let clean_post = remove_citations(&ai_draft);
publish_to_cms(clean_post);
```

<br />

#### **2. Documentation Generation**

```rust
// Remove citations from AI-generated documentation
let docs = generate_docs_with_ai();
let clean_docs = remove_citations(&docs);
write_to_file("README.md", clean_docs);
```

<br />

#### **3. Content Aggregation**

```rust
// Remove citations from multiple AI responses
use markdown_ai_cite_remove::CitationRemover;

let cleaner = CitationRemover::new();
let responses = vec![chatgpt_response, claude_response, perplexity_response];

let cleaned: Vec<String> = responses
    .iter()
    .map(|r| cleaner.remove_citations(r))
    .collect();
```

<br />

#### **4. Streaming API Processing**

```rust
// Remove citations from AI responses in real-time
async fn process_stream(stream: impl Stream<Item = String>) {
    let cleaner = CitationRemover::new();

    stream
        .map(|chunk| cleaner.remove_citations(&chunk))
        .for_each(|cleaned| {
            send_to_client(cleaned).await;
        })
        .await;
}
```

<br />

#### **5. Simple File Processing (CLI)**

```bash
# Remove citations and auto-generate output file
mdcr ai_response.md
# Creates: ai_response__cite_removed.md
```

<br />
<br />

### Common Scenarios

- โœ… Remove citations from AI chatbot responses (ChatGPT, Claude, Perplexity, Gemini)
- โœ… Prepare markdown for blog posts and articles
- โœ… Remove citations before website publishing
- โœ… Process streaming API responses in real-time
- โœ… Batch document cleaning for content pipelines
- โœ… Remove citations from documentation generated by AI tools
- โœ… Prepare content for CMS ingestion
- โœ… Remove annotations from research summaries

<br />

---

## ๐Ÿ“ฆ Installation

<br />

### Prerequisites

**Minimum Requirements:**
- Rust 1.70 or later
- Cargo (comes with Rust)

**Optional (for enhanced benchmarking):**
- Gnuplot (for benchmark visualization)
  - macOS: `brew install gnuplot`
  - Ubuntu/Debian: `sudo apt-get install gnuplot`
  - Windows: Download from http://www.gnuplot.info/

<br />

### Library Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
markdown-ai-cite-remove = "0.1"
```

<br />

### CLI Installation

Install the command-line tool globally:

```bash
# Install from crates.io (when published)
cargo install markdown-ai-cite-remove

# Or install from local source
cargo install --path .
```

Verify installation:

```bash
mdcr --version
```

<br />

---

## ๐Ÿš€ Quick Start

<br />

### Quick Reference

| Task | Command |
|------|---------|
| Remove citations from stdin | `echo "Text[1]" \| mdcr` |
| Auto-generate output file | `mdcr input.md` |
| Specify output file | `mdcr input.md -o output.md` |
| Verbose output | `mdcr input.md --verbose` |
| Run tests | `cargo test` |
| Run benchmarks | `cargo bench` |
| View docs | `cargo doc --open` |

<br />

### Library Usage

```rust
use markdown_ai_cite_remove::clean;

let markdown = "AI research shows promise[1][2].\n\n[1]: https://example.com\n[2]: https://test.com";
let result = remove_citations(markdown);
assert_eq!(cleaned.trim(), "AI research shows promise.");
```

<br />

### CLI Usage

<br />

#### Basic Examples

**1. Process from stdin to stdout (pipe mode):**

```bash
echo "Text[1] here." | mdcr
# Output: Text here.
```

<br />

**2. Auto-generate output file (easiest!):**

```bash
mdcr ai_response.md
# Creates: ai_response__cite_removed.md
```

<br />

**3. Specify custom output file:**

```bash
mdcr input.md -o output.md
```

<br />

**4. Verbose output (shows processing details):**

```bash
mdcr input.md --verbose
# Output:
# Reading from file: input.md
# Removing citations (input size: 1234 bytes)...
# Citations removed (output size: 1100 bytes)
# Writing to file: input__cite_removed.md
# Done!
```

<br />
<br />

#### Advanced CLI Usage

**Process multiple files (auto-generated output):**

```bash
# Process all markdown files in current directory
for file in *.md; do
  mdcr "$file"
done
# Creates: file1__cite_removed.md, file2__cite_removed.md, etc.
```

<br />

**Integration with other tools:**

```bash
# Remove citations from AI output from curl
curl https://api.example.com/ai-response | mdcr

# Remove citations and preview
mdcr document.md -o - | less

# Remove citations and count words
mdcr document.md -o - | wc -w

# Chain with other markdown processors
mdcr input.md -o - | pandoc -f markdown -t html -o output.html
```

<br />

**Advanced shell script example:**

For more complex workflows, create a custom shell script. See the [CLI Guide](docs/guides/CLI_GUIDE.md) for advanced automation examples including:
- Batch processing with custom naming
- Directory watching and auto-processing
- Git pre-commit hooks
- CI/CD integration

<br />

---

## ๐Ÿ”ง Features

- โœ… Remove inline numeric citations `[1][2][3]`
- โœ… Remove named citations `[source:1][ref:2][cite:3][note:4]`
- โœ… Remove reference link lists `[1]: https://...`
- โœ… Remove reference section headers `## References`, `# Citations`, `### Sources`
- โœ… Remove bibliographic entries `[1] Author (2024). Title...`
- โœ… Preserve markdown formatting (bold, italic, links, lists, etc.)
- โœ… Whitespace normalization
- โœ… Configurable cleaning options

<br />

---

## ๐Ÿ“– Documentation

- **[FAQ]docs/guides/FAQ.md** - Frequently asked questions and troubleshooting
- **[CLI Usage Guide]docs/guides/CLI_GUIDE.md** - Complete command-line tool documentation
- **[Benchmarking Guide]docs/performance/BENCHMARKING.md** - Understanding performance metrics
- **[Documentation Index]docs/DOCUMENTATION_INDEX.md** - Complete guide to all documentation
- **[API Documentation]https://docs.rs/markdown-ai-cite-remove** - Full API reference
- **[Examples]examples/** - Working code examples

<br />

---

## ๐Ÿ“š Advanced Usage

<br />

### Custom Configuration

```rust
use markdown_ai_cite_remove::{CitationRemover, RemoverConfig};

// Remove only inline citations, keep reference sections
let config = RemoverConfig::inline_only();
let cleaner = CitationRemover::with_config(config);
let result = cleaner.remove_citations("Text[1] here.\n\n[1]: https://example.com");

// Remove only reference sections, keep inline citations
let config = RemoverConfig::references_only();
let result = cleaner.remove_citations("Text[1] here.\n\n[1]: https://example.com");

// Full custom configuration
let config = RemoverConfig {
    remove_inline_citations: true,
    remove_reference_links: true,
    remove_reference_headers: true,
    remove_reference_entries: true,
    normalize_whitespace: true,
    remove_blank_lines: true,
    trim_lines: true,
};
```

<br />

### Reusable Cleaner Instance

```rust
use markdown_ai_cite_remove::CitationRemover;

let cleaner = CitationRemover::new();

// Reuse for multiple documents
let doc1 = cleaner.remove_citations("First document[1].");
let doc2 = cleaner.remove_citations("Second document[2][3].");
let doc3 = cleaner.remove_citations("Third document[source:1].");
```

<br />

---

## ๐Ÿงช Examples

See the [`examples/`](examples/) directory for more:

- [`basic_usage.rs`]examples/basic_usage.rs - Simple examples
- [`custom_config.rs`]examples/custom_config.rs - Configuration options

Run examples:

```bash
cargo run --example basic_usage
cargo run --example custom_config
```

<br />

---

## ๐ŸŽ๏ธ Performance

<br />

### Running Benchmarks

```bash
# Run all benchmarks
cargo bench

# Run specific benchmark
cargo bench chatgpt_format

# Save baseline for comparison
cargo bench -- --save-baseline main

# Compare against baseline
cargo bench -- --baseline main

# View results (after running benchmarks)
open target/criterion/report/index.html  # macOS
xdg-open target/criterion/report/index.html  # Linux
start target/criterion/report/index.html  # Windows
```

<br />

**Note about benchmark output:**
- Tests shown as "ignored" during `cargo bench` is **normal behavior** - regular tests are skipped during benchmarking to avoid interference
- Outliers (3-13% of measurements) are normal due to OS scheduling and CPU frequency scaling
- "Gnuplot not found" warning is harmless - Criterion uses an alternative plotting backend
- **With Gnuplot installed**: Interactive HTML reports with charts are generated in `target/criterion/report/`

<br />

### Performance Characteristics

Typical performance on modern hardware (Apple Silicon M-series):

| Benchmark | Time | Throughput | Notes |
|-----------|------|------------|-------|
| Simple inline citations | ~580 ns | 91 MiB/s | Single sentence |
| Complex document | ~2.5 ฮผs | 287 MiB/s | Multiple sections |
| Real ChatGPT output | ~18 ฮผs | 645 MiB/s | 11.8 KB document |
| Real Perplexity output | ~245 ฮผs | 224 MiB/s | 54.9 KB document |
| Batch (5 documents) | ~2.2 ฮผs | 43 MiB/s | Total for all 5 |
| No citations (passthrough) | ~320 ns | 393 MiB/s | Fastest path |

<br />

**Key Insights:**
- **Throughput**: 100-650 MB/s depending on document complexity
- **Latency**: Sub-microsecond to ~250 ฮผs for large documents
- **Scalability**: Linear with document size
- **Memory**: ~200-300 bytes per operation

<br />

---

## ๐Ÿงช Testing

This library has **100%+ test coverage** with comprehensive edge case testing.

<br />

### Running Tests

```bash
# Run all tests (unit + integration + doc tests)
cargo test

# Run with output visible
cargo test -- --nocapture

# Run specific test
cargo test test_real_world_chatgpt

# Run only unit tests
cargo test --lib

# Run only integration tests
cargo test --test integration_tests

# Run tests with all features enabled
cargo test --all-features
```

<br />

### Test Coverage

- **58 total tests** covering all functionality
- **18 unit tests** - Core logic, patterns, configuration
- **36 integration tests** - Real-world scenarios, edge cases
- **4 doc tests** - Documentation examples

<br />

**What's tested:**
- โœ… All citation formats (numeric, named, reference links)
- โœ… Real AI outputs (ChatGPT, Perplexity)
- โœ… Edge cases (empty strings, no citations, only citations)
- โœ… Unicode and emoji support
- โœ… Large documents (1000+ citations)
- โœ… Configuration variations
- โœ… Markdown preservation (formatting, links, images)

<br />

### Understanding Test Output

When running `cargo bench`, you'll see tests marked as "ignored" - **this is normal**. Rust automatically skips regular tests during benchmarking to avoid timing interference. All tests pass when running `cargo test`.

<br />

---

## ๐Ÿ”ง Troubleshooting

<br />

### Common Issues

<br />

**Q: Why do tests show as "ignored" when running `cargo bench`?**

A: This is normal Rust behavior. When running benchmarks, regular tests are automatically skipped to avoid interfering with timing measurements. All tests pass when you run `cargo test`. See [BENCHMARKING.md](docs/performance/BENCHMARKING.md) for details.

<br />

**Q: What does "Gnuplot not found, using plotters backend" mean?**

A: This is just an informational message. Criterion (the benchmarking library) can use Gnuplot for visualization, but falls back to an alternative plotting backend if it's not installed. Benchmarks still run correctly. To install Gnuplot:
- macOS: `brew install gnuplot`
- Ubuntu/Debian: `sudo apt-get install gnuplot`
- Windows: Download from http://www.gnuplot.info/

<br />

**Q: Why are there performance outliers in benchmarks?**

A: Outliers (typically 3-13% of measurements) are normal due to:
- Operating system scheduling
- CPU frequency scaling
- Background processes
- Cache effects

This is expected and doesn't indicate a problem. Criterion automatically detects and reports outliers.

<br />

**Q: The CLI tool isn't found after installation**

A: Make sure Cargo's bin directory is in your PATH:

```bash
# Add to ~/.bashrc, ~/.zshrc, or equivalent
export PATH="$HOME/.cargo/bin:$PATH"

# Then reload your shell
source ~/.bashrc  # or source ~/.zshrc
```

<br />

**Q: How do I know if citations were actually removed?**

A: Use the `--verbose` flag to see before/after sizes:

```bash
mdcr input.md --verbose
```

<br />

### Getting Help

- **Issues**: Report bugs or request features on [GitHub]https://github.com/opensite-ai/markdown-ai-cite-remove/issues
- **Documentation**: Run `cargo doc --open` for full API docs
- **Examples**: Check the `examples/` directory for working code

<br />

---

## ๐Ÿค Contributing

Built by [OpenSite AI](https://opensite.ai) for the developer community.

Contributions welcome! Please feel free to submit a Pull Request.

<br />

### Development Setup

```bash
# Clone the repository
git clone https://github.com/opensite-ai/markdown-ai-cite-remove.git
cd markdown-ai-cite-remove

# Run tests
cargo test

# Run benchmarks
cargo bench

# Build documentation
cargo doc --open

# Format code
cargo fmt

# Run linter
cargo clippy
```

<br />

---

## ๐Ÿ“„ License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.