KyleKing/mdformat-mkdocs

Literal block content is re-formatted (wrongly)

pelson opened this issue · 2 comments

With the following content:

# A B C

1. Create a `.pre-commit-config.yaml` file in your repository and add the desired
   hooks. For example:

    ```yaml
    repos:
      - repo: https://github.com/psf/black
        rev: v24.4

    ```

Test

```yaml
file: abc.txt
```

Run with mdformat --align-semantic-breaks-in-lists --number, the content is re-formatted to:

# A B C

1. Create a `.pre-commit-config.yaml` file in your repository and add the desired
   hooks. For example:

    ```yaml
    repos:
      - repo: https://github.com/psf/black
      rev: v24.4

    ```

Test

```yaml
file: abc.txt
```

Note that the yaml content has been re-formatted. The re-formatting of the content within code blocks is not desirable (risky).

Note that if you try to re-format the re-formatted code, you get an error:

Warning: Failed formatting content of a yaml code block (line 6 before formatting). 

Yeah, that shouldn't be formatted and likely could be from an incomplete patch of #31. I'll add these as test cases and work on a fix when I next have time!

Also, this is possibly related to the error in #35

I can replicate the issue after first formatting with mdformat-config!

pipx install mdformat && pipx inject mdformat mdformat-config && mdformat tmp.md

Generates:

# A B C

1. Create a `.pre-commit-config.yaml` file in your repository and add the desired
   hooks. For example:

   \`\`\`yaml
   repos:
     - repo: https://github.com/psf/black
       rev: v24.4

   \`\`\`

The problem is that mdformat-mkdocs needs to correct the indentation to be a multiple of 4x instead of 3x, but doesn't properly indent rev: v24.4 for some reason, which also does seem to be a problem with ignoring code blocks:

1. List

   \`\`\`md
   # Title
   Content
   1. Numbered List
     * Unordered Sub-List
   \`\`\`

And the root cause is semantic_indents! Which wasn't respecting code blocks

Edited: code blocks within code blocks aren't handled by Github's renderer