Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 20, 2021
1 parent a799349 commit d2cd488
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 195 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -2,3 +2,4 @@ node_modules/
coverage/
*.html
*.md
*.mdx
40 changes: 21 additions & 19 deletions cli.js
@@ -1,24 +1,26 @@
#!/usr/bin/env node
'use strict'
import fs from 'fs'
import notifier from 'update-notifier'
import supportsColor from 'supports-color'
import meow from 'meow'
import engine from 'unified-engine'
import unified from 'unified'
import markdown from 'remark-parse'
import html from 'rehype-parse'
import frontmatter from 'remark-frontmatter'
import mdx from 'remark-mdx'
import english from 'retext-english'
import remark2retext from 'remark-retext'
import rehype2retext from 'rehype-retext'
import defaultReporter from 'vfile-reporter'
import equality from 'retext-equality'
import profanities from 'retext-profanities'
import diff from 'unified-diff'
import {filter} from './filter.js'

var notifier = require('update-notifier')
var supportsColor = require('supports-color')
var meow = require('meow')
var engine = require('unified-engine')
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')
var defaultReporter = require('vfile-reporter')
var equality = require('retext-equality')
var profanities = require('retext-profanities')
var diff = require('unified-diff')
var pack = require('./package.json')
var filter = require('./filter.js')
const pack = JSON.parse(
fs.readFileSync(new URL('./package.json', import.meta.url))
)

var textExtensions = [
'txt',
Expand Down
10 changes: 3 additions & 7 deletions filter.js
@@ -1,10 +1,6 @@
'use strict'
import remarkMessageControl from 'remark-message-control'

var control = require('remark-message-control')

module.exports = filter

function filter(options) {
export function filter(options) {
/* c8 ignore next */
var settings = options || {}

Expand All @@ -14,7 +10,7 @@ function filter(options) {
)
}

return control({
return remarkMessageControl({
name: 'alex',
reset: Boolean(settings.deny),
enable: settings.deny,
Expand Down
63 changes: 30 additions & 33 deletions index.js
@@ -1,32 +1,24 @@
'use strict'

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')
var profanities = require('retext-profanities')
var remark2retext = require('remark-retext')
var rehype2retext = require('rehype-retext')
var sort = require('vfile-sort')
var filter = require('./filter.js')

module.exports = alex
alex.text = noMarkdown
alex.markdown = alex
alex.mdx = mdxParse
alex.html = htmlParse
import VFile from 'vfile'
import unified from 'unified'
import remarkParse from 'remark-parse'
import remarkFrontmatter from 'remark-frontmatter'
import remarkMdx from 'remark-mdx'
import rehypeParse from 'rehype-parse'
import retextEnglish from 'retext-english'
import retextEquality from 'retext-equality'
import retextProfanities from 'retext-profanities'
import remarkRetext from 'remark-retext'
import rehypeRetext from 'rehype-retext'
import sort from 'vfile-sort'
import {filter} from './filter.js'

function makeText(config) {
return unified()
.use(english)
.use(equality, {
.use(retextEnglish)
.use(retextEquality, {
noBinary: config && config.noBinary
})
.use(profanities, {
.use(retextProfanities, {
sureness: config && config.profanitySureness
})
}
Expand All @@ -53,37 +45,42 @@ function core(value, config, processor) {
return file
}

export default markdown

// Alex.
function alex(value, config) {
export function markdown(value, config) {
return core(
value,
config,
unified()
.use(markdown)
.use(frontmatter, ['yaml', 'toml'])
.use(remark2retext, makeText(config))
.use(remarkParse)
.use(remarkFrontmatter, ['yaml', 'toml'])
.use(remarkRetext, makeText(config))
)
}

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

// Alex, for HTML.
function htmlParse(value, config) {
export function html(value, config) {
return core(
value,
config,
unified().use(html).use(rehype2retext, makeText(config))
unified().use(rehypeParse).use(rehypeRetext, makeText(config))
)
}

// Alex, without the markdown.
function noMarkdown(value, config) {
export function text(value, config) {
return core(value, config, makeText(config))
}
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -55,6 +55,8 @@
"Christian Oliff <christianoliff@yahoo.com>",
"Daan <daan@devign.it>"
],
"type": "module",
"sideEffects": false,
"bin": "cli.js",
"files": [
"index.js",
Expand Down
44 changes: 25 additions & 19 deletions readme.md
Expand Up @@ -58,11 +58,10 @@ Or you can follow this step-by-step tutorial:
* [Configuration](#configuration)
* [CLI](#cli)
* [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)
* [`markdown(value, config)`](#markdownvalue-config)
* [`mdx(value, config)`](#mdxvalue-config)
* [`html(value, config)`](#htmlvalue-config)
* [`text(value, config)`](#textvalue-config)
* [Workflow](#workflow)
* [FAQ](#faq)
* [This is stupid!](#this-is-stupid)
Expand Down Expand Up @@ -319,18 +318,19 @@ See `$ alex --help` for more information.
## API

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

[npm][]:

```sh
$ npm install alex --save
```

**alex** is also available as an AMD, CommonJS, and globals module,
[uncompressed and compressed][releases].

### `alex(value, config)`
This package exports the identifiers `markdown`, `mdx`, `html`, and `text`.
The default export is `markdown`.

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

Check Markdown (ignoring syntax).

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

```js
import alex from 'alex'

alex('We’ve confirmed his identity.').messages
```

Expand All @@ -373,7 +375,7 @@ Yields:
]
```

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

Check [MDX][] (ignoring syntax).

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

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

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

Yields:
Expand All @@ -413,7 +417,7 @@ Yields:
]
```

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

Check HTML (ignoring syntax).

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

```js
alex.html('<p class="black">He walked to class.</p>').messages
import {html} from 'alex'

html('<p class="black">He walked to class.</p>').messages
```

Yields:
Expand All @@ -452,7 +458,7 @@ Yields:
]
```

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

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

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

```js
alex('The `boogeyman`.').messages // => []
import {markdown, text} from 'alex'

markdown('The `boogeyman`.').messages // => []

alex.text('The `boogeyman`.').messages
text('The `boogeyman`.').messages
```

Yields:
Expand Down Expand Up @@ -655,8 +663,6 @@ Lots of [people helped since][contributors]!

[screenshot]: screenshot.png

[releases]: https://github.com/get-alex/alex/releases

[vfile]: https://github.com/vfile/vfile

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

0 comments on commit d2cd488

Please sign in to comment.