eslint / eslint-plugin-markdown

Lint JavaScript code blocks in Markdown documents

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

eslint-plugin-markdown v2 doesn't lint code samples with `javascript` syntax

bmish opened this issue · comments

eslint-plugin-markdown v2 doesn't lint my documentation code samples at all when they have javascript syntax specified (this worked in v1). It only lints code samples with js syntax specified.

i.e.

# Title

```javascript
var x = 123;
```fake-name-to-avoid-breaking-rendering-of-this-block

Can you give me more information about your ESLint version (6 or 7), your configuration, and the command you're using to run eslint? ESLint by default only lints *.js files, and this plugin also tells it to look for *.md files. So ESLint treats your js code blocks as regular *.js files, but by default it doesn't do anything with *.javascript files. Take a look at the --exit option. If you pass --ext .js,.md,.javascript, do javascript code blocks get linted? If you're using ESLint v7, you can also add *.javascript to an overrides configuration so you don't have to pass the --ext option.

Edit: v1 of this plugin had a hard-coded list of extensions that it would check. That's now fully configurable within ESLint. Check out the migration section of the README for more details. If you want to mimic the old behavior, there's an example configuration near the bottom of that section.

Edit 2: I see from #174 that you're using ESLint v7 with the recommended config from this plugin. You should be able to borrow the overrides example from the end of the migration section of the README to get the v1 behavior that includes javascript code blocks.

I added a language mapper in this PR #178, it may help, I think it's very common usage.

How about adding a custom mapper support like https://github.com/mdx-js/eslint-mdx/pull/291/files#diff-73ff12864790ccf215d39920bfae4f63a0fdf9f1ba2ffc9e35ff6ef6520a3d30R13?

What means users should be able to define there own language mapper.

No updates from the original author, so I'll assume the previous response solved this.

@JounQin we used to have something like a language mapper and moved away from that. v1 of this plugin used ESLint's old processor API. This plugin hard-coded all the extensions for both Markdown files (.md and a few others) and code blocks (.js, .jsx, and a few others). All code blocks would be interpreted by ESLint as JS. That was extremely limiting for users because if their file extension or code block syntax wasn't in the hard-coded list, the only way to lint it was to make a PR to this repository, hope it gets accepted, and wait for a release.

With the new API now in v2, users can configure whatever extensions they'd like for both Markdown files and code blocks. In the common *.md case, the recommended config will Just Work, and ESLint will lint code blocks with whatever extensions they've already configured - ```js/jsx/ts - whatever they pass in --ext or match in overrides.

That's OK, I changed to use this custom feature in eslint-plugin-mdx which supports .md files at the same time instead.