Skip to content
Merged
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ cargo install --path .

## Command-line usage


```bash
mdtablefix [--wrap] [--renumber] [--breaks] [--ellipsis] [--fences] [--footnotes] [--in-place] [FILE...]
```
Expand Down
40 changes: 27 additions & 13 deletions src/fences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use regex::Regex;
static FENCE_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(\s*)(`{3,}|~{3,})([A-Za-z0-9_+.,-]*)\s*$").unwrap());

static ORPHAN_LANG_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[A-Za-z0-9_+.-]+(?:,[A-Za-z0-9_+.-]+)*$").unwrap());
static ORPHAN_LANG_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[A-Za-z0-9_+.-]*[A-Za-z0-9_+\-](?:,[A-Za-z0-9_+.-]*[A-Za-z0-9_+\-])*$").unwrap()
});

/// Compress backtick fences to exactly three backticks.
///
Expand Down Expand Up @@ -62,7 +63,7 @@ pub fn compress_fences(lines: &[String]) -> Vec<String> {
/// "```".to_string(),
/// ];
/// let fixed = attach_orphan_specifiers(&compress_fences(&lines));
/// assert_eq!(fixed[0], "```Rust");
/// assert_eq!(fixed[0], "```rust");
/// ```
#[must_use]
pub fn attach_orphan_specifiers(lines: &[String]) -> Vec<String> {
Expand All @@ -71,24 +72,37 @@ pub fn attach_orphan_specifiers(lines: &[String]) -> Vec<String> {
for line in lines {
let trimmed = line.trim();

if trimmed.starts_with("```") {
if let Some(cap) = FENCE_RE.captures(trimmed) {
if in_fence {
in_fence = false;
} else {
while matches!(out.last(), Some(l) if l.trim().is_empty()) {
out.pop();
out.push(line.clone());
continue;
}

let indent = cap.get(1).map_or("", |m| m.as_str());
let lang_present = cap.get(3).map_or("", |m| m.as_str());

if lang_present.is_empty() {
let mut idx = out.len();
while idx > 0 && out[idx - 1].trim().is_empty() {
idx -= 1;
}
if let Some(prev) = out.last() {
let lang = prev.trim().to_string();
if ORPHAN_LANG_RE.is_match(&lang) {
out.pop();
out.push(format!("```{lang}"));
if idx > 0 {
let candidate = out[idx - 1].trim().to_string();
if ORPHAN_LANG_RE.is_match(&candidate)
&& (idx == 1 || out[idx - 2].trim().is_empty())
{
out.truncate(idx - 1);
out.push(format!("{indent}```{}", candidate.to_lowercase()));
in_fence = true;
continue;
}
}
in_fence = true;
}

in_fence = true;
out.push(line.clone());
continue;
}

out.push(line.clone());
Expand Down
50 changes: 48 additions & 2 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn test_cli_fences_orphan_specifier() {
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"```Rust\nfn main() {}\n```\n"
"```rust\nfn main() {}\n```\n"
);
}

Expand All @@ -219,7 +219,53 @@ fn test_cli_fences_with_renumber() {
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"```Rust\nfn main() {}\n```\n\n1. first\n2. second\n",
"```rust\nfn main() {}\n```\n\n1. first\n2. second\n",
);
}

#[test]
fn test_cli_fences_preserve_existing_language() {
let output = Command::cargo_bin("mdtablefix")
.expect("Failed to create cargo command for mdtablefix")
.arg("--fences")
.write_stdin("ruby\n```rust\nfn main() {}\n```\n")
.output()
.expect("Failed to execute mdtablefix command");
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"ruby\n```rust\nfn main() {}\n```\n"
Comment on lines +231 to +237
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider adding a CLI test for orphaned specifiers with extra whitespace or unusual formatting.

Adding tests with leading/trailing whitespace or indented fences will help ensure the CLI handles varied input formats reliably.

);
}

#[test]
fn test_cli_fences_orphan_specifier_symbols() {
let output = Command::cargo_bin("mdtablefix")
.expect("Failed to create cargo command for mdtablefix")
.arg("--fences")
.write_stdin("C++\n```\nfn main() {}\n```\n")
.output()
.expect("Failed to execute mdtablefix command");
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"```c++\nfn main() {}\n```\n"
);
}

#[test]
fn test_cli_no_attach_without_preceding_blank_line() {
let input = concat!("text\n", "Rust\n", "```\n", "fn main() {}\n", "```\n");
let output = Command::cargo_bin("mdtablefix")
.expect("Failed to create cargo command for mdtablefix")
.arg("--fences")
.write_stdin(input)
.output()
.expect("Failed to execute mdtablefix command");
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"text\nRust\n```\nfn main() {}\n```\n",
);
}

Expand Down
59 changes: 53 additions & 6 deletions tests/fences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ fn leaves_other_lines_untouched() {
fn fixes_orphaned_specifier() {
let input = lines_vec!["Rust", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&compress_fences(&input));
assert_eq!(out, lines_vec!["```Rust", "fn main() {}", "```"]);
assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[test]
fn attaches_orphan_specifier_unit() {
let input = lines_vec!["Rust", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&input);
assert_eq!(out, lines_vec!["```Rust", "fn main() {}", "```"]);
assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[test]
fn attaches_orphan_specifier_with_blank_line_unit() {
let input = lines_vec!["Rust", "", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&input);
assert_eq!(out, lines_vec!["```Rust", "fn main() {}", "```"]);
assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[test]
fn fixes_orphaned_specifier_with_blank_line() {
let input = lines_vec!["Rust", "", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&compress_fences(&input));
assert_eq!(out, lines_vec!["```Rust", "fn main() {}", "```"]);
assert_eq!(out, lines_vec!["```rust", "fn main() {}", "```"]);
}

#[test]
Expand All @@ -78,6 +78,7 @@ fn fixes_multiple_orphaned_specifiers() {
"```",
"fn main() {}",
"```",
"",
"Python",
"```",
"print('hi')",
Expand All @@ -87,10 +88,11 @@ fn fixes_multiple_orphaned_specifiers() {
assert_eq!(
out,
lines_vec![
"```Rust",
"```rust",
"fn main() {}",
"```",
"```Python",
"",
"```python",
"print('hi')",
"```"
]
Expand All @@ -112,3 +114,48 @@ fn does_not_attach_non_orphan_lines_before_fences() {
let out = attach_orphan_specifiers(&input);
assert_eq!(out, input);
}

#[test]
fn does_not_overwrite_existing_fence() {
let input = lines_vec!["ruby", "```rust", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&compress_fences(&input));
assert_eq!(out, lines_vec!["ruby", "```rust", "fn main() {}", "```"]);
}

#[test]
fn does_not_attach_specifier_without_preceding_blank_line() {
let input = lines_vec!["intro", "Rust", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&compress_fences(&input));
assert_eq!(
out,
lines_vec!["intro", "Rust", "```", "fn main() {}", "```"]
);
}

#[test]
fn attaches_orphan_specifier_with_symbols() {
let input = lines_vec!["C++", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&compress_fences(&input));
assert_eq!(out, lines_vec!["```c++", "fn main() {}", "```"]);
}

#[test]
fn attaches_orphan_specifier_with_hyphen_and_dot() {
let input = lines_vec!["objective-c", "```", "int main() {}", "```"];
let out = attach_orphan_specifiers(&compress_fences(&input));
assert_eq!(out, lines_vec!["```objective-c", "int main() {}", "```"]);
}

#[test]
fn does_not_attach_specifier_with_trailing_period() {
let input = lines_vec!["rust.", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&input);
assert_eq!(out, input);
}

#[test]
fn does_not_attach_specifier_with_trailing_question_mark() {
let input = lines_vec!["rust?", "```", "fn main() {}", "```"];
let out = attach_orphan_specifiers(&input);
assert_eq!(out, input);
}