Run gitbook cli commands using a local copy installed with NPM
Because gitbook-cli installs gitbook to the user home directory and we needed to monkey-patch gitbook from the using project.
Install gitbook locally with npm:
npm install --save-dev gitbook
Install gitbook-cli-local:
npm install --save-dev @tepez/gitbook-cli-local
Start any gitbook command, e.g. serve:
node node_modules/@tepez/gitbook-cli-local/bin/gitbook.js serve
Or, if you need to make modifications, e.g. monkey-patching, gitbook first:
constParse=require('gitbook/lib/parse');constorigParsePagesList=Parse.parsePagesList;// Monkey-patch parsePagesList to set the direction of a page based on the language// of the book, instead of using wooorm/direction to determine its direction from its content// As is done by gitbook:// https://github.com/GitbookIO/gitbook/blob/3.2.2/lib/parse/parsePageFromString.js#L18// This will fail to recognize a text as Hebrew if the first line is for example// `# {{ page.title }}`Parse.parsePagesList=function(book){constlanguage=book.getConfig().getValue('language');returnorigParsePagesList.apply(this,arguments).then((pages)=>{if(language==='he'){returnpages.map((page)=>page.set('dir','rtl'));}returnpages;})};require('@tepez/gitbook-cli-local/bin/gitbook.js');