var _ = require('lodash');

// Split a page up into sections (lesson, exercises, ...)
function split(nodes) {
    var section = [];

    return _.chain(nodes)
    .reduce(function(sections, el) {
        if(el.type === 'hr') {
            sections.push(section);
            section = [];
            section.links = nodes.links || {};
        } else {
            section.push(el);
        }

        return sections;
    }, [])
     // Add remaining nodes
    .concat([section])
    // Exclude empty sections
    .filter(_.negate(_.isEmpty))
    // Spit out JS array
    .value();
}

function join(sections) {
    var nodes = _.reduce(sections, function(nodes, section) {
        return nodes.concat(section);
    }, []);
    nodes.links = (sections[0] && (sections[0].links) || {});
    return nodes;
}

module.exports = {
    split: split,
    join: split,
};

var _ = require('lodash');

// Split a page up into sections (lesson, exercises, ...)
function split(nodes) {
    var section = [];

    var sections = _.chain(nodes)
    .reduce(function(sections, el) {
        if(el.type === 'hr') {
            sections.push(section);
            section = [];
        } else {
            section.push(el);
        }

        return sections;
    }, [])
     // Add remaining nodes
    .concat([section])
    // Exclude empty sections
    .filter(_.negate(_.isEmpty))
    // Spit out JS array
    .value();

    // Copy over links (kramed needs them)
    sections.links = nodes.links || {};

    return sections;
}

function join(sections) {
    var nodes = _.reduce(sections, function(nodes, section) {
        return nodes.concat(section);
    }, []);

    // Restore links (needed by kramed)
    nodes.links = sections.links || {};

    return nodes;
}

module.exports = {
    split: split,
    join: split,
};

Fix carrying kramed links around
Fix carrying kramed links around
