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 Apr 25, 2024
1 parent 742ed5d commit e134786
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
109 changes: 109 additions & 0 deletions src/snapshots/nixdoc__test__headings.snap.new
Original file line number Diff line number Diff line change
@@ -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
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 e134786

Please sign in to comment.