Gitbook必备技能

GitBook是一个基于Node.js的命令行工具,可使用Github/Git和Markdown来制作电子书(其实主要是写用户手册,API使用文档之类的啦)。总之,程序员必备技能,这里就不多强调重要性了!markdown语法请自行google,本文只简单介绍一些gitbook的目录结构和简单用法。

安装gitbook-cli

1
npm install gitbook-cli -g

安装好之后,可以大概了解下其功能和命令

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
33
gitbook help
build [book] [output] build a book
--log Minimum log level to display (Default is info; Values are debug, info, warn, error, disabled)
--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, disabled)
--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, disabled)

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, disabled)

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

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, disabled)

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, disabled)

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, disabled)

制作电纸书

初始化

首先创建一个电子书的目录,比如叫做book。然后进入到book目录下,执行以下操作。

1
2
3
4
$ gitbook init

$ ls
README.md SUMMARY.md

执行以上命令会自动创建电纸书必须的README.md和SUMMARY.md文件。

  • README.md文件是你作品的介绍
  • SUMMARY.md是你作品的目录结构

添加内容

初始化之后,我们就可以往book目录下添加子目录和文件(需要按照markdown语法来写内容)了,下面是添加了二级目录和内容后的结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ tree
.
├── LICENSE
├── README.md
├── SUMMARY.md
├── book
│ ├── README.md
│ ├── file.md
│ └── prjinit.md
├── howtouse
│ ├── Nodejsinstall.md
│ ├── README.md
│ ├── gitbookcli.md
│ └── gitbookinstall.md
├── output
│ ├── README.md
│ ├── outfile.md
│ └── pdfandebook.md
└── publish
├── README.md
└── gitpages.md

对应的,我们需要修改SUMMARY.md, 从而建立电子书的目录层次和到具体文章的链接(请注意这里是使用的相对地址,而不是绝对地址)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ cat SUMMARY.md

* [Introduction](README.md)
* [基本安装](howtouse/README.md)
* [Node.js安装](howtouse/nodejsinstall.md)
* [Gitbook安装](howtouse/gitbookinstall.md)
* [Gitbook命令行速览](howtouse/gitbookcli.md)
* [图书项目结构](book/README.md)
* [README.md 与 SUMMARY编写](book/file.md)
* [目录初始化](book/prjinit.md)
* [图书输出](output/README.md)
* [输出为静态网站](output/outfile.md)
* [输出PDF](output/pdfandebook.md)
* [发布](publish/README.md)
* [发布到Github Pages](publish/gitpages.md)
* [结束](end/README.md)

运行服务

1
2
# 默认启动,使用4000端口
gitbook serve

运行起来后,可以在浏览器中通过 http://localhost:4000 来访问启动后的服务。
[注意]: 默认启动使用的是4000端口,当然我们也可以通过参数 -p 来指定使用的接口。

1
2
# 通过-p指定端口
gitbook serve -p 8080

发布

撰写完成后,我们可以生成静态网站用来发布。

1
gitbook build

0%