Skip to content

Implement disableMarkdownRendering setting to allow users to disable markdown … #2236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
14 changes: 10 additions & 4 deletions crates/chat-cli/src/cli/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,10 @@ impl ChatSession {
let mut buf = String::new();
let mut offset = 0;
let mut ended = false;
let mut state = ParseState::new(Some(self.terminal_width()));
let mut state = ParseState::new(
Some(self.terminal_width()),
os.database.settings.get_bool(Setting::ChatDisableMarkdownRendering),
);
let mut response_prefix_printed = false;

let mut tool_uses = Vec::new();
Expand Down Expand Up @@ -1983,10 +1986,13 @@ impl ChatSession {
},
parser::ResponseEvent::AssistantText(text) => {
// Add Q response prefix before the first assistant text.
// This must be markdown - using a code tick, which is printed
// as green.
if !response_prefix_printed && !text.trim().is_empty() {
buf.push_str("`>` ");
queue!(
self.stdout,
style::SetForegroundColor(Color::Green),
style::Print("> "),
style::SetForegroundColor(Color::Reset)
)?;
response_prefix_printed = true;
}
buf.push_str(&text);
Expand Down
71 changes: 65 additions & 6 deletions crates/chat-cli/src/cli/chat/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl<'a> ParserError<Partial<&'a str>> for Error<'a> {
#[derive(Debug)]
pub struct ParseState {
pub terminal_width: Option<usize>,
pub markdown_disabled: Option<bool>,
pub column: usize,
pub in_codeblock: bool,
pub bold: bool,
Expand All @@ -93,9 +94,10 @@ pub struct ParseState {
}

impl ParseState {
pub fn new(terminal_width: Option<usize>) -> Self {
pub fn new(terminal_width: Option<usize>, markdown_disabled: Option<bool>) -> Self {
Self {
terminal_width,
markdown_disabled,
column: 0,
in_codeblock: false,
bold: false,
Expand Down Expand Up @@ -135,8 +137,12 @@ pub fn interpret_markdown<'a, 'b>(
};
}

match state.in_codeblock {
false => {
match (state.in_codeblock, state.markdown_disabled.unwrap_or(false)) {
(_, true) => {
// If markdown is disabled, do not include markdown-related parsers
stateful_alt!(text, line_ending, fallback);
},
(false, false) => {
stateful_alt!(
// This pattern acts as a short circuit for alphanumeric plaintext
// More importantly, it's needed to support manual wordwrapping
Expand Down Expand Up @@ -167,7 +173,7 @@ pub fn interpret_markdown<'a, 'b>(
fallback
);
},
true => {
(true, false) => {
stateful_alt!(
codeblock_less_than,
codeblock_greater_than,
Expand Down Expand Up @@ -646,7 +652,7 @@ mod tests {
use super::*;

macro_rules! validate {
($test:ident, $input:literal, [$($commands:expr),+ $(,)?]) => {
($test:ident, $input:literal, [$($commands:expr),+ $(,)?], $markdown_enabled:expr) => {
#[test]
fn $test() -> eyre::Result<()> {
use crossterm::ExecutableCommand;
Expand All @@ -655,7 +661,7 @@ mod tests {
input.push(' ');
input.push(' ');

let mut state = ParseState::new(Some(80));
let mut state = ParseState::new(Some(80), Some($markdown_enabled));
let mut presult = vec![];
let mut offset = 0;

Expand Down Expand Up @@ -686,6 +692,10 @@ mod tests {
Ok(())
}
};

($test:ident, $input:literal, [$($commands:expr),+ $(,)?]) => {
validate!($test, $input, [$($commands),+], false);
};
}

validate!(text_1, "hello world!", [style::Print("hello world!")]);
Expand Down Expand Up @@ -759,4 +769,53 @@ mod tests {
validate!(square_bracket_url_like_2, "[text](without url part", [style::Print(
"[text](without url part"
)]);

validate!(markdown_disabled_bold, "**hello**", [style::Print("**hello**")], true);
validate!(markdown_disabled_italic, "*hello*", [style::Print("*hello*")], true);
validate!(markdown_disabled_code, "`print`", [style::Print("`print`")], true);
validate!(
markdown_disabled_heading,
"# Hello World",
[style::Print("# Hello World")],
true
);
validate!(markdown_disabled_bullet, "- bullet", [style::Print("- bullet")], true);
validate!(markdown_disabled_number, "1. number", [style::Print("1. number")], true);
validate!(markdown_disabled_blockquote, "> hello", [style::Print("> hello")], true);
validate!(
markdown_disabled_url,
"[amazon](amazon.com)",
[style::Print("[amazon](amazon.com)")],
true
);
validate!(
markdown_disabled_codeblock,
"```java hello world!```",
[style::Print("```java hello world!```")],
true
);
validate!(
markdown_disabled_text,
"hello world!",
[style::Print("hello world!")],
true
);
validate!(
markdown_disabled_line_ending,
"line one\nline two",
[
style::Print("line one"),
style::ResetColor,
style::SetAttribute(style::Attribute::Reset),
style::Print("\n"),
style::Print("line two")
],
true
);
validate!(
markdown_disabled_fallback,
"+ % @ . ?",
[style::Print("+ % @ . ?")],
true
);
}
14 changes: 14 additions & 0 deletions crates/chat-cli/src/database/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum Setting {
McpNoInteractiveTimeout,
McpLoadedBefore,
ChatDefaultModel,
ChatDisableMarkdownRendering,
ChatDefaultAgent,
ChatDisableAutoCompaction,
ChatEnableHistoryHints,
Expand All @@ -57,6 +58,7 @@ impl AsRef<str> for Setting {
Self::McpNoInteractiveTimeout => "mcp.noInteractiveTimeout",
Self::McpLoadedBefore => "mcp.loadedBefore",
Self::ChatDefaultModel => "chat.defaultModel",
Self::ChatDisableMarkdownRendering => "chat.disableMarkdownRendering",
Self::ChatDefaultAgent => "chat.defaultAgent",
Self::ChatDisableAutoCompaction => "chat.disableAutoCompaction",
Self::ChatEnableHistoryHints => "chat.enableHistoryHints",
Expand Down Expand Up @@ -91,6 +93,7 @@ impl TryFrom<&str> for Setting {
"mcp.noInteractiveTimeout" => Ok(Self::McpNoInteractiveTimeout),
"mcp.loadedBefore" => Ok(Self::McpLoadedBefore),
"chat.defaultModel" => Ok(Self::ChatDefaultModel),
"chat.disableMarkdownRendering" => Ok(Self::ChatDisableMarkdownRendering),
"chat.defaultAgent" => Ok(Self::ChatDefaultAgent),
"chat.disableAutoCompaction" => Ok(Self::ChatDisableAutoCompaction),
"chat.enableHistoryHints" => Ok(Self::ChatEnableHistoryHints),
Expand Down Expand Up @@ -213,12 +216,17 @@ mod test {
assert_eq!(settings.get(Setting::ShareCodeWhispererContent), None);
assert_eq!(settings.get(Setting::McpLoadedBefore), None);
assert_eq!(settings.get(Setting::ChatDefaultModel), None);
assert_eq!(settings.get(Setting::ChatDisableMarkdownRendering), None);

settings.set(Setting::TelemetryEnabled, true).await.unwrap();
settings.set(Setting::OldClientId, "test").await.unwrap();
settings.set(Setting::ShareCodeWhispererContent, false).await.unwrap();
settings.set(Setting::McpLoadedBefore, true).await.unwrap();
settings.set(Setting::ChatDefaultModel, "model 1").await.unwrap();
settings
.set(Setting::ChatDisableMarkdownRendering, false)
.await
.unwrap();

assert_eq!(settings.get(Setting::TelemetryEnabled), Some(&Value::Bool(true)));
assert_eq!(
Expand All @@ -234,15 +242,21 @@ mod test {
settings.get(Setting::ChatDefaultModel),
Some(&Value::String("model 1".to_string()))
);
assert_eq!(
settings.get(Setting::ChatDisableMarkdownRendering),
Some(&Value::Bool(false))
);

settings.remove(Setting::TelemetryEnabled).await.unwrap();
settings.remove(Setting::OldClientId).await.unwrap();
settings.remove(Setting::ShareCodeWhispererContent).await.unwrap();
settings.remove(Setting::McpLoadedBefore).await.unwrap();
settings.remove(Setting::ChatDisableMarkdownRendering).await.unwrap();

assert_eq!(settings.get(Setting::TelemetryEnabled), None);
assert_eq!(settings.get(Setting::OldClientId), None);
assert_eq!(settings.get(Setting::ShareCodeWhispererContent), None);
assert_eq!(settings.get(Setting::McpLoadedBefore), None);
assert_eq!(settings.get(Setting::ChatDisableMarkdownRendering), None);
}
}