summaryrefslogtreecommitdiffstats
path: root/src/render/markdown.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/render/markdown.rs')
-rw-r--r--src/render/markdown.rs20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/render/markdown.rs b/src/render/markdown.rs
index 834c47b..f491fc1 100644
--- a/src/render/markdown.rs
+++ b/src/render/markdown.rs
@@ -82,13 +82,13 @@ impl MarkdownRender {
.join("\n")
}
- pub fn render_with_indent(&mut self, text: &str, padding: usize) -> String {
- let text = format!("{}{}", " ".repeat(padding), text);
+ pub fn render_with_indent(&mut self, text: &str, indent: usize) -> String {
+ let text = format!("{}{}", " ".repeat(indent), text);
let output = self.render(&text);
if output.starts_with('\n') {
output
} else {
- output.chars().skip(padding).collect()
+ output.chars().skip(indent).collect()
}
}
@@ -186,7 +186,7 @@ impl MarkdownRender {
if is_code && !self.options.wrap_code {
return line;
}
- textwrap::wrap(&line, width as usize).join("\n")
+ wrap(&line, width as usize)
} else {
line
}
@@ -203,6 +203,12 @@ impl MarkdownRender {
}
}
+fn wrap(text: &str, width: usize) -> String {
+ let indent: usize = text.chars().take_while(|c| *c == ' ').count();
+ let wrap_options = textwrap::Options::new(width).initial_indent(&text[0..indent]);
+ textwrap::wrap(&text[indent..], wrap_options).join("\n")
+}
+
#[derive(Debug, Clone, Default)]
pub struct RenderOptions {
pub theme: Option<Theme>,
@@ -381,5 +387,11 @@ std::error::Error>> {
let expect =
"To unzip a file in Rust, you can use the\n`zip` crate. Here's an example code";
assert_eq!(output, expect);
+
+ let input = "Unzip a file";
+ let output = render.render_with_indent(input, 76);
+ let expect = "\nUnzip a file";
+
+ assert_eq!(output, expect);
}
}