Skip to content

Commit d2cd488

Browse files
committedOct 20, 2021
Use ESM
1 parent a799349 commit d2cd488

File tree

9 files changed

+175
-195
lines changed

9 files changed

+175
-195
lines changed
 

‎.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
coverage/
33
*.html
44
*.md
5+
*.mdx

‎cli.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#!/usr/bin/env node
2-
'use strict'
2+
import fs from 'fs'
3+
import notifier from 'update-notifier'
4+
import supportsColor from 'supports-color'
5+
import meow from 'meow'
6+
import engine from 'unified-engine'
7+
import unified from 'unified'
8+
import markdown from 'remark-parse'
9+
import html from 'rehype-parse'
10+
import frontmatter from 'remark-frontmatter'
11+
import mdx from 'remark-mdx'
12+
import english from 'retext-english'
13+
import remark2retext from 'remark-retext'
14+
import rehype2retext from 'rehype-retext'
15+
import defaultReporter from 'vfile-reporter'
16+
import equality from 'retext-equality'
17+
import profanities from 'retext-profanities'
18+
import diff from 'unified-diff'
19+
import {filter} from './filter.js'
320

4-
var notifier = require('update-notifier')
5-
var supportsColor = require('supports-color')
6-
var meow = require('meow')
7-
var engine = require('unified-engine')
8-
var unified = require('unified')
9-
var markdown = require('remark-parse')
10-
var html = require('rehype-parse')
11-
var frontmatter = require('remark-frontmatter')
12-
var mdx = require('remark-mdx')
13-
var english = require('retext-english')
14-
var remark2retext = require('remark-retext')
15-
var rehype2retext = require('rehype-retext')
16-
var defaultReporter = require('vfile-reporter')
17-
var equality = require('retext-equality')
18-
var profanities = require('retext-profanities')
19-
var diff = require('unified-diff')
20-
var pack = require('./package.json')
21-
var filter = require('./filter.js')
21+
const pack = JSON.parse(
22+
fs.readFileSync(new URL('./package.json', import.meta.url))
23+
)
2224

2325
var textExtensions = [
2426
'txt',

‎filter.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
'use strict'
1+
import remarkMessageControl from 'remark-message-control'
22

3-
var control = require('remark-message-control')
4-
5-
module.exports = filter
6-
7-
function filter(options) {
3+
export function filter(options) {
84
/* c8 ignore next */
95
var settings = options || {}
106

@@ -14,7 +10,7 @@ function filter(options) {
1410
)
1511
}
1612

17-
return control({
13+
return remarkMessageControl({
1814
name: 'alex',
1915
reset: Boolean(settings.deny),
2016
enable: settings.deny,

‎index.js

+30-33
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
1-
'use strict'
2-
3-
var VFile = require('vfile')
4-
var unified = require('unified')
5-
var markdown = require('remark-parse')
6-
var frontmatter = require('remark-frontmatter')
7-
var mdx = require('remark-mdx')
8-
var html = require('rehype-parse')
9-
var english = require('retext-english')
10-
var equality = require('retext-equality')
11-
var profanities = require('retext-profanities')
12-
var remark2retext = require('remark-retext')
13-
var rehype2retext = require('rehype-retext')
14-
var sort = require('vfile-sort')
15-
var filter = require('./filter.js')
16-
17-
module.exports = alex
18-
alex.text = noMarkdown
19-
alex.markdown = alex
20-
alex.mdx = mdxParse
21-
alex.html = htmlParse
1+
import VFile from 'vfile'
2+
import unified from 'unified'
3+
import remarkParse from 'remark-parse'
4+
import remarkFrontmatter from 'remark-frontmatter'
5+
import remarkMdx from 'remark-mdx'
6+
import rehypeParse from 'rehype-parse'
7+
import retextEnglish from 'retext-english'
8+
import retextEquality from 'retext-equality'
9+
import retextProfanities from 'retext-profanities'
10+
import remarkRetext from 'remark-retext'
11+
import rehypeRetext from 'rehype-retext'
12+
import sort from 'vfile-sort'
13+
import {filter} from './filter.js'
2214

2315
function makeText(config) {
2416
return unified()
25-
.use(english)
26-
.use(equality, {
17+
.use(retextEnglish)
18+
.use(retextEquality, {
2719
noBinary: config && config.noBinary
2820
})
29-
.use(profanities, {
21+
.use(retextProfanities, {
3022
sureness: config && config.profanitySureness
3123
})
3224
}
@@ -53,37 +45,42 @@ function core(value, config, processor) {
5345
return file
5446
}
5547

48+
export default markdown
49+
5650
// Alex.
57-
function alex(value, config) {
51+
export function markdown(value, config) {
5852
return core(
5953
value,
6054
config,
6155
unified()
62-
.use(markdown)
63-
.use(frontmatter, ['yaml', 'toml'])
64-
.use(remark2retext, makeText(config))
56+
.use(remarkParse)
57+
.use(remarkFrontmatter, ['yaml', 'toml'])
58+
.use(remarkRetext, makeText(config))
6559
)
6660
}
6761

6862
// Alex, for MDX.
69-
function mdxParse(value, config) {
63+
export function mdx(value, config) {
7064
return core(
7165
value,
7266
config,
73-
unified().use(markdown).use(mdx).use(remark2retext, makeText(config))
67+
unified()
68+
.use(remarkParse)
69+
.use(remarkMdx)
70+
.use(remarkRetext, makeText(config))
7471
)
7572
}
7673

7774
// Alex, for HTML.
78-
function htmlParse(value, config) {
75+
export function html(value, config) {
7976
return core(
8077
value,
8178
config,
82-
unified().use(html).use(rehype2retext, makeText(config))
79+
unified().use(rehypeParse).use(rehypeRetext, makeText(config))
8380
)
8481
}
8582

8683
// Alex, without the markdown.
87-
function noMarkdown(value, config) {
84+
export function text(value, config) {
8885
return core(value, config, makeText(config))
8986
}

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
"Christian Oliff <christianoliff@yahoo.com>",
5656
"Daan <daan@devign.it>"
5757
],
58+
"type": "module",
59+
"sideEffects": false,
5860
"bin": "cli.js",
5961
"files": [
6062
"index.js",

‎readme.md

+25-19
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ Or you can follow this step-by-step tutorial:
5858
* [Configuration](#configuration)
5959
* [CLI](#cli)
6060
* [API](#api)
61-
* [`alex(value, config)`](#alexvalue-config)
62-
* [`alex.markdown(value, config)`](#alexmarkdownvalue-config)
63-
* [`alex.mdx(value, config)`](#alexmdxvalue-config)
64-
* [`alex.html(value, config)`](#alexhtmlvalue-config)
65-
* [`alex.text(value, config)`](#alextextvalue-config)
61+
* [`markdown(value, config)`](#markdownvalue-config)
62+
* [`mdx(value, config)`](#mdxvalue-config)
63+
* [`html(value, config)`](#htmlvalue-config)
64+
* [`text(value, config)`](#textvalue-config)
6665
* [Workflow](#workflow)
6766
* [FAQ](#faq)
6867
* [This is stupid!](#this-is-stupid)
@@ -319,18 +318,19 @@ See `$ alex --help` for more information.
319318
320319
## API
321320

321+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
322+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
323+
322324
[npm][]:
323325

324326
```sh
325327
$ npm install alex --save
326328
```
327329

328-
**alex** is also available as an AMD, CommonJS, and globals module,
329-
[uncompressed and compressed][releases].
330-
331-
### `alex(value, config)`
330+
This package exports the identifiers `markdown`, `mdx`, `html`, and `text`.
331+
The default export is `markdown`.
332332

333-
### `alex.markdown(value, config)`
333+
### `markdown(value, config)`
334334

335335
Check Markdown (ignoring syntax).
336336

@@ -348,6 +348,8 @@ shown in the example below, because it holds the possible violations.
348348
###### Example
349349

350350
```js
351+
import alex from 'alex'
352+
351353
alex('We’ve confirmed his identity.').messages
352354
```
353355

@@ -373,7 +375,7 @@ Yields:
373375
]
374376
```
375377

376-
### `alex.mdx(value, config)`
378+
### `mdx(value, config)`
377379

378380
Check [MDX][] (ignoring syntax).
379381

@@ -392,7 +394,9 @@ Check [MDX][] (ignoring syntax).
392394
###### Example
393395

394396
```js
395-
alex.mdx('<Component>He walked to class.</Component>').messages
397+
import {mdx} from 'alex'
398+
399+
mdx('<Component>He walked to class.</Component>').messages
396400
```
397401

398402
Yields:
@@ -413,7 +417,7 @@ Yields:
413417
]
414418
```
415419

416-
### `alex.html(value, config)`
420+
### `html(value, config)`
417421

418422
Check HTML (ignoring syntax).
419423

@@ -429,7 +433,9 @@ Check HTML (ignoring syntax).
429433
###### Example
430434

431435
```js
432-
alex.html('<p class="black">He walked to class.</p>').messages
436+
import {html} from 'alex'
437+
438+
html('<p class="black">He walked to class.</p>').messages
433439
```
434440

435441
Yields:
@@ -452,7 +458,7 @@ Yields:
452458
]
453459
```
454460

455-
### `alex.text(value, config)`
461+
### `text(value, config)`
456462

457463
Check plain text (as in, syntax is checked).
458464

@@ -468,9 +474,11 @@ Check plain text (as in, syntax is checked).
468474
###### Example
469475

470476
```js
471-
alex('The `boogeyman`.').messages // => []
477+
import {markdown, text} from 'alex'
478+
479+
markdown('The `boogeyman`.').messages // => []
472480

473-
alex.text('The `boogeyman`.').messages
481+
text('The `boogeyman`.').messages
474482
```
475483

476484
Yields:
@@ -655,8 +663,6 @@ Lots of [people helped since][contributors]!
655663

656664
[screenshot]: screenshot.png
657665

658-
[releases]: https://github.com/get-alex/alex/releases
659-
660666
[vfile]: https://github.com/vfile/vfile
661667

662668
[profanities]: https://github.com/retextjs/retext-profanities/blob/main/rules.md

‎test/api.js

+86-107
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
'use strict'
1+
import fs from 'fs'
2+
import test from 'tape'
3+
import alex, {markdown, mdx, text, html} from '../index.js'
24

3-
var fs = require('fs')
4-
var path = require('path')
5-
var test = require('tape')
6-
var alex = require('..')
7-
8-
var html = fs.readFileSync(path.join(__dirname, 'fixtures', 'three.html'))
9-
var mdx = fs.readFileSync(path.join(__dirname, 'fixtures', 'four.mdx'))
5+
var threeHtml = fs.readFileSync(
6+
new URL('./fixtures/three.html', import.meta.url)
7+
)
8+
var fourMdx = fs.readFileSync(new URL('./fixtures/four.mdx', import.meta.url))
109

1110
// Tests. Note that these are small because alex is in fact
1211
// just a collection of well-tested modules.
@@ -117,20 +116,18 @@ test('alex()', function (t) {
117116
'should work with deny and profanity config'
118117
)
119118

120-
t.deepEqual(alex.markdown, alex, 'alex.markdown is an alias of alex')
119+
t.deepEqual(markdown, alex, 'markdown is an alias of alex')
121120

122121
t.deepEqual(
123-
alex
124-
.text(
125-
[
126-
'The `boogeyman` wrote all changes to the **master server**. Thus,',
127-
'the slaves were read-only copies of master. But not to worry,',
128-
'he was a _cripple_.',
129-
'',
130-
'Eric is pretty set on beating your butt for the sheriff.'
131-
].join('\n')
132-
)
133-
.messages.map(String),
122+
text(
123+
[
124+
'The `boogeyman` wrote all changes to the **master server**. Thus,',
125+
'the slaves were read-only copies of master. But not to worry,',
126+
'he was a _cripple_.',
127+
'',
128+
'Eric is pretty set on beating your butt for the sheriff.'
129+
].join('\n')
130+
).messages.map(String),
134131
[
135132
'1:6-1:15: `boogeyman` may be insensitive, use `boogeymonster` instead',
136133
'1:44-1:50: `master` may be insensitive, use `primary`, `hub`, `reference` instead',
@@ -140,202 +137,184 @@ test('alex()', function (t) {
140137
'3:11-3:18: `cripple` may be insensitive, use `person with a limp` instead',
141138
'5:36-5:40: Be careful with `butt`, it’s profane in some cases'
142139
],
143-
'alex.text()'
140+
'text()'
144141
)
145142

146143
t.deepEqual(
147-
alex
148-
.text(
149-
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
150-
['butt']
151-
)
152-
.messages.map(String),
144+
text(
145+
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
146+
['butt']
147+
).messages.map(String),
153148
[
154149
'1:5-1:14: `boogeyman` may be insensitive, use `boogeymonster` instead',
155150
'1:31-1:37: Don’t use `asshat`, it’s profane'
156151
],
157-
'alex.text() with allow array'
152+
'text() with allow array'
158153
)
159154

160155
t.deepEqual(
161-
alex
162-
.text(
163-
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
164-
{allow: ['butt']}
165-
)
166-
.messages.map(String),
156+
text(
157+
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
158+
{allow: ['butt']}
159+
).messages.map(String),
167160
[
168161
'1:5-1:14: `boogeyman` may be insensitive, use `boogeymonster` instead',
169162
'1:31-1:37: Don’t use `asshat`, it’s profane'
170163
],
171-
'alex.text() with allow config'
164+
'text() with allow config'
172165
)
173166

174167
t.deepEqual(
175-
alex
176-
.text(
177-
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
178-
{
179-
deny: ['butt']
180-
}
181-
)
182-
.messages.map(String),
168+
text(
169+
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
170+
{
171+
deny: ['butt']
172+
}
173+
).messages.map(String),
183174
['1:52-1:56: Be careful with `butt`, it’s profane in some cases'],
184-
'alex.text() with deny config'
175+
'text() with deny config'
185176
)
186177

187178
t.throws(function () {
188-
alex.text(
179+
text(
189180
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
190181
{
191182
allow: ['asshat'],
192183
deny: ['boogeyman-boogeywoman']
193184
}
194185
)
195-
}, 'alex.text() with allow and deny config')
186+
}, 'text() with allow and deny config')
196187

197188
t.deepEqual(
198-
alex
199-
.text(
200-
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
201-
{profanitySureness: 1}
202-
)
203-
.messages.map(String),
189+
text(
190+
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
191+
{profanitySureness: 1}
192+
).messages.map(String),
204193
[
205194
'1:5-1:14: `boogeyman` may be insensitive, use `boogeymonster` instead',
206195
'1:31-1:37: Don’t use `asshat`, it’s profane'
207196
],
208-
'alex.text() with profanity config'
197+
'text() with profanity config'
209198
)
210199

211200
t.deepEqual(
212-
alex
213-
.text(
214-
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
215-
{
216-
allow: ['asshat'],
217-
profanitySureness: 1
218-
}
219-
)
220-
.messages.map(String),
201+
text(
202+
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
203+
{
204+
allow: ['asshat'],
205+
profanitySureness: 1
206+
}
207+
).messages.map(String),
221208
['1:5-1:14: `boogeyman` may be insensitive, use `boogeymonster` instead'],
222-
'alex.text() with allow and profanity config'
209+
'text() with allow and profanity config'
223210
)
224211

225212
t.deepEqual(
226-
alex
227-
.text(
228-
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
229-
{
230-
deny: ['boogeyman-boogeywoman'],
231-
profanitySureness: 1
232-
}
233-
)
234-
.messages.map(String),
213+
text(
214+
'The boogeyman asked Eric, the asshat, to beat your butt for the sheriff.',
215+
{
216+
deny: ['boogeyman-boogeywoman'],
217+
profanitySureness: 1
218+
}
219+
).messages.map(String),
235220
['1:5-1:14: `boogeyman` may be insensitive, use `boogeymonster` instead'],
236-
'alex.text() with deny and profanity config'
221+
'text() with deny and profanity config'
237222
)
238223

239224
t.deepEqual(
240-
alex.html(html).messages.map(String),
225+
html(threeHtml).messages.map(String),
241226
[
242227
'17:22-17:24: `He` may be insensitive, use `They`, `It` instead',
243228
'18:5-18:8: `She` may be insensitive, use `They`, `It` instead',
244229
'18:36-18:42: Don’t use `asshat`, it’s profane',
245230
'18:74-18:78: Be careful with `butt`, it’s profane in some cases'
246231
],
247-
'alex.html()'
232+
'html()'
248233
)
249234

250235
t.deepEqual(
251-
alex.html(html, ['butt']).messages.map(String),
236+
html(threeHtml, ['butt']).messages.map(String),
252237
[
253238
'17:22-17:24: `He` may be insensitive, use `They`, `It` instead',
254239
'18:5-18:8: `She` may be insensitive, use `They`, `It` instead',
255240
'18:36-18:42: Don’t use `asshat`, it’s profane'
256241
],
257-
'alex.html() with allow array'
242+
'html() with allow array'
258243
)
259244

260245
t.deepEqual(
261-
alex.html(html, {allow: ['butt']}).messages.map(String),
246+
html(threeHtml, {allow: ['butt']}).messages.map(String),
262247
[
263248
'17:22-17:24: `He` may be insensitive, use `They`, `It` instead',
264249
'18:5-18:8: `She` may be insensitive, use `They`, `It` instead',
265250
'18:36-18:42: Don’t use `asshat`, it’s profane'
266251
],
267-
'alex.html() with allow config'
252+
'html() with allow config'
268253
)
269254

270255
t.deepEqual(
271-
alex
272-
.html(html, {
273-
deny: ['butt']
274-
})
275-
.messages.map(String),
256+
html(threeHtml, {
257+
deny: ['butt']
258+
}).messages.map(String),
276259
['18:74-18:78: Be careful with `butt`, it’s profane in some cases'],
277-
'alex.html() with deny config'
260+
'html() with deny config'
278261
)
279262

280263
t.throws(function () {
281-
alex.html(html, {
264+
html(threeHtml, {
282265
allow: ['he-she'],
283266
deny: ['butt']
284267
})
285-
}, 'alex.html() with allow and deny config')
268+
}, 'html() with allow and deny config')
286269

287270
t.deepEqual(
288-
alex.html(html, {profanitySureness: 1}).messages.map(String),
271+
html(threeHtml, {profanitySureness: 1}).messages.map(String),
289272
[
290273
'17:22-17:24: `He` may be insensitive, use `They`, `It` instead',
291274
'18:5-18:8: `She` may be insensitive, use `They`, `It` instead',
292275
'18:36-18:42: Don’t use `asshat`, it’s profane'
293276
],
294-
'alex.html() with profanity config'
277+
'html() with profanity config'
295278
)
296279

297280
t.deepEqual(
298-
alex
299-
.html(html, {
300-
allow: ['he-she'],
301-
profanitySureness: 1
302-
})
303-
.messages.map(String),
281+
html(threeHtml, {
282+
allow: ['he-she'],
283+
profanitySureness: 1
284+
}).messages.map(String),
304285
['18:36-18:42: Don’t use `asshat`, it’s profane'],
305-
'alex.html() with allow and profanity config'
286+
'html() with allow and profanity config'
306287
)
307288

308289
t.deepEqual(
309-
alex
310-
.html(html, {
311-
deny: ['he-she'],
312-
profanitySureness: 1
313-
})
314-
.messages.map(String),
290+
html(threeHtml, {
291+
deny: ['he-she'],
292+
profanitySureness: 1
293+
}).messages.map(String),
315294
[
316295
'17:22-17:24: `He` may be insensitive, use `They`, `It` instead',
317296
'18:5-18:8: `She` may be insensitive, use `They`, `It` instead'
318297
],
319-
'alex.html() with deny and profanity config'
298+
'html() with deny and profanity config'
320299
)
321300

322301
t.deepEqual(
323-
alex.mdx(mdx).messages.map(String),
302+
mdx(fourMdx).messages.map(String),
324303
[
325304
'3:1-3:4: `She` may be insensitive, use `They`, `It` instead',
326305
'3:32-3:38: Don’t use `asshat`, it’s profane',
327306
'3:70-3:74: Be careful with `butt`, it’s profane in some cases'
328307
],
329-
'alex.mdx()'
308+
'mdx()'
330309
)
331310

332311
t.deepEqual(
333-
alex.mdx(mdx, ['butt']).messages.map(String),
312+
mdx(fourMdx, ['butt']).messages.map(String),
334313
[
335314
'3:1-3:4: `She` may be insensitive, use `They`, `It` instead',
336315
'3:32-3:38: Don’t use `asshat`, it’s profane'
337316
],
338-
'alex.mdx() with options'
317+
'mdx() with options'
339318
)
340319

341320
t.end()

‎test/cli.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
'use strict'
1+
import fs from 'fs'
2+
import path from 'path'
3+
import childProcess from 'child_process'
4+
import test from 'tape'
25

3-
var path = require('path')
4-
var childProcess = require('child_process')
5-
var test = require('tape')
6-
7-
var pkg = require('../package.json')
6+
const pkg = JSON.parse(fs.readFileSync('package.json'))
87

98
test('alex-cli', function (t) {
109
t.test('version', function (t) {

‎test/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
31
/* eslint-disable import/no-unassigned-import */
4-
require('./api.js')
5-
require('./cli.js')
2+
import './api.js'
3+
import './cli.js'
64
/* eslint-enable import/no-unassigned-import */

0 commit comments

Comments
 (0)
Please sign in to comment.