1.下载nodejs

地址:https://nodejs.org/dist/
根据项目要求选择版本,要选择带有-linux-x64.tar.gz结尾的安装包,这种是二进制文件安装,方便快捷,这里我选择的是v10.16.0

完整地址: https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.gz

2.安装gitbook

1
2
3
4
5
6
7
8
9
wget -P /usr/local/src  https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.gz

tar -zxvf node-v10.16.0-linux-x64.tar.gz

cp -a node-v10.16.0-linux-x64 /usr/local/node

ln -sf /usr/local/node/bin/node /usr/bin/node
ln -sf /usr/local/node/bin/npm /usr/bin/npm

在配置好node、npm的环境变量以后安装gitbook

1
2
3
4
5
6
7
8
npm install -g cnpm --registry=https://registry.npm.taobao.org

ln -sf /usr/local/node/bin/cnpm /usr/bin/cnpm

cnpm install -g gitbook-cli

#装完以后添加gitbook的全局变量
ln -sf /usr/local/node/bin/gitbook /usr/bin/gitbook

3.部署

  • 初始化
    1
    2
    3
    4
    5
    mkdir -p /data/book && cd /data/book

    gitbook init

    #执行完会在当前目录产生README.md,SUMMARY.md两个文件
  • 编辑示例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # Summary

    * [Introduction](README.md)
    * [首页](index.md)
    * [wifi](wifi.md)
    * 邮箱
    * [mail](mail/mail.md)
    * 打印机
    * [1.下载](printer/download.md)
    * [2.安装](printer/install.md)
  • 目录初始化
    1
    2
    3
    gitbook init #自动生成对应的目录及文件夹

    gitbook serve . #生成html静态文件,并启动一个web站点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost book]# gitbook serve .
Live reload server started on port: 35729
Press CTRL+C to quit ...

info: 7 plugins are installed
info: loading plugin "livereload"... OK
info: loading plugin "highlight"... OK
info: loading plugin "search"... OK
info: loading plugin "lunr"... OK
info: loading plugin "sharing"... OK
info: loading plugin "fontsettings"... OK
info: loading plugin "theme-default"... OK
info: found 7 pages
info: found 1 asset files
info: >> generation finished with success in 0.7s !

Starting server ...
Serving book on http://localhost:4000

  • 访问
    1
    浏览器打开 http://ip:4000即可访问

后续修改或添加文件

  • 在添加好目录后或修改文件后都需要执行 gitbook initgit serve .来生成静态文件和更新站点

4.添加插件

  • SUMMARY.md同级目录下创建book.json文件,写入以下内容。

    1
    2
    3
    4
    5
    6
    {
    "plugins":["expandable-chapters"], # 增加目录隐藏插件
    "back-to-top-button", # 增加回到顶部按钮
    "page-treeview" # 增加业内目录按钮
    }

  • 执行gitbook install

  • 执行gitbook build

  • 执行gitbook serve .打开站点即可查看效果

更多插件:http://www.zhaowenyu.com/gitbook-doc/plugins/plugins-index.html

5.一些错误

1.对于使用node10以上版本的,在gitbook init的时候会出现以下错误

1
2
3
4
5
6
7
8
9
mist@zss$ gitbook  init
Installing GitBook 3.2.3
/usr/local/node/lib/node_modules/gitbook-cli/node_modules/_npm@5.1.0@npm/node_modules/graceful-fs/polyfills.js:287
if (cb) cb.apply(this, arguments)
^

TypeError: cb.apply is not a function
at /usr/local/node/lib/node_modules/gitbook-cli/node_modules/_npm@5.1.0@npm/node_modules/graceful-fs/polyfills.js:287:18

解决办法

https://blog.csdn.net/weixin_48299611/article/details/108979495

1
2
3
4
5
6
7
8
9
10
11
12
1.node版本切换到10.x版本

2.禁用statFix函数

/usr/local/node/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js


将此文件`polyfills.js`中有关statFix函数的地方全部都注释掉
我这里显示的62-64行和278-290行


再次gitbook init即可

2.增加page-treeview插件后隐藏版权信息文字

解决办法

1
2
3
4
5
6
7
90        <div style="display: none;" class="treeview__container-title">
91 <span class="treeview__main-title">Treeview</span>
92 <span class="treeview__copyright">${options.copyright} all right reserved, powered by
93 <a href="https://github.com/aleen42" target="_blank">aleen42</a>
94 </span>
95 </div>`).replace(/\r?\n|\t/g, '') : '';

打开 ../node_modules/gitbook-plugin-page-treeview/lib/index.js ,找到大概90行的位置,在div里添加样式style="display: none;",即可隐藏.

6.shell脚本更新到服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
source /etc/profile

PROJECT_PATH="/data/www/book/doc"
BOOK_FILE="/data/www/book"
RSYNC_FILE="/data/www/book/_book"
PROJECT_IP="xxxx"

echo "**********开始同步*********"
cd ${PROJECT_PATH}
git pull origin master
tar -zcvf src/js.tgz SDK.js
date

echo "********生成静态文件********"
cd ${BOOK_FILE}
gitbook build

# 替换链接
for f in `grep "Published with GitBook" -rl ${RSYNC_FILE}`;do
sed -i "s/www.gitbook.com/wiki.xxx.com/g" $f
sed -i "s/Published with GitBook/@2022 - xxx/g" $f
echo "$f ok"
done

#代码更新
rsync -zcav -e ssh \
--exclude=".git"\
--exclude=".gitignore" \
${RSYNC_FILE} root@${PROJECT_IP}:/data/www/website