layoutformattingmarkdownpre-commitlinter

How to handle wide markdown tables and line-length checks in pre-commit?


Context

After applying a line length limit of 80 characters on the pre-commit check of markdown-lint, I was experiencing some difficulties in including a markdown table that I create with more width than 80 characters.

Note

I see value in applying the linter to the README.md because I quite often forget about the line length while typing the README.md. (In essence, the trivial solution: disable the linter or disable MD013 everywhere, is considered sub-optimal).

Pre-commit of MarkdownLint

- repo: https://github.com/markdownlint/markdownlint
   rev: v0.11.0
   hooks:
     - id: markdownlint

Markdown table example

| Algorithm                            | Encoding | Adaptation | Radiation    | Backend                      |
| ------------------------------------ | -------- | ---------- | ------------ | ---------------------------- |
| Minimum Dominating Set Approximation | Sparse   | Redundancy | Neuron Death | - networkx LIF<br>- Lava LIF |
| Some Algorithm Approximation         | Sparse   | Redundancy | Neuron Death | - networkx LIF<br>- Lava LIF |
|                                      |          |            |              |                              |

Approach I

First I tried to include a ignore MD013 (line length check) in the relevant section of the Markdown table, however, Markdown Lint does not support such an option.

Approach II

I tried to manually apply the new line breaks to the table, however, that results in additional rows in the table: enter image description here

Question

How can I stay within the 80 lines whilst including a wide markdown table, (without generating new horizontal lines)?


Solution

  • You could try changing your hook to another similar project: igorshubovych/markdownlint-cli

    repos:
      - repo: https://github.com/igorshubovych/markdownlint-cli
        rev: v0.32.2
        hooks:
          - id: markdownlint
            args: ["--fix"]
    

    You may include a .markdownlint.yaml file in the same directory as your .pre-commit-config.yaml. Set the line length rule but ignore it for tables. Like so:

    # Default state for all rules
    default: true
    
    # MD013/line-length - Line length
    MD013:
      line_length: 80
      tables: false
    

    Check the .markdownlint.yaml schema for other configuration options.