Skip to content

Commit e0f65f3

Browse files
authored
fix(chat): diplay special chars properly and blockquotes formatting (#1090)
1 parent 0ae64a2 commit e0f65f3

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

crates/q_cli/src/cli/chat/parse.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,18 @@ fn code<'a, 'b>(
279279
state: &'b mut ParseState,
280280
) -> impl FnMut(&mut Partial<&'a str>) -> PResult<(), Error<'a>> + 'b {
281281
move |i| {
282-
"`".parse_next(i)?;
283-
let code = terminated(take_until(0.., "`"), "`").parse_next(i)?;
284-
let out = code.replace("&amp;", "&").replace("&gt;", ">").replace("&lt;", "<");
282+
let start = i.checkpoint();
283+
284+
let display = match delimited::<_, _, _, _, Error<'a>, _, _, _>("`", take_until(1.., "`"), "`").parse_next(i) {
285+
Ok(display) => display,
286+
Err(_) => {
287+
// If it doesn't match, reset position and fail
288+
i.reset(&start);
289+
return Err(ErrMode::from_error_kind(i, ErrorKind::Fail));
290+
},
291+
};
292+
293+
let out = display.replace("&amp;", "&").replace("&gt;", ">").replace("&lt;", "<");
285294

286295
queue_newline_or_advance(&mut o, state, out.width())?;
287296
queue(&mut o, style::SetForegroundColor(Color::Green))?;
@@ -299,7 +308,7 @@ fn blockquote<'a, 'b>(
299308
return Err(ErrMode::from_error_kind(i, ErrorKind::Fail));
300309
}
301310

302-
let level = repeat::<_, _, Vec<&'_ str>, _, _>(1.., terminated("&gt;", space0))
311+
let level = repeat::<_, _, Vec<&'_ str>, _, _>(1.., terminated(">", space0))
303312
.parse_next(i)?
304313
.len();
305314
let print = "│ ".repeat(level);
@@ -743,7 +752,7 @@ mod tests {
743752
validate!(bulleted_item_1, "- bullet", [style::Print("• bullet")]);
744753
validate!(bulleted_item_2, "* bullet", [style::Print("• bullet")]);
745754
validate!(numbered_item_1, "1. number", [style::Print("1. number")]);
746-
validate!(blockquote_1, "&gt; hello", [
755+
validate!(blockquote_1, "> hello", [
747756
style::SetForegroundColor(BLOCKQUOTE_COLOR),
748757
style::Print("│ hello"),
749758
]);

0 commit comments

Comments
 (0)