diff options
Diffstat (limited to 'lib/parse/renderer.js')
-rw-r--r-- | lib/parse/renderer.js | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index 949a9ee..625a7ce 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -1,5 +1,7 @@ var url = require('url'); var inherits = require('util').inherits; +var links = require('../utils').links; + var path = require('path'); @@ -38,23 +40,38 @@ GitBookRenderer.prototype._unsanitized = function(href) { }; GitBookRenderer.prototype.link = function(href, title, text) { + // Our "fixed" href + var _href = href; + // Don't build if it looks malicious if (this.options.sanitize && this._unsanitized(href)) { - return ''; + return text; } // Parsed version of the url var parsed = url.parse(href); var o = this._extra_options; + // Relative link, rewrite it to point to github repo - if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.repo && o.dir) { - href = url.resolve('https://github.com/' + o.repo + '/blob/', [o.dir, href].join("/")); - parsed = url.parse(href); + if(links.isRelative(_href)) { + if (path.extname(_href) == ".md") { + _href = links.toAbsolute(_href, o.dir || "./", o.outdir || "./"); + + if (o.singleFile) { + _href = "#"+_href; + } else { + _href = _href.replace(".md", ".html"); + } + } else if (o && o.repo && o.dir) { + href = url.resolve('https://github.com/' + o.repo + '/blob/', [o.dir, _href].join("/")); + parsed = url.parse(href); + _href = parsed.href; + } } // Generate HTML for link - var out = '<a href="' + parsed.href + '"'; + var out = '<a href="' + _href + '"'; // Title if no null if (title) { out += ' title="' + title + '"'; @@ -78,15 +95,11 @@ GitBookRenderer.prototype.image = function(href, title, text) { var o = this._extra_options; // Relative image, rewrite it depending output - if(!parsed.protocol && parsed.path && parsed.path[0] != '/' && o && o.dir && o.outdir) { + if(links.isRelative(href) && o && o.dir && o.outdir) { // 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); + _href = links.toAbsolute(_href, o.dir, o.outdir); } return GitBookRenderer.super_.prototype.image.call(this, _href, title, text); |