mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-17 08:05:19 +00:00
c166a9863e
Using outidr & dir options
95 lines
2.3 KiB
JavaScript
95 lines
2.3 KiB
JavaScript
var url = require('url');
|
|
var inherits = require('util').inherits;
|
|
|
|
var path = require('path');
|
|
|
|
var marked = require('marked');
|
|
|
|
|
|
function GitBookRenderer(options, extra_options) {
|
|
if(!(this instanceof GitBookRenderer)) {
|
|
return new GitBookRenderer(options, extra_options);
|
|
}
|
|
GitBookRenderer.super_.call(this, options);
|
|
|
|
this._extra_options = extra_options;
|
|
}
|
|
inherits(GitBookRenderer, marked.Renderer);
|
|
|
|
GitBookRenderer.prototype._unsanitized = function(href) {
|
|
var prot = '';
|
|
try {
|
|
prot = decodeURIComponent(unescape(href))
|
|
.replace(/[^\w:]/g, '')
|
|
.toLowerCase();
|
|
|
|
} 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);
|
|
|
|
var o = this._extra_options;
|
|
// Relative link, rewrite it to point to github repo
|
|
if(!parsed.protocol && parsed.path[0] != '/' && o && o.repo && o.dir) {
|
|
href = 'https://github.com/' + o.repo + '/blob' + path.normalize(path.join(
|
|
'/',
|
|
o.dir,
|
|
href
|
|
));
|
|
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;
|
|
};
|
|
|
|
GitBookRenderer.prototype.image = function(href, title, text) {
|
|
// Our "fixed" href
|
|
var _href = href;
|
|
|
|
// Parsed version of the url
|
|
var parsed = url.parse(href);
|
|
|
|
// Options
|
|
var o = this._extra_options;
|
|
|
|
// Relative image, rewrite it depending output
|
|
if(!parsed.protocol && parsed.path[0] != '/' && o && o.dir && o.outdir) {
|
|
_href = path.relative(o.outdir, path.normalize(path.join(
|
|
o.dir,
|
|
href
|
|
)));
|
|
}
|
|
|
|
return GitBookRenderer.super_.prototype.image.call(this, _href, title, text);
|
|
};
|
|
|
|
|
|
// Exports
|
|
module.exports = GitBookRenderer;
|