From e13478608548acfeb7d51ae735bbf7c7ded35943 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Thu, 25 Apr 2024 14:34:03 +0200 Subject: [PATCH] headings: headings can have up to 3 leading whitespace --- src/format.rs | 6 +- src/snapshots/nixdoc__test__headings.snap.new | 109 ++++++++++++++++++ test/headings.md | 14 +++ 3 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/snapshots/nixdoc__test__headings.snap.new diff --git a/src/format.rs b/src/format.rs index 690e2e8..bbe85aa 100644 --- a/src/format.rs +++ b/src/format.rs @@ -90,8 +90,9 @@ pub fn shift_headings(raw: &str, levels: usize) -> String { } } - if curr_fence.is_none() && raw_line.starts_with('#') { - let heading = handle_heading(raw_line, levels); + let heading_line = &trim_leading_whitespace(raw_line, 3); + if curr_fence.is_none() && heading_line.starts_with('#') { + let heading = handle_heading(heading_line, levels); result.push_str(&heading); } else { result.push_str(raw_line); @@ -145,7 +146,6 @@ pub fn get_fence(line: &str, allow_info: bool) -> Option<(usize, char)> { pub fn handle_heading(line: &str, levels: usize) -> String { let chars = line.chars(); - // let mut leading_trivials: String = String::new(); let mut hashes = String::new(); let mut rest = String::new(); for char in chars { diff --git a/src/snapshots/nixdoc__test__headings.snap.new b/src/snapshots/nixdoc__test__headings.snap.new new file mode 100644 index 0000000..efc82f0 --- /dev/null +++ b/src/snapshots/nixdoc__test__headings.snap.new @@ -0,0 +1,109 @@ +--- +source: src/test.rs +assertion_line: 168 +expression: output +--- +### h1-heading + +#### h2-heading + +3 leading whitespaces are okay for headings + +#### h2 heading + + # code block + + +#### h2-heading-with-id {#some-id} + +Indented code block + + # Code comment + a = 1; + +##### h3-heading + +```nix +# A comment should not be shifted +``` + +##### annother heading + +``` +# Some pseudocode +map a from b -> 1 +``` + +##### indented (0-3) fences + +3 leading whitespaces are okay for code fences + + ``` lang info +# Some pseudocode +map a from b -> 1 + ``` + +##### indented (0-3) fences asymmetric + +``` +# Some pseudocode +map a from b -> 1 + ``` + +##### More closing fences than opening + +```` +# Some pseudocode +map a from b -> 1 +``````` + +##### Some heading + +````nix +/** + ```nix + # A nested comment should not be shifted + ``` +*/ +1 +# A comment +```` + +###### h4-heading + +Nested tilde fences + +~~~~~nix +/* + ~~~~nix + /** + ~~~nix + # A nested comment should not be shifted + 42 + ~~~ + */ + 1 + # A nested comment ^ + ~~~~ +*/ +# A comment ^ +foo +~~~~~ + +###### h5-heading + +Mixed fences + +~~~nix +/** + ```nix + # A nested comment should not be shifted + ``` +*/ +1 +# A comment +~~~ + +###### h6-heading + +This should be h6 as well diff --git a/test/headings.md b/test/headings.md index 7e606a7..9f8265a 100644 --- a/test/headings.md +++ b/test/headings.md @@ -2,8 +2,20 @@ ## h2-heading +3 leading whitespaces are okay for headings + + ## h2 heading + + # code block + + ## h2-heading-with-id {#some-id} +Indented code block + + # Code comment + a = 1; + ### h3-heading ```nix @@ -19,6 +31,8 @@ map a from b -> 1 ### indented (0-3) fences +3 leading whitespaces are okay for code fences + ``` lang info # Some pseudocode map a from b -> 1