Giter VIP home page Giter VIP logo

plugin's Introduction

GitBook Sample Plugin

This is a model for GitBook plugins.

How GitBook plugin works?

A plugin for GitBook is a node package that can be published on NPM. It has to follow the name convention: gitbook-plugin-*name*.

package.json

name

The package name should begin with gitbook-plugin-.

Examples: gitbook-plugin-mixpanel, gitbook-plugin-googleanalytics.

engine

The package.json should contain a engine field using the standard norm.

"engines": {
    "gitbook": "*"
}

For example if you want your plugin to supports only GitBook version supperior to 0.3.1:

"engines": {
    "gitbook": ">=0.3.1"
}

entry point

The plugin entry point should return an object with some metadata.

plugin's People

Contributors

aarono avatar bebraw avatar rlmv avatar samypesse avatar timelyportfolio avatar ty- avatar viggyprabhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plugin's Issues

`page` hook: `page.content` is a string, not a list of parsed sections

hook.page claims that:

        // page.content is a list of parsed sections

See; https://github.com/GitbookIO/plugin/blob/master/index.js#L59

However, this is not the case - page.content really is just a string containing the markdown for the currently processed input file; which makes the page hook no different from the page:before hook. When implementing a plugin, add the following statement to each of the hooks to verify.

        console.log('typeof page.content:', (typeof page.content));

not working with codemirror.js

When I add the codemirror.js lik following ,

 book: {
        assets: "./book",
        js: [

            "codemirror/lib/codemirror.js"
        ],

I got the following error

Uncaught object app.js:1
makeError app.js:1
_ app.js:1
s app.js:1
requirejs app.js:1
(anonymous function)

plugins with third party libraries

Hi, I created a simple test plug-in that pulls YAML data from a block like this:

var yaml = require('js-yaml');

module.exports = {
    // Extend website resources and html
    website: {
        assets: "./book",
        js: [
            "test.js"
        ],
        css: [
            "test.css"
        ],
        html: {
            "html:start": function() {
                return "<!-- Start book "+this.options.title+" -->"
            },
            "html:end": function() {
                return "<!-- End of book "+this.options.title+" -->"
            },

            "head:start": "<!-- head:start -->",
            "head:end": "<!-- head:end -->",

            "body:start": "<!-- body:start -->",
            "body:end": "<!-- body:end -->"
        }
    },

    // Extend ebook resources and html
    website: {
        assets: "./book",
        js: [
            "test.js"
        ],
        css: [
            "test.css"
        ],
        html: {
            "html:start": function() {
                return "<!-- Start book "+this.options.title+" -->"
            },
            "html:end": function() {
                return "<!-- End of book "+this.options.title+" -->"
            },

            "head:start": "<!-- head:start -->",
            "head:end": "<!-- head:end -->",

            "body:start": "<!-- body:start -->",
            "body:end": "<!-- body:end -->"
        }
    },

    // Extend templating blocks
    blocks: {
        // Author will be able to write "{% myTag %}World{% endMyTag %}"
        carlskii: {
            process: function(blk) {
                data = yaml.safeLoad(blk, 'utf8');
                return data
            }
        }
    },

    // Extend templating filters
    filters: {
        // Author will be able to write "{{ 'test'|myFilter }}"
        myFilter: function(s) {
            return "Hello "+s;
        }
    },

    // Hook process during build
    hooks: {
        // For all the hooks, this represent the current generator

        // This is called before the book is generated
        "init": function() {
            console.log("init!");
        },

        // This is called after the book generation
        "finish": function() {
            console.log("finish!");
        }
    }
};

The intention was to then pass the data back as a JSON string into the block, however, I can only get it to return 'undefined'

Is something I'm missing here ?

How can I get page.path in Gitbook 2.0

I am using page.path in "page:after" for loading file.

At version 2.0 , we cannot use "page:after". So, I try to change with block. However, I have no idea , how to get full file path without page.path .

development of plugin to display file by providing the file path , get error "undefined" in index.js generated by gitbook

Recently, I was assigned to develop a customized plugin for gitbook https://github.com/GitbookIO/gitbook to display the file by providing the path in the README.md , The plugin example is right here :https://github.com/GitbookIO/plugin ,the plugin is supposed to work like :

the link in the README.md : ["hello.java"](src/to/hello.java)=======> after build the gitbook ,it turns to

public class Hello{
    public static void main(String []args){
        System.out.println("Hello");
    }
}

Then In my plugin package, I modify the index.js file like this

module.exports = {
book: {
    assets: "./book",
    js: [
    "test.js"
    ],
    css: [
    "test.css"
    ],
    html: {
        "html:start": function() {
            return "<!-- Start book "+this.options.title+" -->"
        },
        "html:end": function() {
            return "<!-- End of book "+this.options.title+" -->"
        },

        "head:start": "<!-- head:start -->",
        "head:end": "<!-- head:end -->",

        "body:start": "<!-- body:start -->",
        "body:end": "<!-- body:end -->"
    }
},
hooks: {
    // For all the hooks, this represent the current generator

    // This is called before the book is generated
    "init": function() {
        console.log("init!");
        },

    // This is called after the book generation
    "finish": function() {
        console.log("finish!");
    },

    // The following hooks are called for each page of the book
    // and can be used to change page content (html, data or markdown)

    // Before parsing markdown
    "page:before": function(page) {

        console.log("============="+ page.content +"===============");

        var pathlist= page.content.match(/\[\"[A-Za-z]*\.[A-Za-z]*\"\]\([A-Za-z\/\-\d]*\.[A-Za-z]*\)/g);

        if (pathlist) {
            console.log( "The length of the match array is " + pathlist.length +" and the content of the array is "+ pathlist);
        }


        if (pathlist && pathlist.length > 0){

            for (index = 0; index <pathlist.length; index++){

                var fs = require("node-fs");
                var index1 = pathlist[index].indexOf("(");
                var index2 = pathlist[index].indexOf(")");
                var sub_string = pathlist[index].substr(index1+1,index2-index1-1);
                var pathToFile = sub_string;

                console.log(pathToFile);

                if(fs.existsSync(pathToFile)){ 

                    console.log("@@@@@@@@@@@@@@@@"+ pathToFile);

                    if(fs.statSync(pathToFile)){

                        try {
                            fs.openSync(pathToFile, "r")
                            }catch(e){
                                console.log('Error:', e);
                                return;
                                }                

                            console.log("Let's print out the source code below , Seeeeeeeeeeeeee");

                            var data = fs.readFileSync(pathToFile).toString();

                            console.log(data);

                            pathlist[index]=data;
                        }
                    };

            }}
            console.log("here is the results array:||||||||||||||||| " + pathlist +"|||||||||||||||||||||||||||||");

            var pattern = /\[\"[A-Za-z]*\.[A-Za-z]*\"\]\([A-Za-z\/\-\d]*\.[A-Za-z]*\)/;

            var input = page.content;

            for (var i = 0; i < pathlist.length; i++) {

                input = input.replace(pattern, "`"+pathlist[i]+"`");    
            }
        page.content = input;
        console.log(page.content);
        return page;    
    },
    "page": function(page) {
        console.log("page");
    },
    "page:after": function(page) {
        console.log("after");
    }           
  }
};  

then I add the plugin folder which I named "hello" in my node_modules folder and excute

npm install path/to/plugin-hello

in my book directory , I add the plugin configuration in the book.json

{
"plugins"       : ["richquotes", "hello"],
"pluginsConfig":{
    "richquotes" :{
        "todos" : true,
        "fixme" : true,
        "info"  : true
    },
    "hello": {}
    }
}

When I build the gitbook the log is look like this:

C02KG1DGFFT3:gitbooktest v619896$ gitbook build ./
Starting build ...
init!
=============gitbooktobedeleted
==================

This is a gitbook for testing

> **todo**
>


["ContactController.java"](ContactController.java)

["web.xml"](src/to/web.xml)
===============
The length of the match array is 2 and the content of the array is ["ContactController.java"]  
(ContactController.java),["web.xml"](src/to/web.xml)
ContactController.java
@@@@@@@@@@@@@@@@ContactController.java
##################
Let's print out the source code below , Seeeeeeeeeeeeee
public class ContactController{
public static void main(String []args){
    System.out.println("Hello");
 }
}
 src/to/web.xml
 @@@@@@@@@@@@@@@@src/to/web.xml
 ##################
 Let's print out the source code below , Seeeeeeeeeeeeee
 <%%%%?xml%%%%version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee

/web-app_3_0.xsd"
     version="3.0">
</web-app>
here is the results array:||||||||||||||||| public class ContactController{
public static void main(String []args){
    System.out.println("Hello");
}
},<%%%%?xml%%%%version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/n/javaee      
/web-app_3_0.xsd"
     version="3.0">
</web-app>|||||||||||||||||||||||||||||
gitbooktobedeleted
==================

This is a gitbook for testing

> **todo**
>


`public class ContactController{
   public static void main(String []args){
    System.out.println("Hello");
    }
}`

`<%%%%?xml%%%%version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/n/javaee      
/web-app_3_0.xsd"
     version="3.0">
</web-app>`

page
after
finish!
Successfully built!

It looks like everything works fine !

but when I try to open the index.html it generated , the page content is "undefined " and nothing else !

Could anyone give me some hint ,where did I make the mistake ? I am really new to Javascript ! Thanks!

Ebook Resources in Gitbook 2.0

In the index.js , it's using same name website for both ebook and websites.

Do I need to use two website key or only one is enough for both website and ebook ?

// Extend website resources and html
    website: {
// Extend ebook resources and html
    website: {

Is it possible to load js file at start of the body instead of before end of the body

I want to add ace editor in markdown. However, ace.js always call at the end of the page.

Example like following,

<body>
.....
<script>
var editor = ace.edit('code');
</script>
....
....
...
<script src="../gitbook/plugins/pywebtest-gitbook/editor/ace.js"></script>
</body>

ace.js at the bottom. So, ace.edit can't use anymore because ace object didn't found.

I try to use with

document.addEventListener('DOMContentLoaded', function(){
var editor = ace.edit('code');
})

It's not working with ajax call. But working with direct URL or refresh the page.

When navigate , ajax call and change the page. So, DOMContentLoaded is not working.

If I can put ace.js after <body> , it will be better and no need DOMContentLoaded any more.

Multi-Lingual Book Settings

Sample plugin is really great to get started. Is there any way i can inherit the current language that is being rendered in the plugin ? I see that the pages that are returned are scoped within a directory (in case of multi-lingual site)

For example, in a book like this:

en_US/chapter-1/section.html is passed to the page:function(page) { } as chapter-1/section.html

How do i get the language ?

Also the default template has <html lang="en-US">. I want to leverage the language Attribute for a multi-lingual doc so that i need to apply custom settings based on language (For example Fonts)

Any pointers are appreciated

plugin - web editor

How do you make a plugin work with the web editor? Until now I had created them using the native application which has now been discontinued. Now when I try to add a plugin in the book.json file and add

{
"plugins": ["video"]
}

And then use the plugin and it does not work. Am I missing out on something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.