diff options
Diffstat (limited to 'lib/parse')
-rw-r--r-- | lib/parse/navigation.js | 75 | ||||
-rw-r--r-- | lib/parse/page.js | 27 | ||||
-rw-r--r-- | lib/parse/renderer.js | 10 |
3 files changed, 51 insertions, 61 deletions
diff --git a/lib/parse/navigation.js b/lib/parse/navigation.js index bba25ce..2f8669e 100644 --- a/lib/parse/navigation.js +++ b/lib/parse/navigation.js @@ -6,6 +6,12 @@ function clean(obj) { return obj && _.omit(obj, ['articles']); } +function flattenChapters(chapters) { + return _.reduce(chapters, function(accu, chapter) { + return accu.concat([clean(chapter)].concat(chapter.articles)); + }, []); +} + // Returns from a summary a map of /* { @@ -20,61 +26,36 @@ function navigation(summary, files) { // Support single files as well as list files = _.isArray(files) ? files : (_.isString(files) ? [files] : null); - // Mapping of prev/next for a give path - var mapping = {}; - // Special README nav var README_NAV = { path: 'README.md', title: 'Introduction', + level: '0', }; - // Walk the chapter/article tree and create navigation entires - _.each(summary.chapters, function(chapter, idx, chapters) { - // Skip if no path - if(!chapter.path) return; - - var currentChapter = clean(chapter); - var prevChapter = (idx-1 < 0) ? README_NAV : chapters[idx-1]; - var nextChapter = (idx+1 >= chapters.length) ? null : chapters[idx+1]; - - var prev = (!prevChapter || _.isEmpty(prevChapter.articles)) ? - prevChapter : _.last(prevChapter.articles); - var next = (!chapter || _.isEmpty(chapter.articles)) ? - nextChapter : _.first(chapter.articles); + // List of all navNodes + var navNodes = [README_NAV].concat(flattenChapters(summary.chapters)); + var prevNodes = [null].concat(navNodes.slice(0, -1)); + var nextNodes = navNodes.slice(1).concat([null]); - // Add chapter mapping - mapping[chapter.path] = { - title: chapter.title, - prev: clean(prev), - next: clean(next), - level: (idx+1).toString(), - }; - - // Check a chapter's articles - _.each(chapter.articles, function(article, _idx, articles) { - // Skip if no path - if(!article.path) return; - - var prev = (_idx-1 < 0) ? currentChapter : clean(articles[_idx-1]); - var next = (_idx+1 >= articles.length) ? nextChapter : clean(articles[_idx+1]); - - mapping[article.path] = { - title: article.title, - prev: clean(prev), - next: clean(next), - level: [idx+1, _idx+1].join('.'), - }; - }); - }); + // Mapping of prev/next for a give path + var mapping = _.chain(_.zip(navNodes, prevNodes, nextNodes)) + .map(function(nodes) { + var current = nodes[0], prev = nodes[1], next = nodes[2]; - // Hack for README.html - mapping['README.md'] = { - title: README_NAV.title, - prev: null, - next: clean(summary.chapters[0]), - level: '0', - }; + // Skip if no path + if(!current.path) return null; + + return [current.path, { + title: current.title, + prev: prev, + next: next, + level: current.level, + }]; + }) + .filter() + .object() + .value(); // Filter for only files we want if(files) { diff --git a/lib/parse/page.js b/lib/parse/page.js index eb118e4..6cfd3ca 100644 --- a/lib/parse/page.js +++ b/lib/parse/page.js @@ -8,17 +8,6 @@ var renderer = require('./renderer'); var lnormalize = require('../utils/lang').normalize; -// Synchronous highlighting with highlight.js -marked.setOptions({ - highlight: function (code, lang) { - try { - return hljs.highlight(lang, code).value; - } catch(e) { - return hljs.highlightAuto(code).value; - } - } -}); - // Render a section using our custom renderer function render(section, _options) { @@ -30,7 +19,21 @@ function render(section, _options) { // Build options using defaults and our custom renderer var options = _.extend({}, marked.defaults, { - renderer: renderer(null, _options) + renderer: renderer(null, _options), + + // Synchronous highlighting with highlight.js + highlight: function (code, lang) { + if(!lang) return code; + + // Normalize lang + lang = lnormalize(lang); + + try { + return hljs.highlight(lang, code).value; + } catch(e) { } + + return code; + } }); return marked.parser(section, options); diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index 2a72d48..949a9ee 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -79,8 +79,14 @@ GitBookRenderer.prototype.image = function(href, title, text) { // Relative image, rewrite it depending output if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.dir && o.outdir) { - var outdir = o.outdir.charAt(o.outdir.length - 1) === '/' ? o.outdir : o.outdir + '/'; - _href = url.resolve(outdir, [o.dir, href].join('/')); + // o.dir: directory parent of the file currently in rendering process + // o.outdir: directory parent from the html output + + // Absolute file in source + _href = path.join(o.dir, _href); + + // make it relative to output + _href = path.relative(o.outdir, _href); } return GitBookRenderer.super_.prototype.image.call(this, _href, title, text); |