summaryrefslogtreecommitdiffstats
path: root/lib
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
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')
-rw-r--r--lib/generate/page/index.js11
-rw-r--r--lib/parse/renderer.js35
-rw-r--r--lib/utils/index.js1
-rw-r--r--lib/utils/links.js29
4 files changed, 62 insertions, 14 deletions
diff --git a/lib/generate/page/index.js b/lib/generate/page/index.js
index 3ee368f..c2f4484 100644
--- a/lib/generate/page/index.js
+++ b/lib/generate/page/index.js
@@ -25,14 +25,18 @@ swig.setFilter('code', function(code, lang) {
var Generator = function() {
BaseGenerator.apply(this, arguments);
- // Load base template
- this.template = swig.compileFile(path.resolve(this.options.theme, 'templates/page.html'));
-
// List of pages content
this.pages = {};
};
util.inherits(Generator, BaseGenerator);
+// Load all templates
+Generator.prototype.loadTemplates = function() {
+ this.template = swig.compileFile(
+ this.plugins.template("page") || path.resolve(this.options.theme, 'templates/page.html')
+ );
+};
+
Generator.prototype.convertFile = function(content, input) {
var that = this;
var json = {
@@ -46,6 +50,7 @@ Generator.prototype.convertFile = function(content, input) {
repo: that.options.githubId,
dir: path.dirname(input) || '/',
outdir: './',
+ singleFile: true
});
})
.then(function(sections) {
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);
diff --git a/lib/utils/index.js b/lib/utils/index.js
index 155e723..dbc4087 100644
--- a/lib/utils/index.js
+++ b/lib/utils/index.js
@@ -1,3 +1,4 @@
module.exports = {
lang: require('./lang'),
+ links: require('./links')
};
diff --git a/lib/utils/links.js b/lib/utils/links.js
new file mode 100644
index 0000000..2501044
--- /dev/null
+++ b/lib/utils/links.js
@@ -0,0 +1,29 @@
+var url = require('url');
+var path = require('path');
+
+// Return true if the link is relative
+var isRelative = function(href) {
+ var parsed = url.parse(href);
+
+ return !parsed.protocol && parsed.path && parsed.path[0] != '/';
+};
+
+// Relative to absolute path
+// dir: directory parent of the file currently in rendering process
+// outdir: directory parent from the html output
+
+var toAbsolute = function(_href, dir, outdir) {
+ // Absolute file in source
+ _href = path.join(dir, _href);
+
+ // make it relative to output
+ _href = path.relative(outdir, _href);
+
+ return _href;
+};
+
+
+module.exports = {
+ isRelative: isRelative,
+ toAbsolute: toAbsolute
+}; \ No newline at end of file