summaryrefslogtreecommitdiffstats
path: root/lib/parse
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-22 17:45:12 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-22 17:45:12 +0200
commit305f2210773c6f6e798d2443a28de95cc8b7bfb2 (patch)
tree632921a4696471b112eda54f6050091e3bbc09f6 /lib/parse
parent53514f2602029d61106e1214790fddf072bf9a81 (diff)
downloadgitbook-305f2210773c6f6e798d2443a28de95cc8b7bfb2.zip
gitbook-305f2210773c6f6e798d2443a28de95cc8b7bfb2.tar.gz
gitbook-305f2210773c6f6e798d2443a28de95cc8b7bfb2.tar.bz2
Fix #136: replace links to .md by links to .html
Add more tests for links in page
Diffstat (limited to 'lib/parse')
-rw-r--r--lib/parse/renderer.js35
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);