cleanview
Version:
Clean the content of html articles
42 lines (41 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addIds = addIds;
const helpers_1 = require("./helpers");
/***
This needs to be a function within a function because the count
needs to reset to 1 each time `addIds` is called
***/
function addIds(elements) {
const allElements = {};
let count = 1;
function newId() {
return count++;
}
navigate(elements, function (el, parent) {
if (!el)
return;
const id = newId();
el.id = id;
el.parentId = parent.id;
allElements[id] = el;
});
return allElements;
}
function navigate(element, func, parent) {
parent = parent !== null && parent !== void 0 ? parent : {};
// if it's an array
if (Array.isArray(element)) {
element.forEach(function (el) {
navigate(el, func, parent);
});
return;
}
if (!(0, helpers_1.isNode)(element))
return;
// if it's an element
func(element, parent);
element.children.forEach(function (el) {
navigate(el, func, element);
});
}