markdown: Fix out of range panic in parser (#30510)
For some reason `pulldown_cmark` treats \````` as a codeblock, meaning that we could end up with an invalid range generated from `extract_code_block_content_range` (`3..2`) Closes #30495 Release Notes: - agent: Fix an edge case where the editor would crash when model generated malformed markdown
This commit is contained in:
parent
1fa19c69a6
commit
06af0310f7
1 changed files with 7 additions and 0 deletions
|
@ -510,6 +510,9 @@ pub(crate) fn extract_code_block_content_range(text: &str) -> Range<usize> {
|
|||
if !range.is_empty() && text.ends_with("```") {
|
||||
range.end -= 3;
|
||||
}
|
||||
if range.start > range.end {
|
||||
range.end = range.start;
|
||||
}
|
||||
range
|
||||
}
|
||||
|
||||
|
@ -686,6 +689,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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue