DavidAnson / markdownlint-cli2

A fast, flexible, configuration-based command-line interface for linting Markdown/CommonMark files with the markdownlint library
MIT License
339 stars 46 forks source link

Wrong positive MD049 (emphasis style) with `$$` math blocks #162

Closed jannismain closed 1 year ago

jannismain commented 1 year ago

In Markdown documents that include Mathjax formulas, defining math blocks like this is quite common:

# Heading

Blabla (*foo*)

$$
q^{'}_{i} = q^{'}_{i+1}
$$

Expected Behavior

No warning should be raised, as the document looks quite reasonable.

Actual Behavior

Running markdownlint-cli2 on this document raises a seemingly unrelated issue:

$ markdownlint-cli2 markdownlint-example.md
markdownlint-cli2 v0.5.1 (markdownlint v0.26.2)
Finding: markdownlint-example.md
Linting: 1 file(s)
Summary: 1 error(s)
markdownlint-example.md:6:6 MD049/emphasis-style Emphasis style should be consistent [Expected: asterisk; Actual: underscore]
jannismain commented 1 year ago

So first I though it would just not handle the custom $$ block syntax correctly.

However, when slightly shortening the formula by removing the exponents (but leaving the subscripts!) like this:

# Heading

Blabla (*foo*)

$$
q_{i} = q_{i+1}
$$

the false positive MD049 warning doesn't appear:

$ markdownlint-cli2 markdownlint-example.md
markdownlint-cli2 v0.5.1 (markdownlint v0.26.2)
Finding: markdownlint-example.md
Linting: 1 file(s)
Summary: 0 error(s)

This leads me to believe that this might be a bug.

nschonni commented 1 year ago

https://github.com/DavidAnson/markdownlint/issues/597#issuecomment-1268709867

DavidAnson commented 1 year ago

There's kind of a lot going on here. Nick good to point out you shouldn't see this if you try using the VS Code extension because it implicitly includes a plug-in for parsing math blocks. CLI2 does not implicitly include plug-ins, so it thinks the content of the math block is normal Markdown. If you install and reference that plug-in in your scenario, this warning should go away. There are examples in the repo for how to do so.

What makes this scenario a little different is that MD049 does use the parser whereas the rule in the referenced issue does not. As I continue the migration to micromark, I will probably reference a similar plug-in implicitly so this scenario "just works" even in CLI2.

DavidAnson commented 1 year ago

In conclusion, I would not consider this a bug assuming referencing the texmath plug-in suppresses the warning.

jannismain commented 1 year ago

Thanks for the pointer towards the texmath plugin. With the texmath plugin enabled, MD049 is no longer raised.

For everybody experiencing the same behavior, I have created a gist that quickly outlines the problem and solution.

DavidAnson commented 1 year ago

Thanks!

jannismain commented 1 year ago

With the configuration included in the gist linked above:

{
  "markdownItPlugins": [
    ["markdown-it-texmath"]
  ]
}

the CLI works as expected now. However, the vscode extension now acts up:

[8:59:32 PM] ERROR: Exception while linting with markdownlint-cli2:
Error: Cannot find module 'markdown-it-texmath'
Require stack:
...

I have installed the requirements globally

$ npm -g list         
/opt/homebrew/lib
...
+-- katex@0.16.7
+-- markdown-it-texmath@1.0.0
+-- markdownlint-cli2@0.5.1
...

Am I missing something here? Is it worth opening up another issue over at @DavidAnson/vscode-markdownlint

DavidAnson commented 1 year ago

See https://github.com/DavidAnson/vscode-markdownlint/issues/180

A local install of the plug-in (within the project as with other dependencies) should work for both scenarios.

jannismain commented 1 year ago

That fixed it! Thanks! I have updated the gist to suggest a local installation to keep compatibility with vscode-markdownlint.