1. 相对路径改绝对路径

修改文件:${gitbook.home}\lib\output\website\createTemplateEngine.js

1
2
3
4
5
6
7
8
9
10
11
12
resolveAsset: function(filePath) {
filePath = LocationUtils.toAbsolute(filePath, '', '');
filePath = path.join('/gitbook', filePath);
//filePath = LocationUtils.relativeForFile(currentFile, filePath);
// Use assets from parent if language book
//if (book.isLanguageBook()) {
// filePath = path.join('../', filePath);
//}
return LocationUtils.normalize(filePath);
},

2. 重用 _book 目录

不使用临时目录,加快生成pdf等ebook格式时编译速度

修改文件:${gitbook.home}\lib\cli\buildEbook.js

第一处,修改目录

1
2
3
// Create temporary directory
var book = getBook(args, kwargs);
var outputFolder = book.fs.root ? book.fs.root + '/_book' : tmp.dirSync().name;

第二处,不删除 _book 目录

1
2
3
4
5
6
7
// Log end
.then(function(count) {
logger.info.ok(count + ' file(s) generated');
//logger.debug('cleaning up... ');
//return logger.debug.promise(fs.rmDir(outputFolder));
});

3. toc 改进

修改ebook格式下,不输出每一页的h1标签;website格式下,将h1的id修改为当前level,配合gtoc生成全局的索引

修改文件:${gitbook.home}lib\output\website\onPage.js

第一处,引入 cheerio

1
2
var fileToOutput = require('../helper/fileToOutput');
var cheerio = require('cheerio');

第二处,改写页面内容

$ = cheerio.load(context.page.content);
context.page.title = context.page.level + '  ' + context.page.title;
if (options.prefix == 'ebook') {
    $('h1').remove();
} else {
    $('h1').attr('id', context.page.level);
    $('h1').text(context.page.title);
}
context.page.content = $.html();

// Render the theme
return Templating.renderFile(engine, prefix + '/page.html', context)