summaryrefslogtreecommitdiffstats
path: root/docs
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-03-07 15:15:41 +0100
committerSamy Pessé <samypesse@gmail.com>2016-03-07 15:15:41 +0100
commite8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45 (patch)
tree1b80210058a58acf11a57100798665a2207836b6 /docs
parent1d795076657f7f58fe15e647ad2a00dd37501ae0 (diff)
downloadgitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.zip
gitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.tar.gz
gitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.tar.bz2
Improve docs for plugins
Diffstat (limited to 'docs')
-rw-r--r--docs/SUMMARY.md4
-rw-r--r--docs/plugins/api.md89
-rw-r--r--docs/plugins/blocks.md62
-rw-r--r--docs/plugins/create.md13
-rw-r--r--docs/plugins/filters.md57
-rw-r--r--docs/plugins/hooks.md90
6 files changed, 314 insertions, 1 deletions
diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md
index 953d436..2d82953 100644
--- a/docs/SUMMARY.md
+++ b/docs/SUMMARY.md
@@ -22,6 +22,10 @@
* [Variables](templating/variables.md)
* [Plugins](plugins/README.md)
* [Create a plugin](plugins/create.md)
+ * [Hooks](plugins/hooks.md)
+ * [Blocks](plugins/blocks.md)
+ * [Filters](plugins/filters.md)
+ * [API & Context](plugins/api.md)
* [Test your plugin](plugins/testing.md)
* [Theming](themes/README.md)
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`.
diff --git a/docs/plugins/blocks.md b/docs/plugins/blocks.md
new file mode 100644
index 0000000..6fc227b
--- /dev/null
+++ b/docs/plugins/blocks.md
@@ -0,0 +1,62 @@
+# Extend Blocks
+
+Extending templating blocks is the best way to provide extra functionalities to authors.
+
+The most common usage is to process the content within some tags at runtime. It's like [filters](./filters.md), but on steroids because you aren't confined to a single expression.
+
+### Defining a new block
+
+Blocks are defined by the plugin, blocks is a map of name associated with a block descriptor. The block descriptor needs to contain at least a `process` method.
+
+```js
+module.exports = {
+ blocks: {
+ tag1: {
+ process: function(block) {
+ return "Hello "+block.body+", How are you?";
+ }
+ }
+ }
+};
+```
+
+The `process` should return the html content that will replace the tag. Refer to [Context and APIs](./api.md) to learn more about `this` and GitBook API.
+
+### Handling block arguments
+
+Arguments can be passed to blocks:
+
+```
+{% tag1 "argument 1", "argument 2", name="Test" %}
+This is the body of the block.
+{% endtag1 %}
+```
+
+And arguments are easily accessible in the `process` method:
+
+```js
+module.exports = {
+ blocks: {
+ tag1: {
+ process: function(block) {
+ // block.args equals ["argument 1", "argument 2"]
+ // block.kwargs equals { "name": "Test" }
+ }
+ }
+ }
+};
+```
+
+### Handling sub-blocks
+
+A defined block can be parsed into different sub-blocks, for example let's consider the source:
+
+```
+{% myTag %}
+ Main body
+ {% subblock1 %}
+ Body of sub-block 1
+ {% subblock 2 %}
+ Body of sub-block 1
+{% endmyTag %}
+```
diff --git a/docs/plugins/create.md b/docs/plugins/create.md
index 3a5e346..64dcc8b 100644
--- a/docs/plugins/create.md
+++ b/docs/plugins/create.md
@@ -8,13 +8,24 @@ A GitBook plugin is a node package published on NPM that follow a defined conven
The `package.json` is a manifest format for describing **Node.js modules**. GitBook plugins are built on top of Node modules. It declares dependencies, version, ownership, and other information required to run a plugin in GitBook. This document describes the schema in detail.
-```
+A plugin manifest `package.json` can also contain details about the required configuration. The configuration schema is defined in the `gitbook` field of the `package.json` (This field follow the [JSON-Schema](http://json-schema.org) guidelines):
+
+```js
{
"name": "gitbook-plugin-mytest",
"version": "0.0.1",
"description": "This is my first GitBook plugin",
"engines": {
"gitbook": ">1.x.x"
+ },
+ "gitbook": {
+ "properties": {
+ "myConfigKey": {
+ "type": "string",
+ "default": "it's the default value",
+ "description": "It defines my awesome config!"
+ }
+ }
}
}
```
diff --git a/docs/plugins/filters.md b/docs/plugins/filters.md
new file mode 100644
index 0000000..9ee9493
--- /dev/null
+++ b/docs/plugins/filters.md
@@ -0,0 +1,57 @@
+# Extend Filters
+
+Filters are essentially functions that can be applied to variables. They are called with a pipe operator (`|`) and can take arguments.
+
+```
+{{ foo | title }}
+{{ foo | join(",") }}
+{{ foo | replace("foo", "bar") | capitalize }}
+```
+
+### Defining a new filter
+
+Plugins can extend filters by defining custom functions in their entry point under the `filters` scope.
+
+A filter function takes as first argument the content to filter, and should return the new content.
+Refer to [Context and APIs](./api.md) to learn more about `this` and GitBook API.
+
+```js
+module.exports = {
+ filters: {
+ hello: function(name) {
+ return 'Hello '+name;
+ }
+ }
+};
+```
+
+The filter `hello` can then be used in the book:
+
+```
+{{ "Aaron"|hello }}, how are you?
+```
+
+### Handling block arguments
+
+Arguments can be passed to filters:
+
+```
+Hello {{ "Samy"|fullName("Pesse", man=true}} }}
+```
+
+Arguments are passed to the function, named-arguments are passed as a last argument (object).
+
+```js
+module.exports = {
+ filters: {
+ fullName: function(firstName, lastName, kwargs) {
+ var name = firstName + ' ' + lastName;
+
+ if (kwargs.man) name = "Mr" + name;
+ else name = "Mrs" + name;
+
+ return name;
+ }
+ }
+};
+``` \ No newline at end of file
diff --git a/docs/plugins/hooks.md b/docs/plugins/hooks.md
index e69de29..7d81b1d 100644
--- a/docs/plugins/hooks.md
+++ b/docs/plugins/hooks.md
@@ -0,0 +1,90 @@
+# Hooks
+
+Hooks is a method of augmenting or altering the behavior of the process, with custom callbacks.
+
+### List of hooks
+
+### Relative to the global pipeline
+
+| Name | Description | Arguments |
+| ---- | ----------- | --------- |
+| `init` | Called after parsing the book, before generating output and pages. | None |
+| `finish:before` | Called after generating the pages, before copying assets, cover, ... | None |
+| `finish` | Called after everything else. | None |
+
+### Relative to the page pipeline
+
+> It is recommended using [templating](./templating.md) to extend page parsing.
+
+| Name | Description | Arguments |
+| ---- | ----------- | --------- |
+| `page:before` | Called before running the templating engine on the page | Page Object |
+| `page` | Called before outputting and indexing the page. | Page Object |
+
+##### Page Object
+
+```js
+{
+ // Parser named
+ "type": "markdown",
+
+ // File Path relative to book root
+ "path": "page.md",
+
+ // Absolute file path
+ "rawpath": "/usr/...",
+
+ // Title of the page in the SUMMARY
+ "title": "",
+
+ // Content of the page
+ // Markdown/Asciidoc in "page:before"
+ // HTML in "page"
+ "content": "# Hello"
+}
+```
+
+##### Example to add a title
+
+In the `page:before` hook, `page.content` is the markdown/asciidoc content.
+
+```js
+{
+ "page:before": function(page) {
+ page.content = "# Title\n" +page.content;
+ return page;
+ }
+}
+```
+
+##### Example to replace some html
+
+In the `page` hook, `page.content` is the HTML generated from the markdown/asciidoc conversion.
+
+```js
+{
+ "page": function(page) {
+ page.content = page.content.replace("<b>", "<strong>")
+ .replace("</b>", "</strong>");
+ return page;
+ }
+}
+```
+
+
+### Asynchronous Operations
+
+Hooks callbacks can be asynchronous and return promises.
+
+Example:
+
+```js
+{
+ "init": function() {
+ return writeSomeFile()
+ .then(function() {
+ return writeAnotherFile();
+ });
+ }
+}
+```