Skip to content

Commit 9892ec4

Browse files
committed
Refactor to externalise core API
This refactor externalises the API (process/ use / parse / transform / compile). The two reasons for that: * Now **retext** can use a similar API, without duplicate code; * Less commits relating to API design will be in this repository. This does change the exports object of `lib/parse` and `lib/compile`, in that they previously exposed different interfaces (a function with a constructor on top). This was normalized into just a constructor (`Parser` / `Compiler`) which receive a virtual file and settings, and expose respectivly either a `parse` or a `compile` method. This additionally changes some internals (where plug-ins) are stored, and removes the limitation of not allowing multiple equal attachers.
1 parent ed3e49a commit 9892ec4

File tree

8 files changed

+3498
-1753
lines changed

8 files changed

+3498
-1753
lines changed

component.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
"jonschlinkert/repeat-string": "^1.5.2",
2727
"component/trim": "^0.0.1",
2828
"wooorm/trim-trailing-lines": "^1.0.0",
29-
"segmentio/ware": "^1.2.0",
30-
"wooorm/vfile": "^1.0.0"
29+
"wooorm/unified": "^1.0.0"
3130
},
3231
"repository": "wooorm/mdast",
3332
"scripts": [

index.js

+10-317
Original file line numberDiff line numberDiff line change
@@ -12,324 +12,17 @@
1212
* Dependencies.
1313
*/
1414

15-
var Ware = require('ware');
16-
var VFile = require('vfile');
17-
var extend = require('extend.js');
18-
var parser = require('./lib/parse.js');
19-
var stringifier = require('./lib/stringify.js');
20-
var utilities = require('./lib/utilities.js');
15+
var unified = require('unified');
16+
var Parser = require('./lib/parse.js');
17+
var Compiler = require('./lib/stringify.js');
2118

2219
/*
23-
* Methods.
20+
* Exports.
2421
*/
2522

26-
var Parser = parser.Parser;
27-
var parseProto = Parser.prototype;
28-
var Compiler = stringifier.Compiler;
29-
var compileProto = Compiler.prototype;
30-
31-
/**
32-
* Throws if passed an exception.
33-
*
34-
* Here until the following PR is merged into
35-
* segmentio/ware:
36-
*
37-
* https://github.com/segmentio/ware/pull/21
38-
*
39-
* @param {Error?} exception
40-
*/
41-
function fail(exception) {
42-
if (exception) {
43-
throw exception;
44-
}
45-
}
46-
47-
/**
48-
* Create a custom, cloned, Parser.
49-
*
50-
* @return {Function}
51-
*/
52-
function constructParser() {
53-
var customProto;
54-
var expressions;
55-
var key;
56-
57-
/**
58-
* Extensible prototype.
59-
*/
60-
function CustomProto() {}
61-
62-
CustomProto.prototype = parseProto;
63-
64-
customProto = new CustomProto();
65-
66-
/**
67-
* Extensible constructor.
68-
*/
69-
function CustomParser() {
70-
Parser.apply(this, arguments);
71-
}
72-
73-
CustomParser.prototype = customProto;
74-
75-
/*
76-
* Construct new objects for things that plugin's
77-
* might modify.
78-
*/
79-
80-
customProto.blockTokenizers = extend({}, parseProto.blockTokenizers);
81-
customProto.blockMethods = extend([], parseProto.blockMethods);
82-
customProto.inlineTokenizers = extend({}, parseProto.inlineTokenizers);
83-
customProto.inlineMethods = extend([], parseProto.inlineMethods);
84-
85-
expressions = parseProto.expressions;
86-
customProto.expressions = {};
87-
88-
for (key in expressions) {
89-
customProto.expressions[key] = extend({}, expressions[key]);
90-
}
91-
92-
return CustomParser;
93-
}
94-
95-
/**
96-
* Create a custom, cloned, Compiler.
97-
*
98-
* @return {Function}
99-
*/
100-
function constructCompiler() {
101-
var customProto;
102-
103-
/**
104-
* Extensible prototype.
105-
*/
106-
function CustomProto() {}
107-
108-
CustomProto.prototype = compileProto;
109-
110-
customProto = new CustomProto();
111-
112-
/**
113-
* Extensible constructor.
114-
*/
115-
function CustomCompiler() {
116-
Compiler.apply(this, arguments);
117-
}
118-
119-
CustomCompiler.prototype = customProto;
120-
121-
return CustomCompiler;
122-
}
123-
124-
/**
125-
* Construct an MDAST instance.
126-
*
127-
* @constructor {MDAST}
128-
*/
129-
function MDAST() {
130-
var self = this;
131-
132-
if (!(self instanceof MDAST)) {
133-
return new MDAST();
134-
}
135-
136-
self.ware = new Ware();
137-
self.attachers = [];
138-
139-
self.Parser = constructParser();
140-
self.Compiler = constructCompiler();
141-
}
142-
143-
/**
144-
* Attach a plugin.
145-
*
146-
* @param {Function|Array.<Function>} attach
147-
* @param {Object?} [options]
148-
* @param {FileSet?} [fileSet] - Optional file-set,
149-
* passed by the CLI.
150-
* @return {MDAST}
151-
*/
152-
function use(attach, options, fileSet) {
153-
var self = this;
154-
var index;
155-
var transformer;
156-
157-
if (!(self instanceof MDAST)) {
158-
self = new MDAST();
159-
}
160-
161-
/*
162-
* Multiple attachers.
163-
*/
164-
165-
if ('length' in attach && typeof attach !== 'function') {
166-
index = -1;
167-
168-
while (attach[++index]) {
169-
self.use(attach[index], options, fileSet);
170-
}
171-
172-
return self;
173-
}
174-
175-
/*
176-
* Single plugin.
177-
*/
178-
179-
if (self.attachers.indexOf(attach) === -1) {
180-
transformer = attach(self, options, fileSet);
181-
182-
self.attachers.push(attach);
183-
184-
if (transformer) {
185-
self.ware.use(transformer);
186-
}
187-
}
188-
189-
return self;
190-
}
191-
192-
/**
193-
* Apply transformers to `node`.
194-
*
195-
* @param {Node} ast
196-
* @param {VFile?} [file]
197-
* @param {Function?} [done]
198-
* @return {Node} - `ast`.
199-
*/
200-
function run(ast, file, done) {
201-
var self = this;
202-
203-
if (typeof file === 'function') {
204-
done = file;
205-
file = null;
206-
}
207-
208-
file = new VFile(file);
209-
210-
done = typeof done === 'function' ? done : fail;
211-
212-
if (typeof ast !== 'object' && typeof ast.type !== 'string') {
213-
utilities.raise(ast, 'ast');
214-
}
215-
216-
/*
217-
* Only run when this is an instance of MDAST.
218-
*/
219-
220-
if (self.ware) {
221-
self.ware.run(ast, file, done);
222-
} else {
223-
done(null, ast, file);
224-
}
225-
226-
return ast;
227-
}
228-
229-
/**
230-
* Wrapper to pass a file to `parser`.
231-
*/
232-
function parse(value, options) {
233-
var file = new VFile(value);
234-
var ast = file.namespace('mdast').ast = parser.call(this, file, options);
235-
236-
return ast;
237-
}
238-
239-
/**
240-
* Wrapper to pass a file to `stringifier`.
241-
*/
242-
function stringify(ast, file, options) {
243-
if (options === null || options === undefined) {
244-
options = file;
245-
file = null;
246-
}
247-
248-
if (!file && ast && !ast.type) {
249-
file = ast;
250-
ast = null;
251-
}
252-
253-
file = new VFile(file);
254-
255-
if (!ast) {
256-
ast = file.namespace('mdast').ast || ast;
257-
}
258-
259-
return stringifier.call(this, ast, file, options);
260-
}
261-
262-
/**
263-
* Parse a value and apply transformers.
264-
*
265-
* @param {string|VFile} value
266-
* @param {Object?} [options]
267-
* @param {Function?} [done]
268-
* @return {string?}
269-
*/
270-
function process(value, options, done) {
271-
var file = new VFile(value);
272-
var self = this instanceof MDAST ? this : new MDAST();
273-
var result = null;
274-
var ast;
275-
276-
if (typeof options === 'function') {
277-
done = options;
278-
options = null;
279-
}
280-
281-
if (!options) {
282-
options = {};
283-
}
284-
285-
/**
286-
* Invoked when `run` completes. Hoists `result` into
287-
* the upper scope to return something for sync
288-
* operations.
289-
*/
290-
function callback(exception) {
291-
if (exception) {
292-
(done || fail)(exception);
293-
} else {
294-
result = self.stringify(ast, file, options);
295-
296-
if (done) {
297-
done(null, result, file);
298-
}
299-
}
300-
}
301-
302-
ast = self.parse(file, options);
303-
304-
self.run(ast, file, callback);
305-
306-
return result;
307-
}
308-
309-
/*
310-
* Methods.
311-
*/
312-
313-
var proto = MDAST.prototype;
314-
315-
proto.use = use;
316-
proto.parse = parse;
317-
proto.run = run;
318-
proto.stringify = stringify;
319-
proto.process = process;
320-
321-
/*
322-
* Functions.
323-
*/
324-
325-
MDAST.use = use;
326-
MDAST.parse = parse;
327-
MDAST.run = run;
328-
MDAST.stringify = stringify;
329-
MDAST.process = process;
330-
331-
/*
332-
* Expose `mdast`.
333-
*/
334-
335-
module.exports = MDAST;
23+
module.exports = unified({
24+
'name': 'mdast',
25+
'type': 'ast',
26+
'Parser': Parser,
27+
'Compiler': Compiler
28+
});

0 commit comments

Comments
 (0)