Skip to content

Commit

Permalink
Update example in readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 13, 2017
1 parent 0b9c3cb commit 4103ac5
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,43 @@ npm install nlcst-is-literal

## Usage

Say we have the following file, `example.txt`:

```text
The word “foo” is meant as a literal.
The word «bar» is meant as a literal.
The word (baz) is meant as a literal.
The word, qux, is meant as a literal.
The word — quux — is meant as a literal.
```

And our script, `example.js`, looks as follows:

```javascript
var retext = require('retext');
var vfile = require('to-vfile');
var unified = require('unified');
var english = require('retext-english');
var visit = require('unist-util-visit');
var toString = require('nlcst-to-string');
var literal = require('nlcst-is-literal');

retext().use(plugin).process([
'The word “foo” is meant as a literal.',
'The word «bar» is meant as a literal.',
'The word (baz) is meant as a literal.',
'The word, qux, is meant as a literal.',
'The word — quux — is meant as a literal.'
].join('\n\n'));

function plugin() {
return transformer;
function transformer(tree) {
visit(tree, 'WordNode', visitor);
}
function visitor(node, index, parent) {
if (literal(parent, index)) {
console.log(toString(node));
}
var file = vfile.readSync('example.txt');
var tree = unified().use(english).parse(file);

visit(tree, 'WordNode', visitor);

function visitor(node, index, parent) {
if (literal(parent, index)) {
console.log(toString(node));
}
}
```

Yields:
Now, running `node example` yields:

```text
foo
Expand Down

0 comments on commit 4103ac5

Please sign in to comment.