Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception on comments with # prefix in code blocks #4

Open
GjjvdBurg opened this issue Feb 19, 2019 · 7 comments
Open

Exception on comments with # prefix in code blocks #4

GjjvdBurg opened this issue Feb 19, 2019 · 7 comments

Comments

@GjjvdBurg
Copy link

Hi!

Thanks for making this package! I just tried to use it but unfortunately ran into a bug when a line contains a # symbol in a code block. Here's a minimal example:

# Header

```python
# look a comment!
#

(ran into an issue with markdown-in-markdown here, so couldn't close the python block, but I hope you get the idea).

The first line in the code block is interpreted as a header and the second line causes the GithubEmptyLinkLabel exception to be raised. Both are undesired behavior I think. Thanks!

@frnmst
Copy link
Owner

frnmst commented Feb 19, 2019

Thank you

Yes I am aware of this bug. I need to add a check that tells the parser to ignore everything between code blocks. I hope to come up with something, soon.

@GjjvdBurg
Copy link
Author

I haven't tested this extensively, but this works for my example above:

diff --git a/md_toc/api.py b/md_toc/api.py
index 646aa45..ce99762 100644
--- a/md_toc/api.py
+++ b/md_toc/api.py
@@ -99,9 +99,16 @@ def build_toc(filename,
     toc = ''
     header_duplicate_counter = dict()
 
+    in_code = False
+
     with open(filename, 'r') as f:
         line = f.readline()
         while line:
+            if line.strip().startswith("```"):
+                in_code = not in_code
+            if in_code:
+                line = f.readline()
+                continue
             header = get_md_header(line, header_duplicate_counter,
                                    keep_header_levels, parser, no_links)
             if header is not None:

@frnmst
Copy link
Owner

frnmst commented Feb 21, 2019

Thanks a lot.

I will add your fix along with the unit tests in the next release. I will keep this issue open in the meantime.

See also:

@GjjvdBurg
Copy link
Author

Great, thanks for making this tool!

@frnmst
Copy link
Owner

frnmst commented Feb 27, 2019

@frnmst
Copy link
Owner

frnmst commented Mar 18, 2019

@frnmst
Copy link
Owner

frnmst commented Mar 29, 2019

Done for github,cmark,gitlab,commonmarker. Missing redcarpet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants