各大站长平时除了写博客外,可以使用 GitBook 将一系列的博客专栏整理成电子书出版。这样既方便各大网友阅读,还可以带来新的离线阅读功能。
在以前我们只能靠出版社,或者编写 word 文档,来实现。然而有了 gitbook 之后,一切都变得那么简单了。今天就为大家分享一下,如何在 Windows 系统上安装 gitbook,进行电子书的编写和发布。
1. GitBook Editor
最简单的方式就是使用GitBook编辑器,没有什么难度,后面的教程主要针对命令行的方式
PS:GitBook的book页面默认没有download按钮的
需要到设置中打开,打开后再次publish生效
同步GitHub
更新失败,无法转换成pdf/mobi/epub格式
Token
git config --global gitbook.user username
git config --global gitbook.token yourtoken
2. 安装 nodejs
首先我们需要做的是安装 Nodejs。大家可以到 Nodejs 的官网进行下载。下载完成后,执行双击进行运行安装。安装完成后,打开 cmd 命令行,输入 node -v 查看安装的 nodejs 的相关版本信息。
也可以在安装目录中找到 node.exe 文件,打开输入 node -v 查看安装的 nodejs 的相关版本信息。
3. nodejs 镜像配置
nodejs 安装完成后,我们就可以开始安装 gitbook 了。但是在安装之前,我们还需要配置一下 nodejs 插件安装的下载镜像地址。因为默认的镜像地址是在国外,需要翻墙才可以访问,因此我们需要设置国内的镜像地址。国内的我推荐大家使用阿里巴巴的镜像地址 http://registry.npm.taobao.org 。执行下面的命令,进行配置。
npm config set registry http://registry.npm.taobao.org
除了上面的方法外,我们也可以在用户主目录下编辑 .npmrc 文件,添加一行 registry=http://registry.npm.taobao.org 保存就可以了。用户的主目录一般在 C:\Users\Administrator ,具体随你的操作系统系统盘而定。
4. 全局安装 gitbook
现在我们可以开始安装全局的 gitbook 了。执行 npm install gitbook-cli -g 命令,进行安装。安装的过程中,由于需要下载安装包,因此大家需要等待一定的时间,具体时长取决于你的网速和硬件配置。
安装完成后,我们可以执行 gitbook -v 查看安装的版本信息。
5. gitbook 常用的命令
安装完成了后,我们就可以开始编写内容,进行电子书发布了。我这里给大家推荐3种最常用的命令
gitbook init //初始化目录文件
gitbook help //列出gitbook所有的命令
gitbook --help //输出gitbook-cli的帮助信息
gitbook build //生成静态网页
gitbook serve //生成静态网页并运行服务器
gitbook build --gitbook=2.0.1 //生成时指定gitbook的版本, 本地没有会先下载
gitbook ls //列出本地所有的gitbook版本
gitbook ls-remote //列出远程可用的gitbook版本
gitbook fetch 标签/版本号 //安装对应的gitbook版本
gitbook update //更新到gitbook的最新版本
gitbook uninstall 2.0.1 //卸载对应的gitbook版本
gitbook build --log=debug //指定log的级别
gitbook builid --debug //输出错误信息
gitbook
C:\Users\AllenIverson\Desktop\gitcourse>gitbook
Usage: gitbook [options] [command]
Commands:
ls List versions installed locally
current Display currently activated version
ls-remote List remote versions available for install
fetch [version] Download and install a <version>
alias [folder] [version] Set an alias named <version> pointing to <folder>
uninstall [version] Uninstall a version
update [tag] Update to the latest version of GitBook
help List commands for GitBook
* run a command with a specific gitbook version
Options:
-h, --help output usage information
-v, --gitbook [version] specify GitBook version to use
-d, --debug enable verbose error
-V, --version Display running versions of gitbook and gitbook-cli
gitbook serve
C:\Users\AllenIverson\Desktop\gitbook-test>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 3 pages
info: found 0 asset files
info: >> generation finished with success in 1.9s !
Starting server ...
Serving book on http://localhost:4000
gitbook ls
C:\Users\AllenIverson\Desktop\gitcourse>gitbook ls
GitBook Versions Installed:
* 3.2.2
Run "gitbook update" to update to the latest version.
gitbook ls-remote
C:\Users\AllenIverson\Desktop\gitcourse>gitbook ls-remote
Available GitBook Versions:
4.0.0-alpha.5, 4.0.0-alpha.4, 4.0.0-alpha.3, 4.0.0-alpha.2, 4.0.0-alpha.1, 3.2.2, 3.2.1, 3.2.0, 3.2.0-pre.1, 3.2.0-
pre.0, 3.1.1, 3.1.0, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-pre.15, 3.0.0-pre.14, 3.0.0-pre.13, 3.0.0-pre.12, 3.0.0-pre.11, 3
.0.0-pre.10, 3.0.0-pre.9, 3.0.0-pre.8, 3.0.0-pre.7, 3.0.0-pre.6, 3.0.0-pre.5, 3.0.0-pre.4, 3.0.0-pre.3, 3.0.0-pre.2, 3.0
.0-pre.1, 2.6.7, 2.6.6, 2.6.5, 2.6.4, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.2, 2.5.1, 2.5.0, 2.5.0-beta.7, 2.5.0-beta.6, 2.5.0
-beta.5, 2.5.0-beta.4, 2.5.0-beta.3, 2.5.0-beta.2, 2.5.0-beta.1, 2.4.3, 2.4.2, 2.4.1, 2.4.0, 2.3.3, 2.3.2, 2.3.1, 2.3.0,
2.2.0, 2.1.0, 2.0.4, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-beta.5, 2.0.0-beta.4, 2.0.0-beta.3, 2.0.0-beta.2, 2.0.0-beta.1,
2.0.0-alpha.9, 2.0.0-alpha.8, 2.0.0-alpha.7, 2.0.0-alpha.6, 2.0.0-alpha.5, 2.0.0-alpha.4, 2.0.0-alpha.3, 2.0.0-alpha.2,
2.0.0-alpha.1
Tags:
latest : 3.2.2
pre : 4.0.0-alpha.5
gitbook -V
C:\Users\AllenIverson\Desktop\gitcourse>gitbook -V
CLI version: 2.3.0
GitBook version: 3.2.2
gitbook pdf
C:\Users\AllenIverson\Desktop\gitcourse>gitbook pdf
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 0 asset files
EbookError: Error during ebook generation: 'ebook-convert' is not recognized as an internal or external command,
operable program or batch file.
错误提示:ebook-convert不是内部或外部命令,原因是GitBook在生成PDF的过程中使用到calibre的转换功能,没有安装calibre或安装了calibre没有配置环境变量都会导致转换PDF失败
C:\Users\AllenIverson\Desktop\gitcourse>gitbook pdf
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 8.6s !
info: >> 1 file(s) generated
安装calibre后,转换成功。PS:安装calibre后需要重新启动命令行窗口
设置PDF输出格式
book.json
{
"gitbook": "2.x.x",
"title": "Go语言圣经",
"description": "<The Go Programming Language>中文版",
"language": "zh",
"structure": {
"readme": "preface.md"
},
"pluginsConfig": {
"fontSettings": {
"theme": "white",
"family": "msyh",
"size": 2
},
"plugins": [
"yahei",
"katex",
"-search"
]
},
"pdf": {
"pageNumbers": true,
"fontFamily": "Arial",
"fontSize": 12,
"paperSize": "a4",
"margin": {
"right": 62,
"left": 62,
"top": 56,
"bottom": 56
}
}
}
Variable | Description |
---|---|
pdf.pageNumbers |
是否添加页码,默认是true |
pdf.fontSize |
字体大小,默认是12 |
pdf.fontFamily |
字体,默认字体是Arial) |
pdf.paperSize |
Paper size, options are 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'legal', 'letter' (default is a4 ) |
pdf.margin.top |
Top margin (default is 56 ) |
pdf.margin.bottom |
Bottom margin (default is 56 ) |
pdf.margin.right |
Right margin (default is 62 ) |
pdf.margin.left |
Left margin (default is 62 ) |
gitbook mobi
C:\Users\AllenIverson\Desktop\gitcourse>gitbook mobi
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 32 pages
info: found 34 asset files
info: >> generation finished with success in 26.0s !
info: >> 1 file(s) generated
gitbook epub
C:\Users\AllenIverson\Desktop\gitcourse>gitbook epub
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 32 pages
info: found 34 asset files
info: >> generation finished with success in 18.5s !
info: >> 1 file(s) generated