Skip to content

Commit 5815a24

Browse files
committed
Add support for passing in ignores, sources
1 parent 3e533ad commit 5815a24

File tree

9 files changed

+483
-58
lines changed

9 files changed

+483
-58
lines changed

index.js

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ var toString = require('nlcst-to-string');
77

88
module.exports = toNLCST;
99

10-
/* Map of ignored mdast nodes: nodes which have no (simple)
11-
* representation in NLCST. */
12-
var IGNORE = {
13-
horizontalRule: true,
14-
table: true,
15-
tableRow: true,
16-
tableCell: true
17-
};
10+
var ignore = [
11+
'table',
12+
'tableRow',
13+
'tableCell'
14+
];
1815

19-
var C_NEWLINE = '\n';
16+
var source = [
17+
'inlineCode'
18+
];
19+
20+
var newline = '\n';
2021

2122
/* Transform `tree` into `nlcst`. */
22-
function toNLCST(tree, file, Parser) {
23+
function toNLCST(tree, file, Parser, options) {
24+
var settings = options || {};
2325
var parser;
24-
var location;
2526

2627
/* Warn for invalid parameters. */
2728
if (!tree || !tree.type) {
@@ -37,8 +38,6 @@ function toNLCST(tree, file, Parser) {
3738
throw new Error('mdast-util-to-nlcst expected parser');
3839
}
3940

40-
location = vfileLocation(file);
41-
4241
if (
4342
!tree.position ||
4443
!tree.position.start ||
@@ -53,44 +52,51 @@ function toNLCST(tree, file, Parser) {
5352
/* Transform mdast into NLCST tokens, and pass these
5453
* into `parser.parse` to insert sentences, paragraphs
5554
* where needed. */
56-
return parser.parse(one(tree, null, null, file, location, parser));
55+
return parser.parse(one({
56+
doc: String(file),
57+
location: vfileLocation(file),
58+
parser: parser,
59+
ignore: ignore.concat(settings.ignore || []),
60+
source: source.concat(settings.source || [])
61+
}, tree));
5762
}
5863

5964
/* Convert `node` into NLCST. */
60-
function one(node, index, parent, file, location, parser) {
65+
function one(config, node) {
66+
var offset = config.location.toOffset;
67+
var parser = config.parser;
68+
var doc = config.doc;
6169
var type = node.type;
62-
var doc = String(file);
63-
var start = location.toOffset(position.start(node));
64-
var end = location.toOffset(position.end(node));
65-
var replacement;
70+
var start = offset(position.start(node));
71+
var end = offset(position.end(node));
6672

67-
if (type in IGNORE) {
68-
return null;
69-
}
73+
if (config.ignore.indexOf(type) === -1) {
74+
if (config.source.indexOf(type) !== -1) {
75+
return patch(config, [parser.tokenizeSource(doc.slice(start, end))], start);
76+
}
7077

71-
if (node.children) {
72-
replacement = all(node, file, location, parser);
73-
} else if (
74-
type === 'image' ||
75-
type === 'imageReference'
76-
) {
77-
replacement = patch(parser.tokenize(node.alt), location, start + 2);
78-
} else if (
79-
type === 'text' ||
80-
type === 'escape'
81-
) {
82-
replacement = patch(parser.tokenize(node.value), location, start);
83-
} else if (node.type === 'break') {
84-
replacement = patch([parser.tokenizeWhiteSpace('\n')], location, start);
85-
} else if (node.type === 'inlineCode') {
86-
replacement = patch([parser.tokenizeSource(doc.slice(start, end))], location, start);
78+
if (node.children) {
79+
return all(config, node);
80+
}
81+
82+
if (type === 'image' || type === 'imageReference') {
83+
return patch(config, parser.tokenize(node.alt), start + 2);
84+
}
85+
86+
if (type === 'text' || type === 'escape') {
87+
return patch(config, parser.tokenize(node.value), start);
88+
}
89+
90+
if (node.type === 'break') {
91+
return patch(config, [parser.tokenizeWhiteSpace('\n')], start);
92+
}
8793
}
8894

89-
return replacement || null;
95+
return null;
9096
}
9197

9298
/* Convert all nodes in `parent` (mdast) into NLCST. */
93-
function all(parent, file, location, parser) {
99+
function all(config, parent) {
94100
var children = parent.children;
95101
var length = children && children.length;
96102
var index = -1;
@@ -108,20 +114,17 @@ function all(parent, file, location, parser) {
108114
endLine = position.start(node).line;
109115

110116
if (prevEndLine && endLine !== prevEndLine) {
111-
child = parser.tokenizeWhiteSpace(
112-
repeat(C_NEWLINE, endLine - prevEndLine)
113-
);
114-
115-
patch([child], location, prevOffset);
117+
child = config.parser.tokenizeWhiteSpace(repeat(newline, endLine - prevEndLine));
118+
patch(config, [child], prevOffset);
116119

117120
if (child.value.length < 2) {
118-
child.value = repeat(C_NEWLINE, 2);
121+
child.value = repeat(newline, 2);
119122
}
120123

121124
result.push(child);
122125
}
123126

124-
child = one(node, index, parent, file, location, parser);
127+
child = one(config, node);
125128

126129
if (child) {
127130
result = result.concat(child);
@@ -138,7 +141,8 @@ function all(parent, file, location, parser) {
138141
/* Patch a position on each node in `nodes`.
139142
* `offset` is the offset in `file` this run of content
140143
* starts at. */
141-
function patch(nodes, location, offset) {
144+
function patch(config, nodes, offset) {
145+
var position = config.location.toPosition;
142146
var length = nodes.length;
143147
var index = -1;
144148
var start = offset;
@@ -151,14 +155,14 @@ function patch(nodes, location, offset) {
151155
children = node.children;
152156

153157
if (children) {
154-
patch(children, location, start);
158+
patch(config, children, start);
155159
}
156160

157161
end = start + toString(node).length;
158162

159163
node.position = {
160-
start: location.toPosition(start),
161-
end: location.toPosition(end)
164+
start: position(start),
165+
end: position(end)
162166
};
163167

164168
start = end;

readme.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,39 @@ RootNode[1] (1:1-1:17, 0-16)
4646

4747
## API
4848

49-
### `toNLCST(node, file, Parser)`
49+
### `toNLCST(node, file, Parser[, options])`
5050

5151
Transform an [MDAST][] syntax tree and corresponding [virtual file][vfile]
52-
into an [NLCST][nlcst] tree.
52+
into an [NLCST][] tree.
5353

5454
##### Parameters
5555

56-
* `node` ([`MDASTNode`][mdast])
57-
— Syntax tree (with positional information)
58-
* `file` ([`VFile`][vfile])
56+
###### `node`
5957

60-
##### `parser`
58+
Syntax tree, with positional information ([`MDASTNode`][mdast]).
59+
60+
###### `file`
61+
62+
Virtual file ([`VFile`][vfile]).
63+
64+
###### `parser`
6165

6266
Constructor of an NLCST parser (`Function`). For example,
6367
[`parse-english`][english], [`parse-dutch`][dutch], or
6468
[`parse-latin`][latin].
6569

70+
###### `options.ignore`
71+
72+
List of node [types][type] to ignore (`Array.<string>`).
73+
74+
`'table'`, `'tableRow'`, and `'tableCell'` are always ignored.
75+
76+
###### `options.source`
77+
78+
List of node [types][type] to mark as [source][] (`Array.<string>`).
79+
80+
`'inlineCode'` is always ignored.
81+
6682
##### Returns
6783

6884
[`NLCSTNode`][nlcst].
@@ -111,3 +127,7 @@ Constructor of an NLCST parser (`Function`). For example,
111127
[latin]: https://github.com/wooorm/parse-latin
112128

113129
[dutch]: https://github.com/wooorm/parse-dutch
130+
131+
[type]: https://github.com/syntax-tree/mdast#ast
132+
133+
[source]: https://github.com/syntax-tree/nlcst#source

test/fixtures/config-ignore/input.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A paragraph.
2+
3+
> A paragraph in a block quote.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ignore": [
3+
"blockquote"
4+
]
5+
}

0 commit comments

Comments
 (0)