packages/vremark-toc/mdast-util-toc/lib/insert.js
function insert(node, parent, tight) {
    var children = parent.children;
    var length = children.length;
    var last = children[length - 1];
    var isLoose = false;
    var index;
    var item;

    if (node.depth === 1) {
        item = listItem();

        item.children.push({
            type: PARAGRAPH,
            children: [
                {
                    type: LINK,
                    title: null,
                    url: '#' + node.id,
                    children: [
                        {
                            type: TEXT,
                            value: node.value
                        }
                    ]
                }
            ]
        });

        children.push(item);
    } else if (last && last.type === LIST_ITEM) {
        insert(node, last, tight);
    } else if (last && last.type === LIST) {
        node.depth--;

        insert(node, last, tight);
    } else if (parent.type === LIST) {
        item = listItem();

        insert(node, item, tight);

        children.push(item);
    } else {
        item = list();
        node.depth--;

        insert(node, item, tight);

        children.push(item);
    }

    /*
    * Properly style list-items with new lines.
    */

    parent.spread = !tight;

    if (parent.type === LIST && parent.spread) {
        parent.spread = false;
        index = -1;

        while (++index < length) {
            if (children[index].children.length > 1) {
                parent.spread = true;
                break;
            }
        }
    }

    // To do: remove `loose` in next major release.
    if (parent.type === LIST_ITEM) {
        parent.loose = tight ? false : children.length > 1;
    } else {
        if (tight) {
            isLoose = false;
        } else {
            index = -1;

            while (++index < length) {
                if (children[index].loose) {
                    isLoose = true;

                    break;
                }
            }
        }

        index = -1;

        while (++index < length) {
            children[index].loose = isLoose;
        }
    }
}
function insert(node, parent, tight) {
    var children = parent.children;
    var length = children.length;
    var last = children[length - 1];
    var isLoose = false;
    var index;
    var item;

    if (node.depth === 1) {
        item = listItem();

        item.children.push({
            type: PARAGRAPH,
            children: [
                {
                    type: LINK,
                    title: null,
                    url: '#' + node.id,
                    children: [
                        {
                            type: TEXT,
                            value: node.value
                        }
                    ]
                }
            ]
        });

        children.push(item);
    } else if (last && last.type === LIST_ITEM) {
        insert(node, last, tight);
    } else if (last && last.type === LIST) {
        node.depth--;

        insert(node, last, tight);
    } else if (parent.type === LIST) {
        item = listItem();

        insert(node, item, tight);

        children.push(item);
    } else {
        item = list();
        node.depth--;

        insert(node, item, tight);

        children.push(item);
    }

    /*
    * Properly style list-items with new lines.
    */

    parent.spread = !tight;

    if (parent.type === LIST && parent.spread) {
        parent.spread = false;
        index = -1;

        while (++index < length) {
            if (children[index].children.length > 1) {
                parent.spread = true;
                break;
            }
        }
    }

    // To do: remove `loose` in next major release.
    if (parent.type === LIST_ITEM) {
        parent.loose = tight ? false : children.length > 1;
    } else {
        if (tight) {
            isLoose = false;
        } else {
            index = -1;

            while (++index < length) {
                if (children[index].loose) {
                    isLoose = true;

                    break;
                }
            }
        }

        index = -1;

        while (++index < length) {
            children[index].loose = isLoose;
        }
    }
}
function insert ( node , parent , tight ) { var children = parent . children ; var length = children . length ; var last = children [ length - 1 ] ; var isLoose = false ; var index ; var item ; if ( node . depth === 1 ) { item = listItem ( ) ; item . children . push ( { type : PARAGRAPH , children : [ { type : LINK , title : null , url : '#' + node . id , children : [ { type : TEXT , value : node . value } ] } ] } ) ; children . push ( item ) ; } else if ( last && last . type === LIST_ITEM ) { insert ( node , last , tight ) ; } else if ( last && last . type === LIST ) { node . depth -- ; insert ( node , last , tight ) ; } else if ( parent . type === LIST ) { item = listItem ( ) ; insert ( node , item , tight ) ; children . push ( item ) ; } else { item = list ( ) ; node . depth -- ; insert ( node , item , tight ) ; children . push ( item ) ; } /*
    * Properly style list-items with new lines.
    */ parent . spread = ! tight ; if ( parent . type === LIST && parent . spread ) { parent . spread = false ; index = - 1 ; while ( ++ index < length ) { if ( children [ index ] . children . length > 1 ) { parent . spread = true ; break ; } } } // To do: remove `loose` in next major release. if ( parent . type === LIST_ITEM ) { parent . loose = tight ? false : children . length > 1 ; } else { if ( tight ) { isLoose = false ; } else { index = - 1 ; while ( ++ index < length ) { if ( children [ index ] . loose ) { isLoose = true ; break ; } } } index = - 1 ; while ( ++ index < length ) { children [ index ] . loose = isLoose ; } } }
Insert a `node` into a `parent`.

@param {Object} node - `node` to insert.
@param {Object} parent - Parent of `node`.
@param {boolean?} [tight] - Prefer tight list-items.
@return {undefined}
Insert a node into a parent .