summaryrefslogtreecommitdiffstats
path: root/lib/parse
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@friendco.de>2014-04-02 18:40:49 -0700
committerAaron O'Mullan <aaron.omullan@friendco.de>2014-04-02 18:40:53 -0700
commitecb07468f69fd4a63f074ca0cc1e0e948eeada56 (patch)
tree2b1aa014d461271915d797e18f7db08e0d6c0c86 /lib/parse
parent8fc09e2e05701d3832de2940ae45949a107c5ecb (diff)
downloadgitbook-ecb07468f69fd4a63f074ca0cc1e0e948eeada56.zip
gitbook-ecb07468f69fd4a63f074ca0cc1e0e948eeada56.tar.gz
gitbook-ecb07468f69fd4a63f074ca0cc1e0e948eeada56.tar.bz2
Resolve relative links to GitHub repo, fixes #6
Useful for linking to blobs and other files …
Diffstat (limited to 'lib/parse')
-rw-r--r--lib/parse/page.js11
-rw-r--r--lib/parse/renderer.js18
2 files changed, 23 insertions, 6 deletions
diff --git a/lib/parse/page.js b/lib/parse/page.js
index 505d70f..937d84e 100644
--- a/lib/parse/page.js
+++ b/lib/parse/page.js
@@ -46,19 +46,22 @@ function sectionType(nodes, idx) {
}
// Render a section using our custom renderer
-function render(section) {
+function render(section, _options) {
// marked's Render expects this, we don't use it yet
section.links = {};
// Build options using defaults and our custom renderer
var options = _.extend({}, marked.defaults, {
- renderer: renderer()
+ renderer: renderer(null, _options)
});
return marked.parser(section, options);
}
-function parsePage(src) {
+function parsePage(src, options) {
+ options = options || {};
+
+ // Lex file
var nodes = marked.lexer(src);
return _.chain(splitSections(nodes))
@@ -113,7 +116,7 @@ function parsePage(src) {
return {
id: id,
type: section.type,
- content: render(section)
+ content: render(section, options)
};
})
.value();
diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js
index 4b5c945..64c5602 100644
--- a/lib/parse/renderer.js
+++ b/lib/parse/renderer.js
@@ -1,14 +1,18 @@
var url = require('url');
var inherits = require('util').inherits;
+var path = require('path');
+
var marked = require('marked');
-function GitBookRenderer(options) {
+function GitBookRenderer(options, extra_options) {
if(!(this instanceof GitBookRenderer)) {
- return new GitBookRenderer(options);
+ return new GitBookRenderer(options, extra_options);
}
GitBookRenderer.super_.call(this, options);
+
+ this._extra_options = extra_options;
}
inherits(GitBookRenderer, marked.Renderer);
@@ -39,6 +43,16 @@ GitBookRenderer.prototype.link = function(href, title, 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.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 + '"';