Skip to content

Commit

Permalink
headings: headings can have up to 3 leading whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed May 16, 2024
1 parent 742ed5d commit 686e696
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn shift_headings(raw: &str, levels: usize) -> String {
let mut curr_fence: Option<(usize, char)> = None;
for raw_line in raw.split_inclusive('\n') {
// Code blocks can only start with backticks or tildes
// code fences can be indented by 0-3 spaces see commonmark spec.
let fence_line = &trim_leading_whitespace(raw_line, 3);
if fence_line.starts_with("```") | fence_line.starts_with("~~~") {
let fence_info = get_fence(fence_line, true);
Expand All @@ -90,8 +91,11 @@ 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);
// Remove up to 0-3 leading whitespaces.
// If the line has 4 or more whitespaces it is not a heading according to commonmark spec.
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);
Expand All @@ -100,7 +104,7 @@ pub fn shift_headings(raw: &str, levels: usize) -> String {
result
}

/// Removes leading whitespaces from code fences
/// Removes leading whitespaces from code fences if present
/// However maximum of [max] whitespaces are removed.
/// This is useful for code fences may have leading whitespaces (0-3).
fn trim_leading_whitespace(input: &str, max: usize) -> String {
Expand Down Expand Up @@ -145,7 +149,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 {
Expand Down
14 changes: 14 additions & 0 deletions src/snapshots/nixdoc__test__headings.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@ expression: output

#### 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
Expand All @@ -23,6 +35,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
Expand Down
14 changes: 14 additions & 0 deletions test/headings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 686e696

Please sign in to comment.