Skip to content

IndexError for markdown tables with \xa0 #629

@Opsi

Description

@Opsi

Describe the bug
When there is an \xa0 before an \n in a markdown table definition the markdown2.markdown function raises an IndexError.

To Reproduce

import markdown2

"""
| A   | B   |
|-----|-----|
| Foo | 123 |
"""

test = "| A   | B   |\n|-----|-----|\xa0\n| Foo | 123 |"
x = markdown2.markdown(test, extras=["tables", "break-on-newline"])
print(f"Rendered HTML: {x}")

This results in

Traceback (most recent call last):
  File "<ROOT>/foo.py", line 10, in <module>
    x = markdown2.markdown(test, extras=["tables", "break-on-newline"])
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<ROOT>/venv/lib/python3.12/site-packages/markdown2.py", line 207, in markdown
    use_file_vars=use_file_vars, cli=cli).convert(text)
                                          ^^^^^^^^^^^^^
  File "<ROOT>/venv/lib/python3.12/site-packages/markdown2.py", line 511, in convert
    text = self._run_block_gamut(text)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<ROOT>/venv/lib/python3.12/site-packages/markdown2.py", line 252, in inner
    text = func(md, text, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<ROOT>/venv/lib/python3.12/site-packages/markdown2.py", line 1221, in _run_block_gamut
    text = self._do_lists(text)
           ^^^^^^^^^^^^^^^^^^^^
  File "<ROOT>/venv/lib/python3.12/site-packages/markdown2.py", line 261, in inner
    text = extra.run(text)
           ^^^^^^^^^^^^^^^
  File "<ROOT>/venv/lib/python3.12/site-packages/markdown2.py", line 3385, in run
    return table_re.sub(self.sub, text)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<ROOT>/venv/lib/python3.12/site-packages/markdown2.py", line 3399, in sub
    if col[0] == ':' and col[-1] == ':':
       ~~~^^^
IndexError: string index out of range

Expected behavior
I feel like this should not raise an IndexError.

Debug info
Version of library being used: 2.5.3

Additional context

I used python 3.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions