-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fences4.rs
More file actions
40 lines (35 loc) · 1.5 KB
/
test_fences4.rs
File metadata and controls
40 lines (35 loc) · 1.5 KB
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
pub fn strip_code_fences(text: &str) -> String {
let trimmed = text.trim();
let lines: Vec<&str> = trimmed.lines().collect();
// Check if the response starts and ends with a code block
if lines.len() >= 3 {
let first = lines.first().copied().unwrap_or("").trim();
let last = lines.last().copied().unwrap_or("").trim();
if first.starts_with("```") && last == "```" {
// Ensure no other ``` in the middle (just one code block)
let inner_fences = lines[1..lines.len() - 1]
.iter()
.filter(|l| l.trim().starts_with("```"))
.count();
if inner_fences == 0 {
return lines[1..lines.len() - 1].join("\n").trim().to_string();
}
}
}
// Attempt to extract a single code block anywhere in the text
if let Some(start_idx) = trimmed.find("```") {
let after_ticks = &trimmed[start_idx + 3..];
if let Some(newline_idx) = after_ticks.find('\n') {
let code_start = start_idx + 3 + newline_idx + 1;
if let Some(end_idx) = trimmed[code_start..].find("\n```") {
// Return only if there are no more code fences in the remaining text
let remaining = &trimmed[code_start + end_idx + 4..];
if !remaining.contains("```") {
return trimmed[code_start..code_start + end_idx].trim().to_string();
}
}
}
}
trimmed.to_string()
}
fn main() {}