Resolve relative links to GitHub repo, fixes #6

Useful for linking to blobs and other files …
This commit is contained in:
Aaron O'Mullan
2014-04-02 18:40:49 -07:00
parent 8fc09e2e05
commit ecb07468f6
3 changed files with 27 additions and 7 deletions
+4 -1
View File
@@ -38,7 +38,10 @@ var initTemplate = function(options) {
// Parse sections
.then(function(markdown) {
return parse.page(markdown);
return parse.page(markdown, {
repo: options.locals.githubId,
dir: path.dirname(_input) || '/'
});
})
//Calcul template
+7 -4
View File
@@ -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();
+16 -2
View File
@@ -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 + '"';