Skip to content

Commit

Permalink
Add MDX support
Browse files Browse the repository at this point in the history
Closes GH-270.
Closes GH-271.
  • Loading branch information
wooorm committed Aug 23, 2020
1 parent 13c870a commit 3894be8
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
16 changes: 15 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var unified = require('unified')
var markdown = require('remark-parse')
var html = require('rehype-parse')
var frontmatter = require('remark-frontmatter')
var mdx = require('remark-mdx')
var english = require('retext-english')
var remark2retext = require('remark-retext')
var rehype2retext = require('rehype-retext')
Expand All @@ -30,6 +31,7 @@ var textExtensions = [
'ron'
]
var htmlExtensions = ['htm', 'html']
var mdxExtensions = ['mdx']

// Update messages.
notifier({pkg: pack}).notify()
Expand All @@ -45,6 +47,7 @@ var cli = meow(
' -q, --quiet output only warnings and errors',
' -t, --text treat input as plain-text (not markdown)',
' -l, --html treat input as html (not markdown)',
' --mdx treat input as mdx (not markdown)',
' -d, --diff ignore unchanged lines (affects Travis only)',
' --stdin read from stdin',
'',
Expand All @@ -62,6 +65,7 @@ var cli = meow(
help: {type: 'boolean', alias: 'h'},
stdin: {type: 'boolean'},
text: {type: 'boolean', alias: 't'},
mdx: {type: 'boolean'},
html: {type: 'boolean', alias: 'l'},
diff: {type: 'boolean', alias: 'd'},
quiet: {type: 'boolean', alias: 'q'},
Expand All @@ -71,7 +75,11 @@ var cli = meow(
)

// Set-up.
var extensions = cli.flags.html ? htmlExtensions : textExtensions
var extensions = cli.flags.html
? htmlExtensions
: cli.flags.mdx
? mdxExtensions
: textExtensions
var defaultGlobs = ['{docs/**/,doc/**/,}*.{' + extensions.join(',') + '}']
var silentlyIgnore
var globs
Expand Down Expand Up @@ -127,6 +135,12 @@ function transform(options) {

if (cli.flags.html) {
plugins = [html, [rehype2retext, unified().use({plugins: plugins})]]
} else if (cli.flags.mdx) {
plugins = [
markdown,
mdx,
[remark2retext, unified().use({plugins: plugins})]
]
} else if (!cli.flags.text) {
plugins = [
markdown,
Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var VFile = require('vfile')
var unified = require('unified')
var markdown = require('remark-parse')
var frontmatter = require('remark-frontmatter')
var mdx = require('remark-mdx')
var html = require('rehype-parse')
var english = require('retext-english')
var equality = require('retext-equality')
Expand All @@ -16,6 +17,7 @@ var filter = require('./filter')
module.exports = alex
alex.text = noMarkdown
alex.markdown = alex
alex.mdx = mdxParse
alex.html = htmlParse

function makeText(config) {
Expand Down Expand Up @@ -63,6 +65,15 @@ function alex(value, config) {
)
}

// Alex, for MDX.
function mdxParse(value, config) {
return core(
value,
config,
unified().use(markdown).use(mdx).use(remark2retext, makeText(config))
)
}

// Alex, for HTML.
function htmlParse(value, config) {
return core(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"rehype-parse": "^7.0.0",
"rehype-retext": "^2.0.1",
"remark-frontmatter": "^2.0.0",
"remark-mdx": "^2.0.0-next.7",
"remark-message-control": "^6.0.0",
"remark-parse": "^8.0.0",
"remark-retext": "^4.0.0",
Expand Down
50 changes: 48 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Give **alex** a spin on the [Online demo »][demo].
* [x] Helps to get better at considerate writing
* [x] Catches many possible offences
* [x] Suggests helpful alternatives
* [x] Reads plain text, HTML, and markdown as input
* [x] Reads plain text, HTML, MDX, or markdown as input
* [x] Stylish

## Install
Expand Down Expand Up @@ -60,6 +60,7 @@ Or you can follow this step-by-step tutorial:
* [API](#api)
* [`alex(value, config)`](#alexvalue-config)
* [`alex.markdown(value, config)`](#alexmarkdownvalue-config)
* [`alex.mdx(value, config)`](#alexmdxvalue-config)
* [`alex.html(value, config)`](#alexhtmlvalue-config)
* [`alex.text(value, config)`](#alextextvalue-config)
* [Workflow](#workflow)
Expand Down Expand Up @@ -107,7 +108,7 @@ like][literals] are not warned about.
* Ember — [`yohanmishkin/ember-cli-alex`](https://github.com/yohanmishkin/ember-cli-alex)
* Probot — [`swinton/linter-alex`](https://github.com/swinton/linter-alex)
* GitHub Actions — [`brown-ccv/alex-recommends`](https://github.com/marketplace/actions/alex-recommends)
* GitHub Actions (reviewdog) — [`reviewdog/action-alex`](https://github.com/marketplace/actions/run-alex-with-reviewdog)
* GitHub Actions (reviewdog) — [`reviewdog/action-alex`](https://github.com/marketplace/actions/run-alex-with-reviewdog)
* Vim — [`w0rp/ale`](https://github.com/w0rp/ale)
* Browser extension — [`skn0tt/alex-browser-extension`](https://github.com/skn0tt/alex-browser-extension)
* Contentful - [`stefanjudis/alex-js-contentful-ui-extension`](https://github.com/stefanjudis/alex-js-contentful-ui-extension)
Expand Down Expand Up @@ -309,6 +310,7 @@ See `$ alex --help` for more information.

> When no input files are given to **alex**, it searches for files in the
> current directory, `doc`, and `docs`.
> If `--mdx` is given, it searches for `mdx` extensions.
> If `--html` is given, it searches for `htm` and `html` extensions.
> Otherwise, it searches for `txt`, `text`, `md`, `mkd`, `mkdn`, `mkdown`,
> `ron`, and `markdown` extensions.
Expand Down Expand Up @@ -369,6 +371,46 @@ Yields:
]
```

### `alex.mdx(value, config)`

Check [MDX][] (ignoring syntax).

> Note: the syntax for [MDX@2][mdx-next], while currently in beta, is used in
> alex.
###### Parameters

* `value` ([`VFile`][vfile] or `string`) — MDX document
* `config` (`Object`, optional) — See the [Configuration][] section

###### Returns

[`VFile`][vfile].

###### Example

```js
alex.mdx('<Component>He walked to class.</Component>').messages
```

Yields:

```js
[
[1:12-1:14: `He` may be insensitive, use `They`, `It` instead] {
reason: '`He` may be insensitive, use `They`, `It` instead',
line: 1,
column: 12,
location: { start: [Object], end: [Object] },
source: 'retext-equality',
ruleId: 'he-she',
fatal: false,
actual: 'He',
expected: [ 'They', 'It' ]
}
]
```

### `alex.html(value, config)`

Check HTML (ignoring syntax).
Expand Down Expand Up @@ -635,3 +677,7 @@ Lots of [people helped since][contributors]!
[ignoring-files]: #ignoring-files

[alexignore]: #alexignore

[mdx]: https://mdxjs.com

[mdx-next]: https://github.com/mdx-js/mdx/issues/1041
20 changes: 20 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var test = require('tape')
var alex = require('..')

var html = fs.readFileSync(path.join(__dirname, 'fixtures', 'three.html'))
var mdx = fs.readFileSync(path.join(__dirname, 'fixtures', 'four.mdx'))

// Tests. Note that these are small because alex is in fact
// just a collection of well-tested modules.
Expand Down Expand Up @@ -318,5 +319,24 @@ test('alex()', function (t) {
'alex.html() with deny and profanity config'
)

t.deepEqual(
alex.mdx(mdx).messages.map(String),
[
'3:1-3:4: `She` may be insensitive, use `They`, `It` instead',
'3:32-3:38: Don’t use `asshat`, it’s profane',
'3:70-3:74: Be careful with `butt`, it’s profane in some cases'
],
'alex.mdx()'
)

t.deepEqual(
alex.mdx(mdx, ['butt']).messages.map(String),
[
'3:1-3:4: `She` may be insensitive, use `They`, `It` instead',
'3:32-3:38: Don’t use `asshat`, it’s profane'
],
'alex.mdx() with options'
)

t.end()
})
16 changes: 16 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ test('alex-cli', function (t) {
}
})

t.test('mdx', function (t) {
var fp = path.join('test', 'fixtures')

t.plan(1)

childProcess.exec('./cli.js ' + fp + ' --mdx', onexec)

function onexec(err, stdout, stderr) {
t.deepEqual(
[err.code, /2 warnings/.test(stderr), stdout],
[1, true, ''],
'should work'
)
}
})

t.test('successful', function (t) {
var fp = path.join('test', 'fixtures', 'ok.txt')

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/four.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Component disabled>We’ve confirmed `his` identity.</Component>

She walked to class. Eric, the asshat, is pretty set on beating your butt
for the sheriff.

1 comment on commit 3894be8

@shreyasminocha
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🎉

Please sign in to comment.