mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 19:06:31 +00:00
Resolve relative links to GitHub repo, fixes #6
Useful for linking to blobs and other files …
This commit is contained in:
@@ -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
@@ -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
@@ -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 + '"';
|
||||
|
||||
Reference in New Issue
Block a user