diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-03-07 15:15:41 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-03-07 15:15:41 +0100 |
commit | e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45 (patch) | |
tree | 1b80210058a58acf11a57100798665a2207836b6 /docs/plugins/api.md | |
parent | 1d795076657f7f58fe15e647ad2a00dd37501ae0 (diff) | |
download | gitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.zip gitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.tar.gz gitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.tar.bz2 |
Improve docs for plugins
Diffstat (limited to 'docs/plugins/api.md')
-rw-r--r-- | docs/plugins/api.md | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/docs/plugins/api.md b/docs/plugins/api.md new file mode 100644 index 0000000..c5a17b3 --- /dev/null +++ b/docs/plugins/api.md @@ -0,0 +1,89 @@ +# Context and APIs + +GitBooks provides different APIs and contexts to plugins. These APIs can vary according to the GitBook version being used, your plugin should specify the `engines.gitbook` field in `package.json` accordingly. + +#### Book instance + +The `Book` class is the central point of GitBook, it centralize all access read methods. This class is defined in [book.js](https://github.com/GitbookIO/gitbook/blob/master/lib/book.js). + +```js +// Read configuration from book.json +var value = book.config.get('title', 'Default Value'); + +// Resolve a filename to an absolute path +var filepath = book.resolve('README.md'); +``` + +#### Output instance + +The `Output` class represent the output/write process. + +```js +// Return root folder for the output +var root = output.root(); + +// Resolve a file in the output folder +var filepath = output.resolve('myimage.png'); + +// Convert a filename to an URL (returns a path to an html file) +var fileurl = output.toURL('mychapter/README.md'); + +// Write a file in the output folder +output.write('hello.txt', 'Hello World') + .then(function() { ... }); + +// Copy a file to the output folder +output.copyFile('./myfile.jpg', 'cover.jpg') + .then(function() { ... }); + +// Verify that a file exists +output.hasFile('hello.txt') + .then(function(exists) { ... }); +``` + +#### Page instance + +A page instance represent the current parsed page. + +```js +// Title of the page (from SUMMARY) +page.title + +// Content of the page (Markdown/Asciidoc/HTML according to the stage) +page.content + +// Relative path in the book +page.path + +// Absolute path to the file +page.rawPath + +// Type of parser used for this file +page.type ('markdown' or 'asciidoc') +``` + +#### Context for Blocks and Filters + +Blocks and filters have access to the same context, this context is bind to the template engine execution: + +```js +{ + // Current templating syntax + "ctx": { + // For example, after a {% set message = "hello" %} + "message": "hello" + }, + + // Book instance + "book" <Book>, + + // Output instance + "output": <Output> +} +``` + +For example a filter or block function can access the current book using: `this.book`. + +#### Context for Hooks + +Hooks only have access to the `<Book>` instance using `this.book`. |