use once_cell::sync::Lazy;
use regex::Regex;
static STANDARDIZE_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(```\w+)\s+markdown-code-runner(?:\s+\S+=\S+)*\s*$").unwrap());
pub fn standardize_code_fences(content: &str) -> String {
let mut result = String::new();
for line in content.lines() {
if let Some(caps) = STANDARDIZE_PATTERN.captures(line) {
result.push_str(&caps[1]);
} else {
result.push_str(line);
}
result.push('\n');
}
if !content.ends_with('\n') && result.ends_with('\n') {
result.pop();
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_standardize_basic() {
let content = "```python markdown-code-runner\nprint('hello')\n```";
let expected = "```python\nprint('hello')\n```";
assert_eq!(standardize_code_fences(content), expected);
}
#[test]
fn test_standardize_with_options() {
let content = "```python markdown-code-runner filename=test.py debug=true\ncode here\n```";
let expected = "```python\ncode here\n```";
assert_eq!(standardize_code_fences(content), expected);
}
#[test]
fn test_standardize_multiple() {
let content = r#"```python markdown-code-runner
first
```
text
```bash markdown-code-runner
second
```
more text
```rust markdown-code-runner filename=test.rs
third
```"#;
let expected = r#"```python
first
```
text
```bash
second
```
more text
```rust
third
```"#;
assert_eq!(standardize_code_fences(content), expected);
}
#[test]
fn test_standardize_preserves_text_references() {
let content =
"Using `markdown-code-runner` is easy.\n```python markdown-code-runner\ncode\n```";
let expected = "Using `markdown-code-runner` is easy.\n```python\ncode\n```";
assert_eq!(standardize_code_fences(content), expected);
}
#[test]
fn test_standardize_leaves_normal_fences() {
let content = "```python\ncode\n```\n```javascript\nmore\n```";
assert_eq!(standardize_code_fences(content), content);
}
}