derlin / bitdowntoc

Online and command-line Markdown TOC generator, with built-in support for BitBucket Server, GitHub, Gitlab, dev.to and more!
https://bitdowntoc.derlin.ch
Other
87 stars 8 forks source link

Must Add Space Before Ending Code Fence So That Following Header Appears In TOC #17

Closed OdyAsh closed 1 year ago

OdyAsh commented 1 year ago

If a language identifier (e.g., "Python") is used in a markdown's code fence, BitDownToc will not generate subsequent headers. Example: (ignore the backticks (``) at the start and at the end, they are just used with

` tags to escape the inner code fence).

```
# h1
abc
## h2
def

``` Python
    print("awesome video: https://www.youtube.com/watch?v=dQw4w9WgXcQ")
```

### h3
sad header

# h1 x 2
sadder header :[
```

will be converted to:

- [h1](#h1)
  * [h2](#h2)

However, the following:

```
# h1
abc
## h2
def

``` Python
    print("awesome video: https://www.youtube.com/watch?v=dQw4w9WgXcQ")
 ```
(^^^ NOTICE THE SPACE)

### h3
sad header

# h1 x 2
sadder header :[
 ```

will be converted to:

- [h1](#h1)
  * [h2](#h2)
    + [h3](#h3)
- [h1 x 2](#h1-x-2)

Additionally, removing Python also gives the correct TOC

derlin commented 1 year ago

Hello and thank you for the reporting.

I believe the problem lies in the space between the three backticks and the language identifier. Usually, in markdown, you use '''python (replace single quotes with backticks) and not ''' Python. What happens is the start of the first code block is not seen as a code block by BitDownToc, but the closing one is (and is treated as the beginning of a code block, thus ignoring everything until closed).

To avoid treating non-code blocks as code blocks, my rule is to use the regex '''([a-z0-9]+)?. I could change this and just treat whatever line starts with backticks as a code block, but I don't know if it may raise false positives. Do you see an example of a line beginning with three backticks which is NOT a code block?

I hesitate... In the meantime, can you simply change the ''' Python to '''python ? (this is the standard markdown syntax for highlighted code blocks).

OdyAsh commented 1 year ago

First of all, thanks for the quick reply; I've learned something new about markdown! 😅

Regarding this:

I Do you see an example of a line beginning with three backticks which is NOT a code block?

Not at the moment unfortunately.

In any case, I think adding a simple "side-note" anywhere in the website --- something like 'type "python" instead of " python"' --- will help people avoid this mistake.

However, as you said, what you suggested IS the standard markdown syntax, so you are completely justified to leave things as they are.

Thanks again for the quick reply! 🙌

derlin commented 1 year ago

Happy to help!

Thanks for using bitdowntoc, and if you haven't already, star the repo 😊

derlin commented 1 year ago

Changed the way bitdowntoc detects code blocks in https://github.com/derlin/bitdowntoc/commit/82e14e35804c3721aa729c1c0f35562ea0c54f74, the issue should not happen again.