Code coverage report for gitbook-plugin-image-captions/index.js

Statements: 100% (23 / 23)      Branches: 100% (10 / 10)      Functions: 100% (5 / 5)      Lines: 100% (23 / 23)      Ignored: none     

All files » gitbook-plugin-image-captions/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61      1   1 8 8 8 7 7 5 5 5   7 7 7 1 6 4       8 1     8     1                                         9 10     9        
/*jslint node: true */
"use strict";
 
var cheerio = require('cheerio');
 
var insertCaptions = function(section) {
  var options = this.options.pluginsConfig['image-captions'] || {};
  var $ = cheerio.load(section.content);
  $('img').each(function(i, elem) {
    var img = $(elem);
    var wrapImage = function(caption) {
      var template = options.caption || 'Figure: _CAPTION_';
      var result = template.replace('_CAPTION_', caption);
      img.replaceWith('<figure>' + $.html(img) + '<figcaption>'+result+'</figcaption></figure>');
    };
    var title = img.attr('title');
    var alt = img.attr('alt');
    if (title) {
      wrapImage(title);
    } else if (alt) {
      wrapImage(alt);
    }
  });
 
  if(options.align) {
    $('figcaption').addClass(options.align);
  }
 
  section.content = $.html();
};
 
module.exports = {
    book: { // compatibility with the gitbook version 1.x
        assets: './assets',
        css: [
            'image-captions.css'
        ]
    },
    website: {
        assets: './assets',
        css: [
            'image-captions.css'
        ],
    },
    ebook: {
      assets: './assets',
      css: [
         'image-captions.css'
      ]
    },
    hooks: {
      'page': function(page) {  // after page has been converted to html
        page.sections.filter(function(section) {
          return section.type == 'normal';
        })
        .forEach(insertCaptions, this);
        return page;
      }
    }
};