summaryrefslogtreecommitdiffstats
path: root/src/render/markdown.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-11-02 21:38:01 +0800
committerGitHub <noreply@github.com>2023-11-02 21:38:01 +0800
commitdce6877f5de297803a737387fef7f672593904cf (patch)
treeea59af8d7f6509b3bd5852dfc05d58085046d2dc /src/render/markdown.rs
parent16eba9baa10255c95e9d680e7f880f75b9bc03b1 (diff)
downloadaichat-dce6877f5de297803a737387fef7f672593904cf.tar.gz
refactor: optimize render (#202)
Diffstat (limited to 'src/render/markdown.rs')
-rw-r--r--src/render/markdown.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/render/markdown.rs b/src/render/markdown.rs
index 8ae0a09..834c47b 100644
--- a/src/render/markdown.rs
+++ b/src/render/markdown.rs
@@ -82,6 +82,16 @@ impl MarkdownRender {
.join("\n")
}
+ pub fn render_with_indent(&mut self, text: &str, padding: usize) -> String {
+ let text = format!("{}{}", " ".repeat(padding), text);
+ let output = self.render(&text);
+ if output.starts_with('\n') {
+ output
+ } else {
+ output.chars().skip(padding).collect()
+ }
+ }
+
pub fn render_line(&self, line: &str) -> String {
let (_, code_syntax, is_code) = self.check_line(line);
if is_code {
@@ -359,4 +369,17 @@ std::error::Error>> {
let output = render.render(TEXT);
assert_eq!(TEXT_WRAP_ALL, output);
}
+
+ #[test]
+ fn wrap_with_indent() {
+ let options = RenderOptions::default();
+ let mut render = MarkdownRender::init(options).unwrap();
+ render.wrap_width = Some(80);
+
+ let input = "To unzip a file in Rust, you can use the `zip` crate. Here's an example code";
+ let output = render.render_with_indent(input, 40);
+ let expect =
+ "To unzip a file in Rust, you can use the\n`zip` crate. Here's an example code";
+ assert_eq!(output, expect);
+ }
}