You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.4 KiB
61 lines
1.4 KiB
var _ = require("underscore");
|
|
|
|
exports.paragraph = paragraph;
|
|
exports.run = run;
|
|
exports._elements = elements;
|
|
exports.getDescendantsOfType = getDescendantsOfType;
|
|
exports.getDescendants = getDescendants;
|
|
|
|
function paragraph(transform) {
|
|
return elementsOfType("paragraph", transform);
|
|
}
|
|
|
|
function run(transform) {
|
|
return elementsOfType("run", transform);
|
|
}
|
|
|
|
function elementsOfType(elementType, transform) {
|
|
return elements(function(element) {
|
|
if (element.type === elementType) {
|
|
return transform(element);
|
|
} else {
|
|
return element;
|
|
}
|
|
});
|
|
}
|
|
|
|
function elements(transform) {
|
|
return function transformElement(element) {
|
|
if (element.children) {
|
|
var children = _.map(element.children, transformElement);
|
|
element = _.extend(element, {children: children});
|
|
}
|
|
return transform(element);
|
|
};
|
|
}
|
|
|
|
|
|
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);
|
|
});
|
|
}
|
|
}
|
|
|