Skip to content

Commit

Permalink
Add support for not treating input as markdown
Browse files Browse the repository at this point in the history
By adding a `.markdown()` method (same as `alex()`), and
`.text()`, it is now possible to *not* ignore code, HTML,
and other raw constructs which are ignored by HTML.

alex(1) receives a new `-t, --text` flag to enable this
behavior.

Related to GH-44.
  • Loading branch information
wooorm committed Oct 7, 2015
1 parent 39952c3 commit d6bbe4e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 6 deletions.
25 changes: 24 additions & 1 deletion alex.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Dependencies.
*/

var VFile = require('vfile');
var bail = require('bail');
var mdast = require('mdast');
var bridge = require('mdast-util-to-nlcst');
Expand Down Expand Up @@ -75,13 +76,35 @@ function alex(value) {
return result;
}

/**
* alex, but just for plain-text.
*
* Useful iof you would rather not have things like
* (inline or block-level) code be ignored.
*
* @param {string|VFile} value - Content
* @return {VFile} - Result.
*/
function text(value) {
var file = new VFile(value);

english.run(english.parse(file), file, bail);

sort(file);

return file;
}

/*
* Expose.
*/

alex.text = text;
alex.markdown = alex;

module.exports = alex;

},{"bail":4,"mdast":20,"mdast-util-to-nlcst":19,"retext":57,"retext-english":53,"retext-equality":54,"vfile-sort":67}],2:[function(require,module,exports){
},{"bail":4,"mdast":20,"mdast-util-to-nlcst":19,"retext":57,"retext-english":53,"retext-equality":54,"vfile":68,"vfile-sort":67}],2:[function(require,module,exports){
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer
Expand Down
2 changes: 1 addition & 1 deletion alex.min.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ notifier({

var cli = meow({
'help': [
'Usage: alex [<file> ...] [-w, --why]',
'Usage: alex [<file> ...] [-w, --why] [-t, --text]',
'',
'Options:',
'',
' -w, --why output more info regarding why things might be ' +
'offensive',
' -t, --text treat input as plain-text (not markdown)',
'',
'When no input files are given, searches for markdown and text',
'files in the current directory, `doc`, and `docs`.',
Expand All @@ -70,6 +71,7 @@ var cli = meow({
var exit = 0;
var result = [];
var why = Boolean(cli.flags.w || cli.flags.why);
var fn = Boolean(cli.flags.t || cli.flags.text) ? 'text' : 'markdown'
var input = cli.input.length ? cli.input : [
'{docs/**/,doc/**/,}*.{md,markdown,mkd,mkdn,mkdown,ron,txt,text}'
];
Expand Down Expand Up @@ -126,7 +128,7 @@ globby(input).then(function (filePaths) {
toFile.read(filePath, function (err, file) {
bail(err);

alex(file);
alex[fn](file);

log(file);
});
Expand Down
3 changes: 2 additions & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"wooorm/retext": "^1.0.0",
"wooorm/retext-english": "^1.0.0",
"wooorm/retext-equality": "^1.4.0",
"wooorm/vfile-sort": "^1.0.0"
"wooorm/vfile-sort": "^1.0.0",
"wooorm/vfile": "^1.1.0"
},
"scripts": [
"index.js"
Expand Down
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Dependencies.
*/

var VFile = require('vfile');
var bail = require('bail');
var mdast = require('mdast');
var bridge = require('mdast-util-to-nlcst');
Expand Down Expand Up @@ -74,8 +75,30 @@ function alex(value) {
return result;
}

/**
* alex, but just for plain-text.
*
* Useful iof you would rather not have things like
* (inline or block-level) code be ignored.
*
* @param {string|VFile} value - Content
* @return {VFile} - Result.
*/
function text(value) {
var file = new VFile(value);

english.run(english.parse(file), file, bail);

sort(file);

return file;
}

/*
* Expose.
*/

alex.text = text;
alex.markdown = alex;

module.exports = alex;
26 changes: 26 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ $ npm install alex --global
* [API](#api)

* [alex(value)](#alexvalue)
* [alex.markdown(value)](#alexmarkdownvalue)
* [alex.text(value)](#alextextvalue)

* [Integrations](#integrations)

Expand Down Expand Up @@ -106,6 +108,8 @@ module, [uncompressed](alex.js) and [compressed](alex.min.js).

### alex(value)

### alex.markdown(value)

**Example**

```js
Expand All @@ -132,6 +136,28 @@ alex('We’ve confirmed his identity.').messages;
in its [`messages`](https://github.com/wooorm/vfile#vfilemessages) property, as
demonstrated in the example above, as it holds the possible violations.

### alex.text(value)

Works just like [`alex()`](#alexvalue), but does not parse as markdown
(thus things like code are not ignored)

**Example**

```js
alex('The `boogeyman`.').messages; // []

alex.text('The `boogeyman`.').messages;
/*
* [ { [1:6-1:15: `boogeyman` may be insensitive, use `boogey` instead]
* name: '1:6-1:15',
* file: '',
* reason: '`boogeyman` may be insensitive, use `boogey` instead',
* line: 1,
* column: 6,
* fatal: false } ]
*/
```

## Integrations

* Atom — [wooorm/atom-linter-alex](https://github.com/wooorm/atom-linter-alex)
Expand Down
16 changes: 15 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var alex = require('./');
* warnings are exposed.
*/

test(function (t) {
test('alex()', function (t) {
t.same(alex([
'The boogeyman wrote all changes to the **master server**. Thus,',
'the slaves were read-only copies of master. But not to worry,',
Expand All @@ -38,3 +38,17 @@ test(function (t) {

t.end();
});

test('alex.markdown()', function (t) {
t.same(alex.markdown('The `boogeyman`.').messages.map(String), []);

t.end();
});

test('alex.text()', function (t) {
t.same(alex.text('The `boogeyman`.').messages.map(String), [
'1:6-1:15: `boogeyman` may be insensitive, use `boogey` instead'
]);

t.end();
});

0 comments on commit d6bbe4e

Please sign in to comment.