用GitBook来写自己的电子书

我这个人呢,不怎么爱学习,但是有点小折腾。看到别人搞什么,自己就想也搞点什么。当然,最后的结果是惨不忍睹,但是现在比以前要好多了-学会了坚持。其实,我看到了网上很多的电子书,做得也不错。所以,这里就想学习一下,把自己学习的东西,也整理成简单点的电子书,留着自己时常复习,做备忘。

这里我们可以借助开源的GitBook来记录我们的学习过程,日积月累慢慢的就会成为”书”

安装GitBook

在安装GitBook之前,需要先安装NodeNode安装非常简单,直接去官网下载,然后直接下一步即可,安装过程中,Node会自动将环境变量添加到PATH中,省去了手动配置环节,这里我已经安装过来,所以就不在演示手动安装,直接看看我安装好的Node版本吧

1
2
D:\>node -v
v13.12.0

现在我们来安装GitBook

1
npm install gitbook-cli -g

当然,如果有安装cnpm,那么就用cnpm来安装吧,会快一些也会更顺利一些。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cnpm install gitbook-cli -g


Downloading gitbook-cli to C:\Users\wmymt\AppData\Roaming\npm\node_modules\gitbook-cli_tmp
Copying C:\Users\wmymt\AppData\Roaming\npm\node_modules\gitbook-cli_tmp\_gitbook-cli@2.3.2@gitbook-cli to C:\Users\wmymt\AppData\Roaming\npm\node_modules\gitbook-cli
Installing gitbook-cli's dependencies to C:\Users\wmymt\AppData\Roaming\npm\node_modules\gitbook-cli/node_modules
[1/11] semver@5.3.0 installed at node_modules\_semver@5.3.0@semver
[2/11] bash-color@0.0.4 installed at node_modules\_bash-color@0.0.4@bash-color
[3/11] commander@2.11.0 installed at node_modules\_commander@2.11.0@commander
[4/11] q@1.5.0 installed at node_modules\_q@1.5.0@q
[5/11] tmp@0.0.31 installed at node_modules\_tmp@0.0.31@tmp
[6/11] user-home@2.0.0 installed at node_modules\_user-home@2.0.0@user-home
[7/11] fs-extra@3.0.1 installed at node_modules\_fs-extra@3.0.1@fs-extra
[8/11] optimist@0.6.1 installed at node_modules\_optimist@0.6.1@optimist
[9/11] lodash@4.17.4 installed at node_modules\_lodash@4.17.4@lodash
[10/11] npmi@1.0.1 installed at node_modules\_npmi@1.0.1@npmi
[11/11] npm@5.1.0 installed at node_modules\_npm@5.1.0@npm
All packages installed (20 packages installed from npm registry, used 4s(network 4s), speed 1.77MB/s, json 18(129.67kB), tarball 7.55MB)
[gitbook-cli@2.3.2] link C:\Users\wmymt\AppData\Roaming\npm\gitbook@ -> C:\Users\wmymt\AppData\Roaming\npm\node_modules\gitbook-cli\bin\gitbook.js

这表示已经安装完成,现在我们来验证一下,输入gitbook -V来看看是否正常,如果是第一次的话,执行命令会继续安装

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
D:\>gitbook -V
CLI version: 2.3.2
Installing GitBook 3.2.3
gitbook@3.2.3 C:\Users\wmymt\AppData\Local\Temp\tmp-12932GHc5pr7xs5ns\node_modules\gitbook
├── escape-string-regexp@1.0.5
├── escape-html@1.0.3
├── destroy@1.0.4
├── ignore@3.1.2
├── bash-color@0.0.4
├── gitbook-plugin-livereload@0.0.1
├── cp@0.2.0
├── graceful-fs@4.1.4
├── nunjucks-do@1.0.0
├── github-slugid@1.0.1
├── q@1.4.1
├── spawn-cmd@0.0.2
├── gitbook-plugin-fontsettings@2.0.0
├── open@0.0.5
├── direction@0.1.5
├── is@3.3.0
├── object-path@0.9.2
├── extend@3.0.2
├── json-schema-defaults@0.1.1
├── gitbook-plugin-search@2.2.1
├── jsonschema@1.1.0
├── crc@3.4.0
├── urijs@1.18.0
├── semver@5.1.0
├── front-matter@2.3.0
├── immutable@3.8.2
├── omit-keys@0.1.0 (array-difference@0.0.1, isobject@0.2.0)
├── dom-serializer@0.1.0 (domelementtype@1.1.3, entities@1.1.2)
├── resolve@1.1.7
├── gitbook-plugin-theme-default@1.0.7
├── mkdirp@0.5.1 (minimist@0.0.8)
├── tmp@0.0.28 (os-tmpdir@1.0.2)
├── error@7.0.2 (xtend@4.0.2, string-template@0.2.1)
├── npmi@2.0.1 (semver@4.3.6)
├── send@0.13.2 (etag@1.7.0, range-parser@1.0.3, statuses@1.2.1, fresh@0.3.0, ms@0.7.1, depd@1.1.2, debug@2.2.0, mime@1.3.4, http-errors@1.3.1, on-finished@2.3.0)
├── rmdir@1.2.0 (node.flow@1.2.3)
├── moment@2.13.0
├── js-yaml@3.13.1 (esprima@4.0.1, argparse@1.0.10)
├── cpr@1.1.1 (rimraf@2.4.5)
├── tiny-lr@0.2.1 (parseurl@1.3.3, livereload-js@2.4.0, qs@5.1.0, debug@2.2.0, body-parser@1.14.2, faye-websocket@0.10.0)
├── fresh-require@1.0.3 (is-require@0.0.1, shallow-copy@0.0.1, astw@1.3.0, sleuth@0.1.1, acorn@0.9.0, through2@0.6.5, escodegen@1.14.1)
├── read-installed@4.0.3 (debuglog@1.0.1, util-extend@1.0.3, slide@1.1.6, readdir-scoped-modules@1.1.0, read-package-json@2.1.1)
├── gitbook-plugin-highlight@2.0.2 (highlight.js@9.2.0)
├── gitbook-plugin-lunr@1.2.0 (html-entities@1.2.0, lunr@0.5.12)
├── gitbook-plugin-sharing@1.0.2 (lodash@3.10.1)
├── chokidar@1.5.0 (async-each@1.0.3, path-is-absolute@1.0.1, inherits@2.0.4, glob-parent@2.0.0, is-glob@2.0.1, is-binary-path@1.0.1, anymatch@1.3.2, readdirp@2.2.1)
├── nunjucks@2.5.2 (asap@2.0.6, yargs@3.32.0, chokidar@1.7.0)
├── cheerio@0.20.0 (entities@1.1.2, css-select@1.2.0, htmlparser2@3.8.3, jsdom@7.2.2, lodash@4.17.15)
├── i18n-t@1.0.1 (lodash@4.17.15)
├── gitbook-markdown@1.3.2 (kramed-text-renderer@0.2.1, gitbook-html@1.3.3, kramed@0.5.6, lodash@4.17.15)
├── gitbook-asciidoc@1.2.2 (gitbook-html@1.3.3, asciidoctor.js@1.5.5-1, lodash@4.17.15)
├── request@2.72.0 (aws-sign2@0.6.0, forever-agent@0.6.1, tunnel-agent@0.4.3, oauth-sign@0.8.2, is-typedarray@1.0.0, caseless@0.11.0, stringstream@0.0.6, aws4@1.9.1, isstream@0.1.2, json-stringify-safe@5.0.1, tough-cookie@2.2.2, qs@6.1.2, node-uuid@1.4.8, combined-stream@1.0.8, mime-types@2.1.26, hawk@3.1.3, bl@1.1.2, har-validator@2.0.6, http-signature@1.1.1, form-data@1.0.1)
├── juice@2.0.0 (deep-extend@0.4.2, slick@1.12.2, batch@0.5.3, cssom@0.3.1, commander@2.9.0, cross-spawn-async@2.2.5, web-resource-inliner@2.0.0)
└── npm@3.9.2
GitBook version: 3.2.3

后面就不会重复安装

1
2
3
D:\>gitbook -V
CLI version: 2.3.2
GitBook version: 3.2.3

使用GitBook新建电子书

1
2
3
4
5
F:\gitbook>gitbook init
warn: no summary file in this book
info: create README.md
info: create SUMMARY.md
info: initialization is finished

会看到gitbook帮我们默认创建了两个文件,现在用vs code打开来看看目录

现在,我们可以借助gitbook serve命令来预览咱们的电子书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
F:\gitbook>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 1 pages
info: found 0 asset files
info: >> generation finished with success in 0.6s !

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

新建目录章节

上面我们成功的创建了电子书并预览到效果,这里我们来简单的认识一下,如何书写?

SUMMARY.md

在这个文件中加入文章的章节目录

1
2
3
4
5
# Summary

* [农民工学编程](README.md)
* [prometheus学习手册](prometheus/prometheus.md)
* [prometheus学习手册](prometheus/install.md)

再次执行gitbook init会自动生成相应的目录和*.md文件

这时候运行gitbook serve就可以看到我们的新建的章节

GitBook是使用Markdown书写,具体语法,大家自己去了解,后面后机会,我会在这个电子书中记录下来。

GitBook发布到七牛云

其实,在执行gitbook serve的时候,会在根目录下生成_book文件夹来存放编译好的静态文件。那么我们在发布的时候,执行gitbook build即可生成电子书静态页面,最后将其上传到七牛云即可。

1
2
3
4
5
6
7
8
9
10
11
12
F:\gitbook>gitbook build
info: 7 plugins are installed
info: 6 explicitly listed
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 3 pages
info: found 2 asset files
info: >> generation finished with success in 0.5s !

关于如何配置七牛云,可以去Hexo静态网页上传到七牛云看看。
这里我就直接上传编译好的文件至七牛云上

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
34
F:\gitbook>qshell qupload2 --overwrite=true --rescan-local=true --src-dir=F:/gitbook/_book --bucket=eyibook
Tip: you can set <ThreadCount> value between 1 and 2000 to improve speed
Writing upload log to file C:\Users\wmymt\.qshell\qupload\e22ed6b0bd6125d988d942432528582f\e22ed6b0bd6125d988d942432528582f.log

Uploading F:\gitbook\_book\gitbook\fonts\fontawesome\FontAwesome.otf => gitbook/fonts/fontawesome/FontAwesome.otf [1/28, 3.6%] ...
Uploading F:\gitbook\_book\gitbook\fonts\fontawesome\fontawesome-webfont.eot => gitbook/fonts/fontawesome/fontawesome-webfont.eot [2/28, 7.1%] ...
Uploading F:\gitbook\_book\gitbook\fonts\fontawesome\fontawesome-webfont.svg => gitbook/fonts/fontawesome/fontawesome-webfont.svg [3/28, 10.7%] ...
Uploading F:\gitbook\_book\gitbook\fonts\fontawesome\fontawesome-webfont.ttf => gitbook/fonts/fontawesome/fontawesome-webfont.ttf [4/28, 14.3%] ...
Uploading F:\gitbook\_book\gitbook\fonts\fontawesome\fontawesome-webfont.woff => gitbook/fonts/fontawesome/fontawesome-webfont.woff [5/28, 17.9%] ...
Uploading F:\gitbook\_book\gitbook\fonts\fontawesome\fontawesome-webfont.woff2 => gitbook/fonts/fontawesome/fontawesome-webfont.woff2 [6/28, 21.4%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-fontsettings\fontsettings.js => gitbook/gitbook-plugin-fontsettings/fontsettings.js [7/28, 25.0%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-fontsettings\website.css => gitbook/gitbook-plugin-fontsettings/website.css [8/28, 28.6%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-highlight\ebook.css => gitbook/gitbook-plugin-highlight/ebook.css [9/28, 32.1%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-highlight\website.css => gitbook/gitbook-plugin-highlight/website.css [10/28, 35.7%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-lunr\lunr.min.js => gitbook/gitbook-plugin-lunr/lunr.min.js [11/28, 39.3%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-lunr\search-lunr.js => gitbook/gitbook-plugin-lunr/search-lunr.js [12/28, 42.9%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-search\lunr.min.js => gitbook/gitbook-plugin-search/lunr.min.js [13/28, 46.4%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-search\search-engine.js => gitbook/gitbook-plugin-search/search-engine.js [14/28, 50.0%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-search\search.css => gitbook/gitbook-plugin-search/search.css [15/28, 53.6%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-search\search.js => gitbook/gitbook-plugin-search/search.js [16/28, 57.1%] ...
Uploading F:\gitbook\_book\gitbook\gitbook-plugin-sharing\buttons.js => gitbook/gitbook-plugin-sharing/buttons.js [17/28, 60.7%] ...
Uploading F:\gitbook\_book\gitbook\gitbook.js => gitbook/gitbook.js [18/28, 64.3%] ...
Uploading F:\gitbook\_book\gitbook\images\apple-touch-icon-precomposed-152.png => gitbook/images/apple-touch-icon-precomposed-152.png [19/28, 67.9%] ...
Uploading F:\gitbook\_book\gitbook\images\favicon.ico => gitbook/images/favicon.ico [20/28, 71.4%] ...
Uploading F:\gitbook\_book\gitbook\style.css => gitbook/style.css [21/28, 75.0%] ...
Uploading F:\gitbook\_book\gitbook\theme.js => gitbook/theme.js [22/28, 78.6%] ...
Uploading F:\gitbook\_book\index.html => index.html [23/28, 82.1%] ...
Uploading F:\gitbook\_book\prometheus\install.html => prometheus/install.html [24/28, 85.7%] ...
Uploading F:\gitbook\_book\prometheus\install.md => prometheus/install.md [25/28, 89.3%] ...
Uploading F:\gitbook\_book\prometheus\prometheus.html => prometheus/prometheus.html [26/28, 92.9%] ...
Uploading F:\gitbook\_book\prometheus\prometheus.md => prometheus/prometheus.md [27/28, 96.4%] ...
Uploading F:\gitbook\_book\search_index.json => search_index.json [28/28, 100.0%] ...

See upload log at path C:\Users\xxxxx\.qshell\qupload\e22ed6b0bd6125d988d942432528582f\e22ed6b0bd6125d988d942432528582f.log

至此,我们的电子书就上传成功了。来看看效果

我的电子书地址是https://eyibook.52fx.biz/,以后我也会不定时更新,欢迎大家指正批评

GitBook常用命令

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
F:\gitbook>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)
  • gitbook build:将md文档编译为静态html文件并保存在_book文件夹中
  • gitbook serve:启动/预览电子书
  • gitbook install:安装插件;
  • gitbook parse:解析电子书;
  • gitbook init:初始化电子书;
  • gitbook pdf:输出pdf格式电子书;
  • gitbook epub:输出epub格式电子书;
  • gitbook mobi:输出mobi格式电子书;

GitBook插件介绍

我们的电子书虽然生成了,页面还是比较简单,当目录结构较多或者是文章过程的时候,默认情况下没有友好的处理,所以这时候就需要借助一下插件来帮我们做这些事情,这时候就需要涉及到一个名叫book.json的文件。这里首先就是新建一个book.json文件,并且做如下的配置

1
2
3
4
5
6
7
8
9
10
11
12
{
"title" : "农民工学编程",
"author" : "eyiadmin",
"description" : "学习总结",
"language" : "zh-hans",
"links" : {
"sidebar" : {
"农民工学编程" : "https://blogs.52fx.biz/"
}
},
"plugins": ["chapter-fold"]
}

这是一些全局配置

  • title:设置书本的标题
  • author:作者的相关信息
  • description:本书的简单描述
  • language:Gitbook使用的语言
  • links:在左侧导航栏添加链接信息
  • plugins:gitbook插件,添加后,需要执行gitbook install

chapter-fold

chapter-fold支持多层目录折叠,默认下,GitBook是不会帮我们折叠,目录过多,这肯定就会是一个噩梦。这里我们来看看前后对比

sitemap

sitemap可以为我们的gitbook生成站点地图,便于爬虫识别

1
2
3
4
5
6
"plugins": ["chapter-fold","sitemap"],
"pluginsConfig": {
"sitemap": {
"hostname": "https://eyibook.52fx.biz/"
}
}

github地址

theme-default

theme-default3.0.0的默认主题,可以美化我们的gitbook样式

1
2
3
4
5
6
"plugins": ["theme-default"],
"pluginsConfig": {
"theme-default": {
"showLevel": true
}
}

未完待续,后续在使用过程中遇到新东西时,加此文继续更新

#
You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×