blob: 22be1afe1ea133ddfeecf172d3d0f26909ec5e8a (
plain)
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
|
var _ = require("underscore");
exports.paragraph = paragraph;
exports.getDescendantsOfType = getDescendantsOfType;
exports.getDescendants = getDescendants;
function paragraph(transform) {
function transformElement(element) {
if (element.children) {
var children = _.map(element.children, transformElement);
element = _.extend(element, {children: children});
}
if (element.type === "paragraph") {
element = transform(element);
}
return element;
}
return transformElement;
}
function getDescendantsOfType(element, type) {
return getDescendants(element).filter(function(descendant) {
return descendant.type === type;
});
}
function getDescendants(element) {
var descendants = [];
visitDescendants(element, function(descendant) {
descendants.push(descendant);
});
return descendants;
}
function visitDescendants(element, visit) {
if (element.children) {
element.children.forEach(function(child) {
visitDescendants(child, visit);
visit(child);
});
}
}
|