GitBook安装及使用

1、简介
GitBook 是基于Node.js的命令行工具,可以使用GitHub/Git和Markdown来制作精美的电子书。

GitBook支持多种文档格式,如pdf、epub、mobi等格式。

GitBook目前已停止更新,所以如果还要继续使用GitBook的话,建议使用低版本的NodeJS或按下文所述修改报错地方。另外,推荐使用VuePress来搭建自己的技术文档。

VuePress官方地址:
https://vuepress.vuejs.org/zh/guide/

VuePress Demo地址:
https://vuepress-theme-blog.billyyyyy3320.com/

大家可以对比GitBook和VuePress的示例,选择自己喜欢的方式搭建技术文档。

GitBook使用案例参考:

纷享企业互联概览
http://crm.eplanningsoft.com/mob/guide/fconnect/

云图使用手册
https://bd-ca-visualization.vlab.eduyun.cn/documents/

OpenResty 最佳实践
http://openresty.kunkkawu.com/

关于开发指南
https://devdoc.jeejio.com/

及策
https://jice.io/manual/index.html

2、GitBook安装

2.1 NodeJS安装

NodeJS下载地址:https://nodejs.org/en/
下载最新稳定版本安装即可。

安装完毕后,使用node -v检查是否安装成功。

C:\Users\whwtr>node -v
v16.13.2

C:\Users\whwtr>

2.2 GitBook安装
通过npm安装,执行命令:
npm install gitbook-cli -g

安装完成后,通过gitbook -V命令查看是否安装成功。

如果报错:

C:\Users\whwtr>gitbook -V
CLI version: 2.3.2
Installing GitBook 3.2.3
...\npm\node_modules\gitbook-cli\node_modules\npm\node_modules\graceful-fs\polyfills.js:287
      if (cb) cb.apply(this, arguments)
                 ^

TypeError: cb.apply is not a function
    at ...\npm\node_modules\gitbook-cli\node_modules\npm\node_modules\graceful-fs\polyfills.js:287:18
    at FSReqCallback.oncomplete (node:fs:199:5)

是因为NodeJS版本太高导致,可以选择Node12低版本或根据路径找到polyfills.js文件,将62-64行注释掉,具体代码如下:

fs.stat = statFix(fs.stat)
fs.fstat = statFix(fs.fstat)
fs.lstat = statFix(fs.lstat)

然后重新执行gitbook -V即可。

2.3 GitBook使用

(1)gitbook init初始化

新建mygitbook文件夹,然后进入该目录;

执行初始化命令:gitbook init

会自动生成两个必要的文件 README.md 和 SUMMARY.md。

README.md:首页文件
SUMMARY.md:目录文件

如果gitbook init报错:
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type

原因还是因为Node版本太高导致,可以将NodeJS版本降至12的,或自己手动创建SUMMARY.md也可以。

SUMMARY.md内容格式如下:

# GitBook文档

* [介绍](README.md)

* [目录一](test/test.md)
    * [测试一](test/test.md)
    * [测试二](test/test.md)

(2)gitbook serve预览

执行预览命令:gitbook serve

gitbook会启动一个4000端口用于预览,可以在浏览器打开网址: http://localhost:4000 预览效果。如下图示:

gitbook-01.pnggitbook-01.png

(3)gitbook build编译

执行编译命令:gitbook build

运行该命令后会在根文件夹生成一个 _book 文件夹,包含了书籍的所有 html 文件. 编译完成后,即可将_book文件夹部署到服务器上直接访问。

扩展:

gitbook常用命令:

D:\test\mygitbook>gitbook help
    build [book] [output]       build a book
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)
        --format                Format to build to (Default is website; Values are website, json, ebook)
        --[no-]timing           Print timing debug information (Default is false)

    serve [book] [output]       serve the book as a website for testing
        --port                  Port for server to listen on (Default is 4000)
        --lrport                Port for livereload server to listen on (Default is 35729)
        --[no-]watch            Enable file watcher and live reloading (Default is true)
        --[no-]live             Enable live reloading (Default is true)
        --[no-]open             Enable opening book in browser (Default is false)
        --browser               Specify browser for opening book (Default is )
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)
        --format                Format to build to (Default is website; Values are website, json, ebook)

    install [book]              install all plugins dependencies
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)

    parse [book]                parse and print debug information about a book
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)

    init [book]                 setup and create files for chapters
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)

    pdf [book] [output]         build a book into an ebook file
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)

    epub [book] [output]        build a book into an ebook file
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)

    mobi [book] [output]        build a book into an ebook file
        --log                   Minimum log level to display (Default is info; Values are debug, info, warn, error, disa  bled)

(完)

添加新评论