Skip to content

Commit

Permalink
Fix comment prefix stripping
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-bourne committed Dec 1, 2023
1 parent 139dfb0 commit f9c082c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
[Lib](lib.rs)

- [Chapter 1](./chapter1.rs)
- [Chapter 1 - 1](./chapter1/chapter1_1.rs)
- [Chapter 1 - 1](./chapter1/chapter1_1.md)
2 changes: 0 additions & 2 deletions examples/book/src/chapter1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod chapter1_1;

pub fn body() {
// Test
// Test
Expand Down
6 changes: 6 additions & 0 deletions examples/book/src/chapter1/chapter1_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Chapter 1.1

- List 1
- List 2

Test
3 changes: 0 additions & 3 deletions examples/book/src/chapter1/chapter1_1.rs

This file was deleted.

10 changes: 9 additions & 1 deletion examples/book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ pub fn body() {
// xyzabc
let _w = 0;

/*
x
- List 0
- List 1
- List 1 1
- List 1 2
*/
// Test
let x = 1;
// Test2
//
// - List 0
// - List 1
//- List 1
// - List 1 1
// - List 1 2
//
//Test4
let y = 1;

Expand Down
17 changes: 15 additions & 2 deletions packages/mdbook-rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,21 @@ fn write_comment(comment: ast::Comment, prefix: &str) -> String {
let comment_text = match comment.kind().shape {
ast::CommentShape::Line => comment_suffix,
ast::CommentShape::Block => comment_suffix.strip_suffix("*/").unwrap_or(comment_suffix),
};

let mut lines = comment_text.split('\n');
let mut output = String::new();

if let Some(first_line) = lines.next() {
output.push_str(first_line.strip_prefix(' ').unwrap_or(first_line));
}
.trim_start();

write_lines(comment_text, prefix)
for line in lines {
output.push('\n');
output.push_str(line.strip_prefix(prefix).unwrap_or(line))
}

output
}

fn parse_module(source_text: &str) -> Result<SourceFile> {
Expand Down Expand Up @@ -267,3 +278,5 @@ fn expect_kind(
bail!("Unexpected token")
}
}

// TODO: Tests

0 comments on commit f9c082c

Please sign in to comment.