diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b18916a..9e9c86d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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' diff --git a/README.md b/README.md index 82ae3f8..b770ec6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 5970e0e..b689bee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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}" diff --git a/src/mdformat_gfm/plugin.py b/src/mdformat_gfm/plugin.py index cfc0833..4bc1734 100644 --- a/src/mdformat_gfm/plugin.py +++ b/src/mdformat_gfm/plugin.py @@ -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, +} diff --git a/tests/data/default_style.md b/tests/data/default_style.md index 43beb0a..5f7e6f8 100644 --- a/tests/data/default_style.md +++ b/tests/data/default_style.md @@ -78,3 +78,12 @@ test.com:443 . test.com:443 . + +Tasklist escape +. +- [x] foo +- \[ ] bim +. +- [x] foo +- \[ \] bim +.