summaryrefslogtreecommitdiffstats
path: root/docs/plugins/filters.md
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/plugins/filters.md
parent1d795076657f7f58fe15e647ad2a00dd37501ae0 (diff)
downloadgitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.zip
gitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.tar.gz
gitbook-e8fe4a2dea6dcac5b987f78ebd068b3bafbe0e45.tar.bz2
Improve docs for plugins
Diffstat (limited to 'docs/plugins/filters.md')
-rw-r--r--docs/plugins/filters.md57
1 files changed, 57 insertions, 0 deletions
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