Skip to content

Commit

Permalink
bug: handle hash sign inside code blocks
Browse files Browse the repository at this point in the history
fixes #2
  • Loading branch information
jar-b committed Dec 28, 2021
1 parent 125038b commit 5cf8464
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog


## v0.2.1

### Fixed
- Ignore lines matching header regex when inside code blocks


## v0.2.0

### Added
Expand Down
32 changes: 32 additions & 0 deletions testdata/codeblock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Title
An example document with code blocks containing `#` characters to verify these are not included as TOC items.

## Heading 1

```
## plaintext
### lines
```

## Heading 2

```sh
## shell
### lines
#### with
##### comments
echo "skip this"
```

## Heading 3

```python
## python
### lines
#### with
##### comments

if __name__ == '__main__':
print('skip this')
```

37 changes: 37 additions & 0 deletions testdata/codeblock_toc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Title
An example document with code blocks containing `#` characters to verify these are not included as TOC items.

<!---mdtoc begin--->
* [Heading 1](#heading-1)
* [Heading 2](#heading-2)
* [Heading 3](#heading-3)
<!---mdtoc end--->
## Heading 1

```
## plaintext
### lines
```

## Heading 2

```sh
## shell
### lines
#### with
##### comments
echo "skip this"
```

## Heading 3

```python
## python
### lines
#### with
##### comments

if __name__ == '__main__':
print('skip this')
```

22 changes: 20 additions & 2 deletions toc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func (t *Toc) Insert(b []byte, force bool) ([]byte, error) {
var new []byte
buf := bytes.NewBuffer(new)

inOld := false
newAdded := false
var inOld, newAdded bool

r := bytes.NewReader(b)
scanner := bufio.NewScanner(r)
Expand Down Expand Up @@ -105,9 +104,28 @@ func (t *Toc) Insert(b []byte, force bool) ([]byte, error) {
func New(b []byte) (*Toc, error) {
toc := Toc{}

var inCodeBlock bool

r := bytes.NewReader(b)
scanner := bufio.NewScanner(r)
for scanner.Scan() {

// handle code blocks to ensure `#` are not captured as headings
// begin code block, set flag and skip
if strings.HasPrefix(scanner.Text(), "```") && !inCodeBlock {
inCodeBlock = true
continue
}
// end code block, reset flag and skip
if inCodeBlock && strings.HasPrefix(scanner.Text(), "```") {
inCodeBlock = false
continue
}
// code block line, skip
if inCodeBlock {
continue
}

m := headingRegex.FindStringSubmatch(scanner.Text())
if len(m) == 3 {
// m[0]: Full regular expression match
Expand Down
3 changes: 3 additions & 0 deletions toc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func TestInsert(t *testing.T) {
specialToc, _ := os.ReadFile("testdata/special_toc.md")
repeat, _ := os.ReadFile("testdata/repeat.md")
repeatToc, _ := os.ReadFile("testdata/repeat_toc.md")
codeblock, _ := os.ReadFile("testdata/codeblock.md")
codeblockToc, _ := os.ReadFile("testdata/codeblock_toc.md")

tt := []struct {
name string
Expand All @@ -25,6 +27,7 @@ func TestInsert(t *testing.T) {
{"basic", basic, false, basicToc, nil},
{"special", special, false, specialToc, nil},
{"repeat", repeat, false, repeatToc, nil},
{"code block", codeblock, false, codeblockToc, nil},
{"existing without force", basicToc, false, nil, ErrExistingToc},
{"existing with force", basicToc, true, basicToc, nil},
}
Expand Down

0 comments on commit 5cf8464

Please sign in to comment.