var cheerio = require('cheerio');
var _ = require('lodash');
var slug = require('github-slugid');

// insert anchor link into section
function insertAnchors(content) {
    var $ = cheerio.load(content);
    $(':header').each(function(i, elem) {
        var header = $(elem);
        var id = header.attr("id");
        if (!id) {
            id = slug(header.text());
            header.attr("id", id);
        }
        header.prepend('<a name="' + id + '" class="plugin-anchor" '
                   + 'href="#' + id + '">'
                   + '<i class="fa fa-link" aria-hidden="true"></i>'
                   + '</a>');
    });
    return $.html();
}

module.exports = {
    book: {
        assets: "./assets",
        css: [ "plugin.css" ]
    },
    hooks: {
        "page": function(page) {
            page.sections = _.map(page.sections, function(section) {
                if (section.type != 'normal') return section;

                section.content = insertAnchors(section.content);
                return section;
            });

            return page;
        }
    }
};

var cheerio = require('cheerio');
var _ = require('lodash');
var slug = require('github-slugid');

// insert anchor link into section
function insertAnchors(content) {
    var $ = cheerio.load(content);
    $(':header').each(function(i, elem) {
        var header = $(elem);
        var id = header.attr("id");
        if (!id) {
            id = slug(header.text());
            header.attr("id", id);
        }
        header.prepend('<a name="' + id + '" class="plugin-anchor" '
                   + 'href="#' + id + '">'
                   + '<i class="fa fa-link" aria-hidden="true"></i>'
                   + '</a>');
    });
    return $.html();
}

module.exports = {
    book: {
        assets: "./assets",
        css: [ "plugin.css" ]
    },
    hooks: {
        "page": function(page) {
            page.content = insertAnchors(page.content);
            return page;
        }
    }
};

Use `page.content` instead of `page.sections`
Use `page.content` instead of `page.sections`
