diff --git a/crates/markdown/src/parser.rs b/crates/markdown/src/parser.rs index ed9f64374bce262e89565fd42290388f290a0749..1c9376c6d41f26bc350585c8164c549c5404a8e1 100644 --- a/crates/markdown/src/parser.rs +++ b/crates/markdown/src/parser.rs @@ -509,6 +509,9 @@ pub(crate) fn extract_code_block_content_range(text: &str) -> Range { if !range.is_empty() && text.ends_with("```") { range.end -= 3; } + if range.start > range.end { + range.end = range.start; + } range } @@ -663,6 +666,10 @@ mod tests { let input = "```python\nprint('hello')\nprint('world')\n```"; assert_eq!(extract_code_block_content_range(input), 10..40); + + // Malformed input + let input = "`````"; + assert_eq!(extract_code_block_content_range(input), 3..3); } #[test]