第一本电子书

基于上面的认识,可以用 gitbook 来制作第一本电子书。

初始化环境

现在,需要在一个空文件夹中创建自己的一本书。

首先,在该文件(D:\temp)下执行命令:

gitbook init

D:\temp>gitbook init
warn: no summary file in this book
info: create README.md
info: create SUMMARY.md
info: initialization is finished

自动创建了两个文件:

README.mdSUMMARY.md

添加说明到 README.md

# 第一本GitBook

本书由 gitbook 生成。

添加一个 markdown 文件

我新建了一个 hello.md 的文件,并写入如下内容。

# Hello World

### 欢迎语

欢迎你来到 GitBook 的世界,希望这里有你想要的东西。

添加菜单到 SUMMARY.md

# Summary

* [Introduction](README.md)

* [Hello](hello.md)

启动服务

在控制台输入命令:gitbook serve

D:\temp>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 2 pages
info: found 4 asset files
info: >> generation finished with success in 0.6s !

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

看到这样的输出,直接在浏览器访问 http://localhost:4000/

Hello

文件目录分析

现在去主文件夹(D:\temp)看可以看到新生成了一个文件夹 _book

.
├── _book
├── hello.md
├── README.md
└── SUMMARY.md

打开 文件夹 _book

.
├── gitbook
|    ├── fonts
|    ├── gitbook-plugin-fontsettings
|    ├── gitbook-plugin-highlight
|    ├── gitbook-plugin-livereload
|    ├── gitbook-plugin-lunr
|    ├── gitbook-plugin-search
|    ├── gitbook-plugin-sharing
|    ├── images
|       ├── apple-touch-icon-precomposed-152.png
|       └── favicon.ico
|    ├── gitbook.js
|    ├── style.css
|    └── theme.js
├── hello.html
├── index.html
└── search_index.json

这个 文件夹 _book 中包含了点子书中的所有内容,将整个文件夹打包放在任何一个 web 服务器中,都能正常显示。

这是个非常简单的例子,之后通过 book.json 的配置和插件的扩展,能实现更多个性化的功能。

完整创建一本 Book

  1. 安装 nvm-windows、通过 nvm 安装 nodejs、npm,之后安装 Honkit。

    自行选择对应的你想设置的安装路径即可

​​ Hello

PS C:\Users\admin> nvm list available

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    21.7.1    |   20.12.0    |   0.12.18    |   0.11.16    |
|    21.7.0    |   20.11.1    |   0.12.17    |   0.11.15    |

PS C:\Users\admin> nvm install 21.7.1
Downloading node.js version 21.7.1 (64-bit)...
Extracting node and npm...
Complete
npm v10.5.0 installed successfully.

PS C:\Users\admin> nvm use 21.7.1
Now using node v21.7.1 (64-bit)
  1. 在对应的 Book 目录下执行以下命令进行 Honkit 初始化
# 这一步会生成 package.json 和 packagelock.json 文件
# 主要记录一些 nodejs 项目的依赖信息
gitbook-guide-book> npm init -y

# 接着安装 honkit
# 这一步会更新 package.json 和 packagelock.json
# 会生成 node_modules,初始化完成,以后该 book 安装的插件也会在这个文件夹里面
gitbook-guide-book> npm install honkit --save-dev

# 更新一下有漏洞的组件
npm audit fix --force

# 初始化
npx honkit init
  1. 启动 Honkit
# 会生成 _book 文件夹,里面就是本 book 编译后的静态文件
gitbook-guide-book> npx honkit serve
  1. 本书用到的插件
npm install gitbook-plugin-code
npm i gitbook-plugin-expandable-chapters-interactive
npm i gitbook-plugin-versions-select-revised
npm i gitbook-plugin-tbfed-pagefooter-fix
npm i gitbook-plugin-flexible-alerts
npm i gitbook-plugin-advanced-emoji
npm i gitbook-plugin-ace-mode-feelpp
npm i gitbook-plugin-theme-comscore
  1. 本书配置 book.json
{
  "title": "Gitbook&Honkit 指南",
  "language": "zh-hans",
  "author": "zha0cai",
  "links": {
    "sidebar": {
      "My github": "https://github.com/zha0cai"
    }
  },
  "plugins": [
    "-lunr",
    "-search",
    "search-pro-ling",
    "highlight",
    "code",
    "theme-comscore",
    "-splitter",
    "-summary",
    "anchor-navigation-ex",
    "expandable-chapters-interactive",
    "versions-select-revised",
    "tbfed-pagefooter-fix",
    "flexible-alerts",
    "advanced-emoji",
    "ace",
    "chart",
    "graph",
    "katex"
  ],

  "pluginsConfig": {
    "anchor-navigation-ex": {
      "showLevel": false,
      "showGoTop": true
    },
    "tbfed-pagefooter": {
      "copyright":"Copyright & Copy zha0cai",
      "modify_label": "该文件修订时间:",
      "modify_format": "YYYY-MM-DD HH:mm:ss"
    }, 
    "flexible-alerts": {
      "note": {
        "label": "Hinweis"
      },
      "tip": {
        "label": "Tipp"
      },
      "warning": {
        "label": "Warnung"
      },
      "danger": {
        "label": "Achtung"
      }
    },
    "chart": {
      "type": "c3"
    },
    "sitemap-general": {
      "prefix": "https://zha0cai.github.io/gitbookPublic/home/"
    },
    "versions": {
      "gitbookConfigURL": "book.json",
      "options": [
        {
          "value": "https://zha0cai.github.io/gitbookPublic/gitbook-guide/",
          "text": "Gitbook & Honkit 指南",
          "selected": true
        },
        {
          "value": "https://zha0cai.github.io/gitbookPublic/home/",
          "text": "Home Page",
          "selected": true
        }
      ]
    }
  },
  "structure": {
    "readme": "home.md"
  }
}
  1. 编译与发布【需要写成脚本自动化】
# gitbook serve [书籍路径] [输出路径]
# gitbook build [书籍路径] [输出路径]
# 注意输出目录的文件夹会被清空

\gitbook-guide-book> npx honkit build

# 其他
#生成 PDF 文件
$ gitbook pdf ./ ./mybook.pdf

#生成 ePub 文件
$ gitbook epub ./ ./mybook.epub

#生成 Mobi 文件
$ gitbook mobi ./ ./mybook.mobi
Copyright & Copy zha0cai该文件修订时间: 2024-03-29 14:36:22

results matching ""

    No results matching ""