Skip to content

Instantly share code, notes, and snippets.

@budiadiono
Last active January 25, 2019 20:09
Show Gist options
  • Save budiadiono/dd409e2168c801c9e7429766ee0101c3 to your computer and use it in GitHub Desktop.
Save budiadiono/dd409e2168c801c9e7429766ee0101c3 to your computer and use it in GitHub Desktop.
Gitbook serve with gulpjs (while wait https://github.com/GitbookIO/gitbook/issues/1379 resolved)
var browserSync = require('browser-sync').create()
var reload = browserSync.reload
var gulp = require('gulp')
var gitbook = require('gitbook')
var path = require('path')
var del = require('del')
// path of your *.md book files
var rootPath = path.join(__dirname, 'docs')
// output path where generated *.html will be stored
var outputPath = path.join(rootPath, '_book')
// gulp.watch instance
var watcher
// rebuild book + start server
gulp.task('default', function (done) {
rebuildBook(function () {
browserSync.init({
server: {
baseDir: outputPath
}
})
done()
})
})
// kill watcher + remove/clean output dir + build book
function rebuildBook (done) {
// pause watcher while building book progress
if (watcher) {
watcher.end()
watcher = undefined;
}
// ensure output path is not exists otherwise book won't generated
return del(path.join(outputPath)).then(paths => {
buildBook().then(function () {
// start watcher after built
startWatch().on('ready', function(){
done();
}).on('error', function(e){
console.error('ERROR: watch error... $s', e)
})
})
})
}
// build book
function buildBook () {
var fs = gitbook.createNodeFS(rootPath)
var book = gitbook.Book.createForFS(fs)
var Parse = gitbook.Parse
var Output = gitbook.Output
var Generator = Output.getGenerator('website')
return Parse.parseBook(book)
.then(function (resultBook) {
return Output.generate(Generator, resultBook, {
root: outputPath
})
})
}
// start watching md files + when files changed reload browser
function startWatch() {
var rp = path.join(rootPath, '/**/*.md')
watcher = gulp.watch([rp, '!' + rp + '/_book/' ], function () {
rebuildBook(reload)
})
return watcher;
}
@jtbsk
Copy link

jtbsk commented Nov 19, 2016

Thanks, it solved my problem, but now the entire page is refreshed, If you can achieve a single page local refresh, i think it's great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment