-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
168 lines (139 loc) · 4.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
var isSelfClosing = require('is-self-closing');
var extend = require('extend-shallow');
var define = require('define-property');
var util = require('snapdragon-util');
var Node = require('snapdragon-node');
/**
* Parse the given `str` and return an AST.
*
* ```js
* var snapdragon = new Snapdgragon([options]);
* var ast = snapdragon.parse('foo/bar');
* console.log(ast);
* ```
* @param {String} `str`
* @param {Object} `options` Set `options.sourcemap` to true to enable source maps.
* @return {Object} Returns an AST.
* @api public
*/
module.exports = function(options) {
return function(snapdragon) {
snapdragon.define('parse', function(str, options) {
return parse(str, extend({}, this.options, options));
});
};
};
function parse(str, options) {
var opts = extend({normalizeWhitespace: false}, options);
var $;
if (typeof str === 'function' && str._root) {
$ = str;
} else if (typeof str !== 'string') {
throw new TypeError('expected a string');
} else {
var cheerio = opts.cheerio || require('cheerio');
$ = cheerio.load(str, opts);
}
if (typeof opts.omitEmpty === 'string' || Array.isArray(opts.omitEmpty)) {
$(util.stringify(opts.omitEmpty)).each(function(i, node) {
var text = $(node).text();
if (!text.trim()) {
$(node).remove();
}
});
}
if (opts.omit) {
$(util.stringify(opts.omit)).remove();
}
// get the nodes to use for the snapdragon AST
var nodes = $._root.children;
// if `options.pick` is used, we essentially
// convert the AST into a token stream, since pick does't
// infer or guarantee any kind of tree-relationship
if (opts.pick) {
var arr = [];
$(util.stringify(opts.pick)).each(function(i, ele) {
arr.push(ele);
});
nodes = arr;
}
var ast = new Node({ type: 'string', nodes: nodes });
var bos = new Node({ type: 'bos', val: ''});
var eos = new Node({ type: 'eos', val: ''});
define(bos, 'parent', ast);
define(eos, 'parent', ast);
nodes.unshift(bos);
nodes.push(eos);
var tokens = [];
// visit over AST, calling "mapVisit" on each "node.nodes" along the way
util.visit(ast, {recurse: true}, function(node, i) {
// pre-process node, before "normalize" is called
if (typeof opts.preprocess === 'function') {
node = opts.preprocess($, node, ast, opts) || node;
}
node = normalize(node);
// post-process node, after "normalize" is called
if (typeof opts.postprocess === 'function') {
node = opts.postprocess(node, ast, opts) || node;
}
// add the index of the token ("node.index" is used for the
// position of the node in relationship to its siblings)
define(node, 'i', tokens.length);
tokens.push(node);
return node;
});
// add non-enumerable tokens array to AST
define(ast, 'tokens', tokens);
// add a non-enumerable reference to cheerio to the AST
define($, '$', ast);
return ast;
}
/**
* Normalize `node` to be a node that is more idiomatic to snapdragon.
* We lose some of the cheerio methods, but we also gain snapdragon
* features.
*
* @param {Object} `node`
* @return {Object}
*/
function normalize(node) {
if (node.type === 'directive') {
node.name = node.name.replace(/^!/, '');
node.type = 'tag';
}
// make some properties non-enumerable
define(node, 'children', node.children);
define(node, 'data', node.data);
// rename to names supported by snapdragon
renameProperty(node, 'children', 'nodes');
renameProperty(node, 'data', 'val');
// make cyclical references non-enumerable
define(node, 'root', node.root);
if (node.type === 'tag') {
node.type = node.name;
delete node.name;
if (!isSelfClosing(node.type)) {
util.wrapNodes(node, Node);
}
}
if (!node.isNode) {
node = new Node(node);
// make cyclical references non-enumerable
define(node, 'parent', node.parent);
define(node, 'firstChild', node.firstChild);
define(node, 'lastChild', node.lastChild);
define(node, 'nodeType', node.nodeType);
}
return node;
}
function renameProperty(node, key, prop) {
if (typeof node[key] !== 'undefined' && !node.hasOwnProperty(prop)) {
node[prop] = node[key];
define(node, key, node[key]);
}
}
/**
* Expose `.parse` method, so it can be used a non-plugin
*/
module.exports.parse = parse;