Skip to content

[critical] Code block parsing error. #327

Closed

Description

@skywind3000

Markdown allows me to insert code block between three backticks (```) , and if my code exactly contains three backticks, I can put them between four or more consecutive backticks like:

`````
```cpp
int x = 10;
```
`````

In another word, if my code block is starting with N (N >= 3) backtick sequence, parser should look for another backtick sequence with exact the same length (N) to determine where the code block ends.

Markdown2 works fine by default:

text = """
`````
```cpp
int x = 10;
```
`````
"""

print(markdown2.markdown(text))

The output is as expected:

<p><code>
```cpp
int x = 10;
```
</code></p>

But if I use fenced-code-blocks extra:

print(markdown2.markdown(text, extras = ['fenced-code-blocks', 'highlightjs-lang']))

The output is totally messed up:

<p>`````</p>

<pre><code class="cpp">int x = 10;
</code></pre>

<p>`````</p>

The correct output should be:

<pre><code>
```cpp
int x = 10;
```
</code></pre>

Both python-markdown and pandoc can handle this correctly, while markdown2 can't.

But they are not as portable as markdown2.

It is important to fixed this issue in fenced-code-blocks

Activity

  1. nicholasserra commented on May 6, 2019

    @nicholasserra
    Collaborator

    Thanks for the report! PRs welcome :)

  2. skywind3000 commented on May 6, 2019

    @skywind3000
    Author

    I wish if I could. But I am not capable for this, not skilled in parsing text and regexp.

    The only thing I can do is using a lot of str.find to replace old code, it may work but I believe that you will not like it.

  3. skywind3000 commented on May 7, 2019

    @skywind3000
    Author

    Now I use an ugly preprocessor before passing text to markdown2

    (EDIT: deleted, see the next post)
    

    This is just an ugly work around which I am using right now.

  4. skywind3000 commented on Jun 25, 2019

    @skywind3000
    Author

    The previous preprocessor has some defects, here is the correct one:

    def markdown_fenced_code_block(content, tabsize = 4):
        import re
        output = []
        source = []
        state = 0
        mark = ''
        lang = None
        p1 = re.compile(r'^\s{0,3}```*')
        p2 = re.compile(r'^\s{0,3}~~~*')
        for line in content.split('\n'):
            line = line.rstrip('\r\n\t ')
            if state == 0:
                m1 = re.match(p1, line)
                m2 = re.match(p2, line)
                mm = m1 and m1 or m2
                if not mm:
                    output.append(line)
                    continue
                span = mm.span()
                mark = line[:span[1]]
                lang = line[span[1]:].strip()
                state = 1
                source = []
            elif state == 1:
                if not line.startswith(mark):
                    source.append(line.expandtabs(tabsize))
                    continue
                src = '\n'.join(source).strip('\n')
                head = '<pre><code>'
                if lang:
                    head = '<pre><code class="%s">'%lang
                replacements = [
                    ("&amp;", "&"),
                    ("&lt;", "<"),
                    ("&gt;", ">"),
                    ("&#96;", "`"),
                ]
                for new, old in replacements:
                    src = src.replace(old, new)
                output.append(head + src)
                output.append('</code></pre>')
                state = 0
        return '\n'.join(output)

    This one works very well as a preprocessor of markdown2

  5. skywind3000 commented on Sep 6, 2019

    @skywind3000
    Author

    @nicholasserra PR is ready:

    #333

  6. added a commit that references this issue on Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions