it('Should skip selected images', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { 'images': { '1.1.1': { 'skip': true }, '1.1.2': { 'skip': false } } } } }; return tester.builder() .withContent('![bar](foo.jpg)\n\n![lorem](ipsum.jpg)') .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results.get('index.html').content, '<p><img src="foo.jpg" alt="bar"></p>\n<figure id="fig1.1.2"><img src="ipsum.jpg" alt="lorem"><figcaption>Figure: lorem</figcaption></figure>'); }); });
it('should keep order of images and their global indexes', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { variable_name: 'pictures', caption: 'Image _BOOK_IMAGE_NUMBER_. - _CAPTION_' } } }; return tester.builder() .withContent('![first](first.jpg)') .withPage('second', '![second](second.jpg)') .withPage('second_a', '![second a](second_a.jpg)', 1) // subpage, under second .withPage('third', '![third](third.jpg)\n\n![fourth](fourth.jpg)') .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results.get('index.html').content, '<figure id="fig1.1.1"><img src="first.jpg" alt="first"><figcaption>Image 1. - first</figcaption></figure>'); assert.equal(results.get('second.html').content, '<figure id="fig1.2.1"><img src="second.jpg" alt="second"><figcaption>Image 2. - second</figcaption></figure>'); // bug in Gitbook 2.0 in numbering of chapters. Second chapter, first subchapter gets level 1.2 instead of 1.1 as in all other versions assert.equal(results.get('second_a.html').$('figure figcaption').text(), 'Image 3. - second a'); assert.equal(results.get('third.html').content, '<figure id="fig1.3.1"><img src="third.jpg" alt="third"><figcaption>Image 4. - third</figcaption></figure>' + '\n' + '<figure id="fig1.3.2"><img src="fourth.jpg" alt="fourth"><figcaption>Image 5. - fourth</figcaption></figure>'); }); });
it('should pass default and specific image attributes', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { 'attributes': { 'width': '300' }, 'images': { '1.1.2': { 'attributes': { 'width': '400' } } } } } }; var pageContent = readFile('image_attributes_provided.md'); var expected = readFile('image_attributes_expected.html'); return tester.builder() .withContent(pageContent) .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results[0].content, expected); }); });
it('should align caption to the left', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': {'align': 'left'} } }; return tester.builder() .withContent('![bar](foo.jpg)') .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results[0].content, '<figure id="fig0.1"><img src="foo.jpg" alt="bar"><figcaption class="left">Figure: bar</figcaption></figure>'); }); });
it('should read caption format from option', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': {'caption': 'Image - _CAPTION_'} } }; return tester.builder() .withContent('![bar](foo.jpg)') .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results[0].content, '<figure id="fig0.1"><img src="foo.jpg" alt="bar"><figcaption>Image - bar</figcaption></figure>'); }); });
it('should handle page numbers', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { caption: 'Image _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_ - _CAPTION_' } } }; return tester.builder() .withContent('![bar](foo.jpg)') .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results[0].content, '<figure id="fig0.1"><img src="foo.jpg" alt="bar"><figcaption>Image 0.1 - bar</figcaption></figure>'); }); });
it('should slice lines', function() { return tester.builder() .withContent('#test me\n\n{% codesnippet "./myfile.js", lines="2:4" %}{% endcodesnippet %}') .withLocalPlugin(path.join(__dirname, '..')) .withBookJson({ gitbook: pkg.engines.gitbook, "plugins": ["codesnippet"] }) .withFile('myfile.js', 'test\ntest 2\ntest 3\ntest 4') .create() .then(function(result) { var index = result.get('index.html'); var $ = index.$; var codeBlock = $('code[class="lang-js"]'); assert.equal(codeBlock.length, 1); assert.equal(codeBlock.text(), 'test 2\ntest 3'); }); });
it('should accept inner content', function() { return tester.builder() .withContent('#test me\n\n{% codesnippet %}this is a {{ book.var|d("variable") }}{% endcodesnippet %}') .withLocalPlugin(path.join(__dirname, '..')) .withBookJson({ gitbook: pkg.engines.gitbook, "plugins": ["codesnippet"] }) .withFile('myfile.js', 'test') .create() .then(function(result) { var index = result.get('index.html'); var $ = index.$; var codeBlock = $('code'); assert.equal(codeBlock.length, 1); assert.equal(codeBlock.text(), 'this is a variable'); }); });
it('should include an url', function() { return tester.builder() .withContent('#test me\n\n{% codesnippet "https://gist.githubusercontent.com/magnetikonline/5274656/raw/6c7281322dc82145f4648891e66fdda02503c881/README.md" %}{% endcodesnippet %}') .withLocalPlugin(path.join(__dirname, '..')) .withBookJson({ gitbook: pkg.engines.gitbook, "plugins": ["codesnippet"] }) .withFile('myfile.js', 'test') .create() .then(function(result) { var index = result.get('index.html'); var $ = index.$; var codeBlock = $('code[class="lang-md"]'); assert.equal(codeBlock.length, 1); assert(codeBlock.text().length > 0); }); });
it('Should use custom caption and align together', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { 'caption': 'Image Description - _CAPTION_', 'align': 'left' } } }; return tester.builder() .withContent('![bar](foo.jpg)') .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { var figcaption = results.get('index.html').$('figure figcaption'); assert.equal(figcaption.text(), 'Image Description - bar'); assert.equal(figcaption.attr('class'), 'left'); }); });
it('should render registry of figures', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { variable_name: 'pictures' } } }; var pageContent = readFile('image_registry_provided.md'); var expected = readFile('image_registry_expected.html'); return tester.builder() .withContent(pageContent) .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results.get('index.html').content, expected); }); });
it('should use image specific caption', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { 'images': { '0.1': { 'caption': 'Special image _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_: _CAPTION_' } } } } }; return tester.builder() .withContent('![bar](foo.jpg)') .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results[0].content, '<figure id="fig0.1"><img src="foo.jpg" alt="bar"><figcaption>Special image 0.1: bar</figcaption></figure>'); }); });
it('should render image global index', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { variable_name: 'pictures', caption: 'Image _BOOK_IMAGE_NUMBER_. - _CAPTION_' } } }; var pageContent = readFile('image_bookwide_caption_provided.md'); var expected = readFile('image_bookwide_caption_expected.html'); return tester.builder() .withContent(pageContent) .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results[0].content, expected); }); });
it('should use different caption for figure and for list', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { 'variable_name': 'pictures', 'list_caption': 'List image _BOOK_IMAGE_NUMBER_: _CAPTION_' } } }; var pageContent = readFile('image_registry_provided.md'); var expected = readFile('image_list_captions_expected.html'); return tester.builder() .withContent(pageContent) .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { assert.equal(results.get('index.html').content, expected); }); });
it('should render registry of figures', function () { var config = { plugins: ['image-captions'], pluginsConfig: { 'image-captions': { variable_name: 'pictures' } } }; var pageContent = readFile('image_registry_provided.md'); var expected = readFile('image_registry_expected.html'); return tester.builder() .withContent(pageContent) .withBookJson(config) .withLocalPlugin(thisModulePath) .create() .then(function (results) { // compatibility between gitbook 2 and 3, version 3 relativizes paths var content = results.get('index.html').content.replace(new RegExp('\\.\\/', 'g'), 'index.html'); assert.equal(content, expected); }); });
function basicBuild (content) { return tester.builder() .withContent(content) .withLocalPlugin(thisModulePath) .create(); }