diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-02 18:40:49 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-02 18:40:53 -0700 |
commit | ecb07468f69fd4a63f074ca0cc1e0e948eeada56 (patch) | |
tree | 2b1aa014d461271915d797e18f7db08e0d6c0c86 /lib/parse/renderer.js | |
parent | 8fc09e2e05701d3832de2940ae45949a107c5ecb (diff) | |
download | gitbook-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/renderer.js')
-rw-r--r-- | lib/parse/renderer.js | 18 |
1 files changed, 16 insertions, 2 deletions
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 + '"'; |