Create a gitbook plugin locally without publishing it

I want to learn how to write plugins for gitbook (because I want to use it and you will need custom plugins).

However, I feel that the documentation is a bit sparse on this issue, and reading the code from existing plugins doesn't help me much in this matter:

How can I check the plugin before publishing it. The documentation gives some hints about how the plugin should look, and then it tells me to publish it.

Of course, I don’t want to do this, I want to develop and test it locally before publishing. But I do not understand how this can be done.

I tried to copy the installed plugin inside /usr/lib/node_modules/gitbook-cli/node_modules/ to create a new plugin, but when I try to use the plugin in a book, I get an error message telling me to install the plugin via npm .

Is there a way to use the plugin (for testing, or maybe at all) without publishing it to npm?

+5
source share
3 answers

You can skip the initial publication in NPM by linking to your plugin directory in the node_modules directory. Assume the directory structure:

 projects/ my-book/ node_modules/ other_files... awesome-plugin/ plugin_files... 

then you could do

 cd projects/my-book/node_modules ln -s ../../awesome-plugin 

You may need to add the plugin to book.json to register, but try it without the first. I also assume * NIX environment; You may have to look for commands for Windows, but the concept is the same.


Apart from my answer above, you can use NPM packages with names to publish your plugin in a private namespace (e.g. @uli_1973/awesome-plugin ). Then, when you are ready for a great time, you can require a name without a name for your plugin ( awesome-plugin ).

+5
source

The gitbook plugin is just an NPM module. You can use the npm module development method locally in the development of the gitbook plugin.

Npm provides a link command to handle this situation.

First, in your gitbook plugins folder, run the command below to create a globally installed symbolic link to your plugin:

 npm link 

Next, in the gitbook folder, run the following command to link the global gitbook-plugin-name symbolic link in the gitbook node_modules folder:

 npm link gitbook-plugin-name 

Set up your plugin in book.json . Now you can test the plugin in your gitbook without publishing.

+2
source

This is not a definitive answer (therefore, I will not β€œaccept” it), but a workaround that I have found that may be acceptable, depending on the circumstances.

What is possible:

  • Burn (preliminary) plugin
  • publication in NPM
  • Add plugin to book.json book
  • run gitbook install in the book directory

This will download and install the plugin in the node_module directory. Changing JavaScript files will take effect on subsequent builds of gitbook. Thus, you can continue to develop your plugin locally.

What you need to see is how (if) this modified plugin can be published after it's ready.

And this may be considered a (more or less serious) flaw that you now have a completely unrepresentable plugin published on npm.

+1
source

All Articles