Skip to content

Commit 4103ac5

Browse files
committed
Update example in readme.md
1 parent 0b9c3cb commit 4103ac5

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

readme.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,43 @@ npm install nlcst-is-literal
1616

1717
## Usage
1818

19+
Say we have the following file, `example.txt`:
20+
21+
```text
22+
The word “foo” is meant as a literal.
23+
24+
The word «bar» is meant as a literal.
25+
26+
The word (baz) is meant as a literal.
27+
28+
The word, qux, is meant as a literal.
29+
30+
The word — quux — is meant as a literal.
31+
```
32+
33+
And our script, `example.js`, looks as follows:
34+
1935
```javascript
20-
var retext = require('retext');
36+
var vfile = require('to-vfile');
37+
var unified = require('unified');
38+
var english = require('retext-english');
2139
var visit = require('unist-util-visit');
2240
var toString = require('nlcst-to-string');
2341
var literal = require('nlcst-is-literal');
2442

25-
retext().use(plugin).process([
26-
'The word “foo” is meant as a literal.',
27-
'The word «bar» is meant as a literal.',
28-
'The word (baz) is meant as a literal.',
29-
'The word, qux, is meant as a literal.',
30-
'The word — quux — is meant as a literal.'
31-
].join('\n\n'));
32-
33-
function plugin() {
34-
return transformer;
35-
function transformer(tree) {
36-
visit(tree, 'WordNode', visitor);
37-
}
38-
function visitor(node, index, parent) {
39-
if (literal(parent, index)) {
40-
console.log(toString(node));
41-
}
43+
var file = vfile.readSync('example.txt');
44+
var tree = unified().use(english).parse(file);
45+
46+
visit(tree, 'WordNode', visitor);
47+
48+
function visitor(node, index, parent) {
49+
if (literal(parent, index)) {
50+
console.log(toString(node));
4251
}
4352
}
4453
```
4554

46-
Yields:
55+
Now, running `node example` yields:
4756

4857
```text
4958
foo

0 commit comments

Comments
 (0)