Skip to content

Commit 0dd7eb9

Browse files
committed
Refactor module
* Remove support for optional parsers (they are now required); * Remove support for storing `nlcst` on the file; * Remove support for `cst` and `ast` properties, replaced with `tree`: unifiedjs/unified@8748cfe
1 parent c2834a1 commit 0dd7eb9

File tree

7 files changed

+38
-2791
lines changed

7 files changed

+38
-2791
lines changed

component.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
"language"
1212
],
1313
"dependencies": {
14-
"wooorm/mdast-range": "^0.4.3",
15-
"wooorm/parse-latin": "^0.5.1",
16-
"wooorm/nlcst-to-string": "^0.1.5"
14+
"wooorm/mdast-range": "^1.0.0",
15+
"wooorm/nlcst-to-string": "^1.0.0"
1716
},
1817
"repository": "wooorm/mdast-util-to-nlcst",
1918
"scripts": [

index.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
var range = require('mdast-range');
19-
var Latin = require('parse-latin');
2019
var toString = require('nlcst-to-string');
2120

2221
/*
@@ -216,14 +215,14 @@ one = function (node, index, parent, file, parser) {
216215
* Transform `ast` into `nlcst`.
217216
*
218217
* @param {File} file - Virtual file.
219-
* @param {Function?} [Parser] - Constructor of an nlcst
220-
* parser. Defaults to `ParseLatin`.
218+
* @param {Parser|Function} Parser - (Instance of) NLCST
219+
* parser.
221220
* @return {NLCSTNode} - NLCST.
222221
*/
223222
function toNLCST(file, Parser) {
224223
var ast;
224+
var space;
225225
var parser;
226-
var cst;
227226

228227
/*
229228
* Warn for invalid parameters.
@@ -233,7 +232,8 @@ function toNLCST(file, Parser) {
233232
throw new Error('mdast-util-to-nlcst expected file');
234233
}
235234

236-
ast = file.namespace('mdast').ast;
235+
space = file.namespace('mdast');
236+
ast = space.tree || space.ast;
237237

238238
if (!ast || !ast.type) {
239239
throw new Error('mdast-util-to-nlcst expected node');
@@ -253,17 +253,10 @@ function toNLCST(file, Parser) {
253253
*/
254254

255255
if (!Parser) {
256-
Parser = Latin;
256+
throw new Error('mdast-util-to-nlcst expected parser');
257257
}
258258

259-
if ('parse' in Parser) {
260-
parser = Parser;
261-
parser.position = true;
262-
} else {
263-
parser = new Parser({
264-
'position': true
265-
});
266-
}
259+
parser = 'parse' in Parser ? Parser : new Parser();
267260

268261
/*
269262
* Patch ranges.
@@ -277,11 +270,7 @@ function toNLCST(file, Parser) {
277270
* where needed.
278271
*/
279272

280-
cst = parser.parse(one(ast, null, null, file, parser));
281-
282-
file.namespace('retext').cst = cst;
283-
284-
return cst;
273+
return parser.parse(one(ast, null, null, file, parser));
285274
}
286275

287276
/*

mdast-util-to-nlcst.js

Lines changed: 9 additions & 2747 deletions
Large diffs are not rendered by default.

mdast-util-to-nlcst.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
],
1313
"dependencies": {
1414
"mdast-range": "^1.0.0",
15-
"parse-latin": "^2.0.0",
1615
"nlcst-to-string": "^1.0.0"
1716
},
1817
"repository": {
@@ -43,6 +42,7 @@
4342
"mocha": "^2.0.0",
4443
"parse-dutch": "^2.0.0",
4544
"parse-english": "^2.0.0",
45+
"parse-latin": "^2.0.0",
4646
"vfile": "^1.0.0"
4747
},
4848
"scripts": {

readme.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,23 @@ mdast.process('Some *foo*s-_ball_.', function (err, doc, file) {
195195

196196
## API
197197

198-
### toNLCST(file\[, Parser | parser\])
198+
### toNLCST(file, Parser | parser)
199199

200200
Transform a by [**mdast**](https://github.com/wooorm/mdast) processed
201-
[**virtual file**](https://github.com/wooorm/vfile) into an file ready
202-
for processing by [**retext**](https://github.com/wooorm/retext).
201+
[**virtual file**](https://github.com/wooorm/vfile) into a
202+
[NLCST](https://github.com/wooorm/nlcst) tree for
203+
[**retext**](https://github.com/wooorm/retext).
203204

204205
Parameters:
205206

206207
* `file` (`File`)
207208
[Virtual file](https://github.com/wooorm/vfile), must be passed through
208209
[`parse()`](https://github.com/wooorm/mdast/blob/master/doc/mdast.3.md#mdastparsefile-options).
209210

210-
* `parser` (`Function` or `Parser`, default: `new ParseLatin()`, optional)
211+
* `parser` (`Function` or `Parser`, optional)
211212
— You can pass the (constructor of) an NLCST parser, such as
212-
[**parse-english**](https://github.com/wooorm/parse-english) or
213-
[**parse-dutch**](https://github.com/wooorm/parse-dutch), the default is
213+
[**parse-english**](https://github.com/wooorm/parse-english),
214+
[**parse-dutch**](https://github.com/wooorm/parse-dutch), or
214215
[**parse-latin**](https://github.com/wooorm/parse-latin).
215216

216217
Returns:

test/index.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var fs = require('fs');
1111
var path = require('path');
1212
var mdast = require('mdast');
1313
var VFile = require('vfile');
14+
var Latin = require('parse-latin');
1415
var Dutch = require('parse-dutch');
1516
var English = require('parse-english');
1617
var toNLCST = require('..');
@@ -37,9 +38,10 @@ var fixtures = fs.readdirSync(ROOT);
3738
/**
3839
* Helper to create a new file from a given cst.
3940
*/
40-
function toFile(ast) {
41+
function toFile(tree) {
4142
var file = new VFile();
42-
file.namespace('mdast').ast = ast;
43+
44+
file.namespace('mdast').tree = tree;
4345

4446
return file;
4547
}
@@ -86,7 +88,7 @@ describe('mdast-util-to-nlcst', function () {
8688
}, /mdast-util-to-nlcst expected file/);
8789
});
8890

89-
it('should fail when not given a positional information', function () {
91+
it('should fail when not given positional information', function () {
9092
assert.throws(function () {
9193
toNLCST(toFile({
9294
'type': 'text',
@@ -122,13 +124,13 @@ describe('mdast-util-to-nlcst', function () {
122124
}
123125
};
124126

125-
toNLCST(toFile(node));
127+
toNLCST(toFile(node), Latin);
126128

127129
assert.equal(node.position.start.offset, 0);
128130
assert.equal(node.position.end.offset, 3);
129131
});
130132

131-
it('should accept an optional parser', function () {
133+
it('should accept a parser', function () {
132134
var node = {
133135
'type': 'text',
134136
'value': 'foo',
@@ -144,9 +146,9 @@ describe('mdast-util-to-nlcst', function () {
144146
}
145147
};
146148

147-
assert.doesNotThrow(function () {
149+
assert.throws(function () {
148150
toNLCST(toFile(node));
149-
});
151+
}, /mdast-util-to-nlcst expected parser/);
150152

151153
assert.doesNotThrow(function () {
152154
toNLCST(toFile(node), English);
@@ -178,14 +180,8 @@ function describeFixture(fixture) {
178180
var input = read(join(filepath, 'input.md'), 'utf-8');
179181

180182
mdast().process(input, function (err, file) {
181-
var cst;
182-
183+
assert.deepEqual(toNLCST(file, Latin), JSON.parse(output));
183184
done(err);
184-
185-
cst = toNLCST(file);
186-
187-
assert.deepEqual(cst, JSON.parse(output));
188-
assert.equal(cst, file.namespace('retext').cst);
189185
});
190186
});
191187
}

0 commit comments

Comments
 (0)