Gitbook是一个使用Git和Markdown生成在线书籍的工具。
首先,Gitbook是基于Node.js的命令行工具,先安装Node.js。
可以到Node.js官网下载。
然后在命令行执行node -v
查看Node.js版本,以此来确认是否安装成功。
$ node -v
v10.16.0
接下来就可以安装Gitbook了,它是使用npm来进行安装的,在命令行输入如下命令:
$ npm install gitbook -g
同样也可以通过查看版本来确认是否安装成功,此处V
要大写。
$ gitbook -V
CLI version: 2.3.2
GitBook version: 3.2.3
在命令行进入放置Gitbook的文件夹,运行如下命令:
$ gitbook init
warn: no summary file in this book
info: create README.md
info: create SUMMARY.md
info: initialization is finished
会自动生成两个Markdown文件。
其中README.md
就是书的简介。
SUMMARY.md
就像是书的目录,是一个链接列表。
链接名字是章节名字,链接指向的是章节文件的路径,可以通过分级列表来添加子章节。
# Summary
* [简介](README.md)
* [章节 1](Chapter1.md)
* [章节 2](Chapter2.md)
* [章节 3](Chapter3/Chapter3.md)
* [子章节](Chapter3/Paragraph.md)
再运行一次gitbook init
,就可以生成这个目录结构对应的目录及文件。
$ gitbook init
info: create Chapter1.md
info: create Chapter2.md
info: create Chapter3/Chapter3.md
info: create Chapter3/Paragraph.md
info: create SUMMARY.md
info: initialization is finished
运行tree
命令可以看到整个目录结构。
$ tree
.
├── Chapter1.md
├── Chapter2.md
├── Chapter3
│ ├── Chapter3.md
│ └── Paragraph.md
├── README.md
└── SUMMARY.md
1 directory, 6 files
运行gitbook serve
,通过这个地址 http://localhost:4000 本地预览。