mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Add target _blank support in renderer for external links
This commit is contained in:
+39
-8
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user