summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-03-31 12:16:16 -0700
committerSamy Pessé <samypesse@gmail.com>2014-03-31 12:16:16 -0700
commit77a4c1042c7c13327641fb057c3c203cbf868782 (patch)
treeacffe873bdf71e8dc89e973bc41ef5a30be63533
parent6df59745b1e32535bc529f97d3c752afac50095d (diff)
parent89593f46ab0c7dddab8414c5da55f0faeb3e61a4 (diff)
downloadgitbook-77a4c1042c7c13327641fb057c3c203cbf868782.zip
gitbook-77a4c1042c7c13327641fb057c3c203cbf868782.tar.gz
gitbook-77a4c1042c7c13327641fb057c3c203cbf868782.tar.bz2
Merge branch 'master' of https://github.com/GitbookIO/gitbook
-rw-r--r--lib/parse/page.js9
-rw-r--r--lib/parse/renderer.js27
-rw-r--r--lib/parse/summary.js7
-rw-r--r--test/fixtures/PAGE.MD2
-rw-r--r--test/summary.js9
5 files changed, 52 insertions, 2 deletions
diff --git a/lib/parse/page.js b/lib/parse/page.js
index 047f3e4..b1e4332 100644
--- a/lib/parse/page.js
+++ b/lib/parse/page.js
@@ -1,6 +1,8 @@
var _ = require('lodash');
var marked = require('marked');
+var renderer = require('./renderer');
+
// Split a page up into sections (lesson, exercises, ...)
function splitSections(nodes) {
@@ -49,10 +51,15 @@ function parsePage(src) {
// 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()
+ });
+
// Render normal pages
return {
type: section.type,
- content: marked.parser(section)
+ content: marked.parser(section, options)
};
})
.value();
diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js
new file mode 100644
index 0000000..66aad46
--- /dev/null
+++ b/lib/parse/renderer.js
@@ -0,0 +1,27 @@
+var inherits = require('util').inherits;
+
+var marked = require('marked');
+
+
+function GitBookRenderer(options) {
+ if(!(this instanceof GitBookRenderer)) {
+ return new GitBookRenderer(options);
+ }
+ GitBookRenderer.super_.call(this, options);
+}
+inherits(GitBookRenderer, marked.Renderer);
+
+
+GitBookRenderer.prototype.link = function(href, title, text) {
+ // Replace .md extensions by .html
+ return GitBookRenderer.super_.prototype.link.call(
+ this,
+ href.replace(/\.md$/, '.html'),
+ title,
+ text
+ );
+};
+
+
+// Exports
+module.exports = GitBookRenderer;
diff --git a/lib/parse/summary.js b/lib/parse/summary.js
index 8787554..f021401 100644
--- a/lib/parse/summary.js
+++ b/lib/parse/summary.js
@@ -76,7 +76,12 @@ function parseTitle(src) {
return {
title: matches[1],
- path: matches[2],
+
+ // Replace .md references with .html
+ path: matches[2].replace(/\.md$/, '.html'),
+
+ // Original, non normalized path
+ _path: matches[2],
};
}
diff --git a/test/fixtures/PAGE.MD b/test/fixtures/PAGE.MD
index 79fc10c..2839e6d 100644
--- a/test/fixtures/PAGE.MD
+++ b/test/fixtures/PAGE.MD
@@ -28,3 +28,5 @@ assert(c, 3)
Some more nice content ....
[Cool stuff](http://gitbook.io)
+
+[Link to another Markdown file](./xyz/file.md)
diff --git a/test/summary.js b/test/summary.js
index 2a1af96..5439182 100644
--- a/test/summary.js
+++ b/test/summary.js
@@ -31,4 +31,13 @@ describe('Summary parsing', function () {
assert(LEXED.chapters[1].chapter.title);
assert(LEXED.chapters[2].chapter.title);
});
+
+ it('should normalize paths from .md to .html', function() {
+ assert.equal(LEXED.chapters[0].chapter.path,'chapter-1/README.html');
+ assert.equal(LEXED.chapters[0].chapter._path,'chapter-1/README.md');
+ assert.equal(LEXED.chapters[1].chapter.path,'chapter-2/README.html');
+ assert.equal(LEXED.chapters[1].chapter._path,'chapter-2/README.md');
+ assert.equal(LEXED.chapters[2].chapter.path,'chapter-3/README.html');
+ assert.equal(LEXED.chapters[2].chapter._path,'chapter-3/README.md');
+ });
});