Skip to content

Commit

Permalink
Support unreleased mdformat 0.7.19 that escapes square brackets less
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Oct 27, 2024
1 parent b3fc3b2 commit b8428ea
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
hooks:
- id: docformatter
- repo: https://github.com/executablebooks/mdformat
rev: 08fba30538869a440b5059de90af03e3502e35fb # frozen: 0.7.17
rev: 86542e37a3a40974eb812b16b076220fe9bb4278 # frozen: 0.7.18
hooks:
- id: mdformat
files: 'README.md'
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ making the tool able to format the following syntax extensions:
## Install

```sh
pip install mdformat-gfm
pipx install mdformat
pipx inject mdformat mdformat-gfm
```

## Usage
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ description = "Run tests under {base_python}"
deps = ["-r tests/requirements.txt"]
commands = [["pytest", { replace = "posargs", extend = true }]]

[tool.tox.env."git-mdformat"]
description = "Run tests against unreleased mdformat from git"
deps = [
"-r tests/requirements.txt",
"git+https://github.com/executablebooks/mdformat.git@master",
"git+https://github.com/executablebooks/mdformat-tables.git@master",
]

[tool.tox.env."mypy"]
base_python = ["python3.11"]
description = "Run mypy under {base_python}"
Expand Down
16 changes: 15 additions & 1 deletion src/mdformat_gfm/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,23 @@ def _escape_text(text: str, node: RenderTreeNode, context: RenderContext) -> str
return text


_RE_GFM_TICK_BOX = re.compile(r"^\[([ xX])]", flags=re.MULTILINE)


def _escape_paragraph(text: str, node: RenderTreeNode, context: RenderContext) -> str:
# Escape tasklists
text = _RE_GFM_TICK_BOX.sub(r"\[" + r"\g<1>" + r"\]", text)

return text


RENDERERS = {
"s": _strikethrough_renderer,
"list_item": _list_item_renderer,
"link": _link_renderer,
}
POSTPROCESSORS = {"text": _escape_text, "inline": _postprocess_inline}
POSTPROCESSORS = {
"text": _escape_text,
"inline": _postprocess_inline,
"paragraph": _escape_paragraph,
}
9 changes: 9 additions & 0 deletions tests/data/default_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ test.com:443
.
test.com:443
.

Tasklist escape
.
- [x] foo
- \[ ] bim
.
- [x] foo
- \[ \] bim
.

0 comments on commit b8428ea

Please sign in to comment.