GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书,GitBook 并非关于 Git 的教程。

服务安装

Node.js

yum install -y gcc gcc-c++ make
cd /opt
wget https://nodejs.org/dist/v6.11.0/node-v6.11.0.tar.gz

tar xvf node-v6.11.0.tar.gz
cd node-v6.11.0
./configure
make
make install
cp /usr/local/bin/node /usr/sbin/

查看当前安装的Node版本
node -v

查看当前安装npm版本    ---类似于Linux下面的yum
npm -v

设置npm安装的路径
npm config set prefix /opt/node

安装成功找不到命令
echo -e "export PATH=$(npm prefix -g)/bin:$PATH" >> ~/.bashrc && source ~/.bashrc

Gitbook

# 安装客服端服务
node install -g gitbook-cli
# 初始化服务
mkdir /opt/gitbook
gitbook init
# 安装服务
gitbook install 
# 启动服务 
gitbook serve .
# 构建服务  生成静态文件
gitbook build

核心介绍

book.json

参考book.json

{
    "title": "Development Notes",
    "description": "Development 的心得与纪要",
    "author": "Luis",
    "output.name": "site",
    "language": "zh-hans",
    "gitbook": "3.2.3",
    "root": ".",
    "structure": {
        "readme": "README.md"
    },
    "links": {
        "sidebar": {
            "Home": "http://www.wswzms.top"
        }
    },
    "plugins": [
        "-lunr",
        "-search",
        "-highlight",
        "-livereload",
        "search-plus@^0.0.11",
        "simple-page-toc@^0.1.1",
        "github@^2.0.0",
        "github-buttons@2.1.0",
        "edit-link@^2.0.2",
        "disqus@^0.1.0",
        "prism@^2.1.0",
        "prism-themes@^0.0.2",
        "advanced-emoji@^0.2.1",
        "anchors@^0.7.1",
        "include-codeblock@^3.0.2",
        "ace@^0.3.2",
        "emphasize@^1.1.0",
        "katex@^1.1.3",
        "splitter@^0.0.8",
        "mermaid-gb3@2.1.0",
        "tbfed-pagefooter@^0.0.1",
        "expandable-chapters-small@^0.1.7",
        "sectionx@^3.1.0",
        "donate@^1.0.2",
        "local-video@^1.0.1",
        "sitemap-general@^0.1.1",
        "anchor-navigation-ex@0.1.8",
        "favicon@^0.0.2",
        "todo@^0.1.3",
        "3-ba@^0.9.0",
        "terminal@^0.3.2",
        "alerts@^0.2.0",
        "include-csv@^0.1.0",
        "puml@^1.0.1",
        "musicxml@^1.0.2",
        "klipse@^1.2.0",
        "versions-select@^0.1.1",
        "rss@^3.0.2",
        "-sharing",
        "sharing-plus@^0.0.2",
        "graph@^0.1.0",
        "chart@^0.2.0"
    ],

    "pluginsConfig": {
        "theme-default": {
            "showLevel": true
        },
        "disqus": {
            "shortName": "gitbookuse"
        },
        "prism": {
            "css": [
                "prism-themes/themes/prism-base16-ateliersulphurpool.light.css"
            ]
        },
        "github": {
            "url": "https://github.com/NightAlexLy/gitbook-use"
        },
        "github-buttons": {
            "repo": "NightAlexLy/gitbook-use",
            "types": [
                "star"
            ],
            "size": "small"
        },
        "include-codeblock": {
            "template": "ace",
            "unindent": true,
            "edit": true
        },
        "sharing": {
           "douban": false,
           "facebook": false,
           "google": true,
           "hatenaBookmark": false,
           "instapaper": false,
           "line": false,
           "linkedin": true,
           "messenger": false,
           "pocket": false,
           "qq": false,
           "qzone": false,
           "stumbleupon": false,
           "twitter": false,
           "viber": false,
           "vk": false,
           "weibo": true,
           "whatsapp": false,
           "all": [
               "facebook", "google", "twitter",
               "weibo", "instapaper", "linkedin",
               "pocket", "stumbleupon", "qq", "qzone"
           ]
       },
        "tbfed-pagefooter": {
            "copyright": "Copyright ? wswzms.top 2019",
            "modify_label": "该文件修订时间:",
            "modify_format": "YYYY-MM-DD HH:mm:ss"
        },
        "3-ba": {
            "token": "ff100361cdce95dd4c8fb96b4009f7bc"
        },
        "donate": {
            "wechat": "http://cdn.wswzms.top/wechatpay.jpg",
            "alipay": "http://cdn.wswzms.top/alipay.jpg",
            "title": "",
            "button": "赏",
            "alipayText": "支付宝打赏",
            "wechatText": "微信打赏"
        },
        "simple-page-toc": {
            "maxDepth": 3,
            "skipFirstH1": true
        },
        "edit-link": {
            "base": "https://github.com/NightAlexLy/gitbook-use/edit/master",
            "label": "Edit This Page"
        },
        "sitemap-general": {
            "prefix": "http://gitbook.wswzms.top"
        },
        "anchor-navigation-ex": {
            "isRewritePageTitle": false,
            "tocLevel1Icon": "fa fa-hand-o-right",
            "tocLevel2Icon": "fa fa-hand-o-right",
            "tocLevel3Icon": "fa fa-hand-o-right"
        },
        "sectionx": {
            "tag": "b"
        },
        "favicon": {
            "shortcut": "favicon.ico",
            "bookmark": "favicon.ico"
        },
        "terminal": {
            "copyButtons": true,
            "fade": false,
            "style": "flat"
        },
        "rss": {
            "title": "GitBook 使用教程",
            "description": "记录 GitBook 的配置和一些插件的使用",
            "author": "Jikai Zhang",
            "feed_url": "http://gitbook.wswzms.top/rss",
            "site_url": "http://gitbook.wswzms.top/",
            "managingEditor": "me@wswzms.top",
            "webMaster": "me@wswzms.top",
            "categories": [
                "gitbook"
            ]
        }
    }
}

Gitbook Command

[root@localhost gitbook]# gitbook --help

  Usage: gitbook [options] [command]


  Options:

    -v, --gitbook [version]  specify GitBook version to use
    -d, --debug              enable verbose error
    -V, --version            Display running versions of gitbook and gitbook-cli
    -h, --help               output usage information


  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

资料

扩展

   不建议采用gitbook serve启动服务,虽然支持动态加载,但是修改编译出现异常时,服务会自动宕掉。 dfg

   部署思路:

  1. 添加文章(md)文件通过git/svn托管
  2. 通过gitbook build生成静态文件
  3. 在通过gulp对生成的静态文件进行压缩
  4. nginx对压缩后的文件进行反向代理(采用的是域名映射目录的方式)
  5. 后期将上面的2~4步骤,通过脚本实现自动更新

SVN托管

svn建立一个文章托管目录

# 服务目录添加一个gitbook目录

# checkout
svn checkout svn://localhost/gitbook

# 文件进行托管
svn add book.js
svn add *.md
svn add content
svn commit -m "init" book.js
svn commit -m "init" *.md
svn commit -m "init" content

gulp

 Gulp的作用是将gitbook build后的代码进行压缩,减少网络带宽。   根目录建立一个package.json文件,定义为node.js项目。

package.json

{
  "name": "gitbook",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
    "gulp": "^3.9.1",
    "gulp-clean": "^0.4.0",
    "gulp-htmlmin": "^4.0.0",
    "gulp-load-plugins": "^1.5.0",
    "gulp-minify-css": "^1.2.4",
    "gulp-minify-html": "^1.0.6",
    "gulp-rename": "^1.2.2",
    "gulp-shell": "^0.6.5",
    "gulp-typescript": "^3.1.7",
    "gulp-uglify": "^2.0.0",
    "gulp-util": "^3.0.8",
    "typescript": "^2.3.4"
  }
}

定义完成,执行npm install,会将项目的依赖下载,等命令执行加载至运行环境中。

编写gulpfile.js脚本。

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var ts = require('gulp-typescript');
var htmlmin = require('gulp-htmlmin');
var gutil = require('gulp-util');

gulp.task("clean",function() {
    return gulp.src("./dst/*")
    .pipe(plugins.clean());
});

gulp.task("css",["clean"],function(){
    var stream = gulp.src(["_book/**/*.css","!_book/**/*.min.css"])
        .pipe(plugins.minifyCss({compatibility: 'ie8'}))
        .pipe(gulp.dest("./dst/"));
    return stream;
});

gulp.task("js",["clean"],function(){
    var stream = gulp.src(["_book/**/*.js","!_book/**/*.min.js"])
         .pipe(ts({
           target: "es5",
           allowJs: true,
           module: "commonjs",
           moduleResolution: "node"
         }))
         .pipe(plugins.uglify())
         .on('error', function (err) {
                gutil.log(gutil.colors.red('[Error]'), err.toString());
            })
        .pipe(gulp.dest("./dst/"));
    return stream;
});

gulp.task("html",["clean"],function(){
    var stream = gulp.src("_book/**/*.html")
        //.pipe(plugins.minifyHtml({ quotes: false,loose: false }))
        //.pipe(plugins.rename({suffix: ".gulp"}))
        .pipe(htmlmin({collapseWhitespace: true}))
        .pipe(gulp.dest("./dst/"));
    return stream;
});

gulp.task("mv",["html"],function() {
    var stream = gulp.src("./dst/*")
        /*.pipe(gulp.dest("./_book/"));*/
        .pipe(plugins.shell([
            "\cp -rf ./dst/* ./_book/"
        ]));
    return stream;
});

gulp.task("watch",function() {
    gulp.watch("_book/*",["optimise"]);
});

gulp.task("default",["mv"],function(){
    console.log("gulp task ok!");
});

执行gulp命令既可以对gitbook生成的静态文件进行压缩。

脚本自动通过

#!/bin/bash

# 加载最新的SVN目录
svn update
# 生成静态文件
gitbook build
# 压缩静态文件
gulp

# nginx通过域名代理目录文件,当有文件变动时,会自动更新。不需要重启服务

Nginx配置

 域名映射目录

   server {
     listen 80;
           server_name gitbook.wswzms.top;
           error_page 404 403 500 502 503 504 /404.html;

           location / {
           # 反向代理到 4000 端口
           #proxy_pass http://127.0.0.1:4000;
           #add_header Access-Control-Allow-Origin *;
           root /opt/gitbook/_book;
           index index.html index.htm;
           }
   }

前置条件:

  1. 需求添加一条二级域名解析,指向云服务器
Copyright © wswzms.top 2019 all right reserved,powered by Gitbook该文件修订时间: 2019-04-19 14:56:06

results matching ""

    No results matching ""