GitBook Image Captions Plugin

Build Status Dependencies Status DevDependencies Status npm version

Add nice generated captions to your book images. This plugin converts alt or title attributes of your images into the captions. Works on both the GitBook website and your own generated book (pdf, mobi).

rendered page

Online demo

http://tdvorak.gitbooks.io/test-book/content/phetchaburi.html

Installation

In your book.json add the plugin:

  1. {
  2. "plugins": [
  3. "image-captions"
  4. ]
  5. }

If you’re building your book locally, download and prepare plugins by simply running gitbook install.

Configuration

The plugin provides reasonable defaults and configuration is not needed. However, there are several config values you can use to adapt captions to your needs:

Caption text

If you want to configure the caption text, you can provide your own template in the form:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "caption": "Image - _CAPTION_"
  4. }
  5. }

The keyword _CAPTION_ will be automatically replaced by the title or alt of your image (the plugin uses first title, if not found, then alt attribute).

Page level and image number

Keywords _PAGE_LEVEL_, _PAGE_IMAGE_NUMBER_ and _BOOK_IMAGE_NUMBER_ are available.

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "caption": "Image _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_ - _CAPTION_"
  4. }
  5. }

Available variables in the caption text:

  • _PAGE_LEVEL_: for example 1.2. Follows chapters numbering.
  • _PAGE_IMAGE_NUMBER_: sequence number of the image in the chapter. First image in chapter gets value 1.
  • _BOOK_IMAGE_NUMBER_: sequence number of the image in the whole book. First image in book gets value 1.

Text alignment

The image caption is by default aligned to the center. You can override this setting by providing a config property align with one of the values:

  • left
  • right

This will align the caption to the left:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "align": "left"
  4. }
  5. }

Image specific captions

You can set up caption template for a specific image by image level. Level is constructed from page level and image order so that on subpage 1.2 second image level is: 1.2.2. That can be used as an index on configuration:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "images": {
  4. "1.2.2": {
  5. "caption": "This is a special image: _CAPTION_"
  6. }
  7. }
  8. }
  9. }

Additional image attributes

Similarly, you can specify image tag attributes globally or at specific image levels:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "attributes": { "width": "300" },
  4. "images": {
  5. "1.2.2": {
  6. "attributes": {
  7. "width": "400"
  8. }
  9. }
  10. }
  11. }
  12. }

Skip selected images

You can specify which images should be skipped and not enriched by a figure caption:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "images": {
  4. "1.2.2": {
  5. "skip": true
  6. }
  7. }
  8. }
  9. }

Image list

As of version 0.3.0, image list is available from book variables. You need to define a variable name:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "variable_name": "pictures"
  4. }
  5. }

This will automatic add image container to the book variables, so that they are present on any page:

  1. "variables": {
  2. "pictures": []
  3. }

Note: it is not necessary to add pictures entry on variables. This is just to clarify usage of the image list. By defining variable_name, you can make sure not to overwrite any previous book variable.

All images are available on any page. Say you have a pictures.md, you can do:

  1. # Pictures
  2. {% for picture in book.pictures %}
  3. 1. [{{ picture.list_caption }}]({{ picture.backlink }})
  4. {% endfor %}

Image properties available in addition to list_caption and backlink are:

  • backlink: link back to the image page containing anchor
  • list_caption: image caption get from alt or title attribute and processed for list image label
  • index: index of an image on a page aka. page wide image number
  • src: image src attribute
  • key: image key concatenated by page_level.index
  • page_level: page level of the image
  • caption: image caption get from alt or title attribute
  • nro: book wide image number

You can set a different caption (label) for each image on a list. This makes it possible to separate page image caption at the actual page from the label of the image on a picture list:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "variable_name": "pictures",
  4. "list_caption": "List image _BOOK_IMAGE_NUMBER_: _CAPTION_"
  5. }
  6. }

You can set a specific image caption / label as well:

  1. "pluginsConfig": {
  2. "image-captions": {
  3. "variable_name": "pictures",
  4. "images": {
  5. "1.2.2": {
  6. "list_caption": "Special list image _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_: _CAPTION_"
  7. }
  8. }
  9. }
  10. }

CSS Styles

This plugin generates simple figure around your images:

  1. <figure>
  2. <img src="../images/phetchaburi.jpg" alt="Phra Nakhon Khiri, Phetchaburi">
  3. <figcaption>Image - Phra Nakhon Khiri, Phetchaburi</figcaption>
  4. </figure>

You can then customize CSS styles of the figure and figcaption. By default, this definition is included in the plugin:

  1. figure {
  2. margin: 1.5em 0px;
  3. padding:10px 0;
  4. }
  5. figcaption {
  6. clear: left;
  7. margin: 0.75em 0px;
  8. text-align: center;
  9. font-style: italic;
  10. line-height: 1.5em;
  11. }

You can attach your own styles by following the guide on help.gitbook.com.

First, you have to create your own css file - for example website.css. Then add your definitions of figure and caption. You can change the text align, colors, borders and so one. Last step is to attach your css style to the book. Open the book.json config file and modify it to look similar to this:

  1. {
  2. "plugins": [
  3. "image-captions"
  4. ],
  5. "pluginsConfig": {},
  6. "styles": {
  7. "website": "website.css"
  8. }
  9. }

Different styles can be attached for web and books, so you can style the captions differently for every medium:

  1. "styles": {
  2. "website": "website.css",
  3. "ebook": "ebook.css",
  4. "pdf": "pdf.css",
  5. "mobi": "ebook.css",
  6. "epub": "ebook.css"
  7. }

The same should apply for the online book editor on gitbook.com.

Configuration of styles in book.json

Under the hood

This plugin attaches itself to the “page” event of GitBook generate task. It receives rendered HTML page of the chapter. Then the plugin goes through the HTML code of the page, searching for images. If there is any image detected, containing also alt or title atribute, the plugin replaces image occurences with the figure tag, including original image and additional figcaption tag with the text read from image attributes.

Tests

Important part of this plugin is the test suite. You can run the test with command:

  1. npm test

The test suite includes JSHint validation of the plugin and test suite itself. Then the Mocha integration tests are executed, validating expected plugin bahavior. Integration tests use gitbook-tester.

The tests are executed with every pushed commit on the Travis-CI server.

Based on

This plugin is based on the example plugin from GitbookIO/plugin.

Changes

0.4.0

  • Compatibility with Gitbook 3.x (while keeping also compatibility with 2.x releases of Gitbook) #8.

Thanks @piranna and @SamyPesse for support.

0.3.3 & 0.3.4

  • Fixes for #7

Thanks @ghuntley and @aschempp for reports and verification.

0.3.2

  • Readme spelling and grammar fixes (#5)
  • Fixed possible issue with processing pages (promises) (#6)

Thanks @klauern for #5 and @markomanninen for #6

0.3.1

  • Fixed figure inside a link (#4)

Thanks @michaellwest for reporting this issue!

0.3.0

  • added support for book wide and page wide image numbering
  • added support for image specific caption and attribute configuration
  • added support for image list construction by book variables
  • new template keywords: _PAGE_LEVEL_, _PAGE_IMAGE_NUMBER_, _BOOK_IMAGE_NUMBER_in addition to _CAPTION_

Thanks @markomanninen for all new features!

0.2.0

  • Paragraphs and inline image

Thanks @aschempp for PR #1 and new test cases!

0.1.0

  • figcaption text-align configurable

0.0.2 - 0.0.6

  • dependencies fix
  • dependencies cleanup, readme update
  • npmignore configuration
  • integration with coverage tools, readme, code cleanup
  • initial commit