diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-03-31 18:15:05 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-03-31 18:15:05 -0700 |
commit | 138dc1f2dea9d32241ee5e8f2c655613cdc168bb (patch) | |
tree | 159fd2ab3e066a699071d176bfb54d72266f1988 /lib/parse/renderer.js | |
parent | 1ab31ede81ca98150445b81b0693c533ca224199 (diff) | |
download | gitbook-138dc1f2dea9d32241ee5e8f2c655613cdc168bb.zip gitbook-138dc1f2dea9d32241ee5e8f2c655613cdc168bb.tar.gz gitbook-138dc1f2dea9d32241ee5e8f2c655613cdc168bb.tar.bz2 |
Add target _blank support in renderer for external links
Diffstat (limited to 'lib/parse/renderer.js')
-rw-r--r-- | lib/parse/renderer.js | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index 66aad46..4b5c945 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -1,3 +1,4 @@ +var url = require('url'); var inherits = require('util').inherits; var marked = require('marked'); @@ -11,17 +12,47 @@ function GitBookRenderer(options) { } inherits(GitBookRenderer, marked.Renderer); +GitBookRenderer.prototype._unsanitized = function(href) { + var prot = ''; + try { + prot = decodeURIComponent(unescape(href)) + .replace(/[^\w:]/g, '') + .toLowerCase(); -GitBookRenderer.prototype.link = function(href, title, text) { - // Replace .md extensions by .html - return GitBookRenderer.super_.prototype.link.call( - this, - href.replace(/\.md$/, '.html'), - title, - text - ); + } catch (e) { + return true; + } + + if(prot.indexOf('javascript:') === 0) { + return true; + } + + return false; }; +GitBookRenderer.prototype.link = function(href, title, text) { + // Don't build if it looks malicious + if (this.options.sanitize && this._unsanitized(href)) { + return ''; + } + + // Parsed version of the url + var parsed = url.parse(href); + + + // Generate HTML for link + var out = '<a href="' + href + '"'; + // Title if no null + if (title) { + out += ' title="' + title + '"'; + } + // Target blank if external + if(parsed.protocol) { + out += ' target="_blank"'; + } + out += '>' + text + '</a>'; + return out; +}; // Exports module.exports = GitBookRenderer; |