BenjaminHoegh / ParsedownExtended

ParsedownExtended is an extention for Parsedown, offering additional features and functionalities.
https://benjaminhoegh.github.io/ParsedownExtended
MIT License
37 stars 7 forks source link

Hash characters in code blocks make headings in ToC #17

Closed KoljaL closed 3 years ago

KoljaL commented 4 years ago

I may have found an issue, because if the markdown page contains code blocks that uses hash characters for commenting out, all these lines will be displayed as headings in the ToC.

Example.md:

# All my Python Code
## my function
def my_function():
  print("Hello from a function") 

makes this HTML Code:


<li><a href="#all-my-python-code">All my Python Code</a><ul>
<li><a href="#my-function">my function</a></li>
</ul>
</li>
KoljaL commented 4 years ago

maybe a little dirty, but it seems to work:

ParsedownExtended.php:630

            // var for code case
            $is_code = false;

            foreach ($lines as $headerLine) {

              // if code detected (line starts with "``") toggle code var
              if (strspn($headerLine, '``') > 0)
              {
                $is_code = !$is_code;
              }

                // only search for headers, if code var is false
                if (($is_code == false)   &&   ((strspn($headerLine, '#') > 0) || (strspn($headerLine, '=') >= 3) || (strspn($headerLine, '-') >= 3))){
BenjaminHoegh commented 4 years ago

Hello @KoljaL i have rewritten the ToC function from top to bottom and just like to hear what features you would like me to include in the ToC

KoljaL commented 4 years ago

Thanks for the question, but when you've fixed the code block problem, I'll be happy. The only thing I can think of as a new (and perhaps useful) function is a "highlighting the current position on page in the TOC", with a bold font, or something. But JavaScript is necessary for this and that takes away the elegance of the whole thing.