executablebooks / mdformat

CommonMark compliant Markdown formatter
https://mdformat.rtfd.io
MIT License
413 stars 45 forks source link

Escape "\|" not working in table cell #398

Closed Godsing closed 1 year ago

Godsing commented 1 year ago

Describe the bug

Badcase

context When I run the following code:

md_doc = r"""
| variable | type |
| --- | --- |
| ids | List \| Tuple |
"""
print(mdformat.text(md_doc))

expectation Expected output:

| variable | type |
| --- | --- |
| ids | List \| Tuple |

bug But instead, its output:

| variable | type |
| --- | --- |
| ids | List | Tuple |

Goodcase

But the following code work well:

md_doc = r"""
1\. hello
2\. world
"""
print(mdformat.text(md_doc))

output:

1\. hello
2\. world

Debug

I have debugged and found that it may be fixed by modifying the paragraph function in the "mdformat/renderer/_context.py" file.

The Goodcase works well because of the code lines[i] = lines[i].replace(".", "\\.", 1) in the paragraph function.

Reproduce the bug

See above.

List your environment

No response

welcome[bot] commented 1 year ago

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

hukkin commented 1 year ago

Hi!

This seems to work fine with mdformat-tables:

foo@bar:~$ python3 -m venv venv
foo@bar:~$ source venv/bin/activate
(venv) foo@bar:~$ pip install mdformat mdformat-tables
Collecting mdformat
  Using cached mdformat-0.7.16-py3-none-any.whl (29 kB)
Collecting mdformat-tables
  Using cached mdformat_tables-0.4.1-py3-none-any.whl (4.6 kB)
Collecting markdown-it-py<3.0.0,>=1.0.0
  Using cached markdown_it_py-2.2.0-py3-none-any.whl (84 kB)
Collecting tomli>=1.1.0
  Using cached tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting mdurl~=0.1
  Using cached mdurl-0.1.2-py3-none-any.whl (10.0 kB)
Installing collected packages: tomli, mdurl, markdown-it-py, mdformat, mdformat-tables
Successfully installed markdown-it-py-2.2.0 mdformat-0.7.16 mdformat-tables-0.4.1 mdurl-0.1.2 tomli-2.0.1
(venv) foo@bar:~$ mdformat --version
mdformat 0.7.16 (mdformat_tables: 0.4.1)
(venv) foo@bar:~$ printf '| variable | type |\n| --- | --- |\n| ids | List \| Tuple |\n' | mdformat -
| variable | type          |
| -------- | ------------- |
| ids      | List \| Tuple |

The table syntax is not CommonMark so you'll need the table plugin. See https://mdformat.readthedocs.io/en/stable/users/plugins.html#parser-extension-plugins

Godsing commented 1 year ago

Hi! ... The table syntax is not CommonMark so you'll need the table plugin. See https://mdformat.readthedocs.io/en/stable/users/plugins.html#parser-extension-plugins表格语法不是 CommonMark,因此您需要表格插件。见 https://mdformat.readthedocs.io/en/stable/users/plugins.html#parser-extension-plugins

Thanks!

Now I see. I need to explicitly enable the tables extension! I added the parameter extensions={"tables"} to mdformat.text, and it worked.