Skip to content

Commit 6c043a3

Browse files
markdown: Fix indentation of multi-line code blocks
Before this commit the "Welcome to Rust!" text was: ``` This path will then be added to your PATH environment variable by modifying the profile files located at: /Users/alexander/.profile /Users/alexander/.zprofile ``` Notice the second line of the code block is not indented. This is because the `Text` is passed to us as a single `&str` (which contains newlines). We need to split the text and call `LineWrapper.write_indent()` for each line.
1 parent 76ac3af commit 6c043a3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/cli/markdown.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ impl<'a, T: Terminal + 'a> LineWrapper<'a, T> {
7272
self.write_word(word);
7373
}
7474
}
75+
// Writes code block where each line is indented
76+
fn write_code_block(&mut self, text: &str) {
77+
for line in text.lines() {
78+
self.write_word(line); // Will call write_indent()
79+
self.write_line();
80+
}
81+
}
7582
// Constructor
7683
fn new(w: &'a mut T, indent: u32, margin: u32) -> Self {
7784
LineWrapper {
@@ -188,7 +195,7 @@ impl<'a, T: Terminal + io::Write + 'a> LineFormatter<'a, T> {
188195
End(tag) => self.end_tag(tag),
189196
Text(text) => {
190197
if self.is_code_block {
191-
self.wrapper.write_word(&text);
198+
self.wrapper.write_code_block(&text);
192199
} else {
193200
self.wrapper.write_span(&text);
194201
}

0 commit comments

Comments
 (0)