summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/page/index.js20
-rw-r--r--test/page.js16
2 files changed, 23 insertions, 13 deletions
diff --git a/lib/page/index.js b/lib/page/index.js
index f3a8f39..6c63489 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -23,12 +23,13 @@ function Page(book, filename) {
this.book = book;
this.log = this.book.log;
+ // Map of attributes from YAML frontmatter
+ // Description is also extracted by default from content
+ this.attributes = {};
+
// Current content
this.content = '';
- // Short description for the page
- this.description = '';
-
// Relative path to the page
this.path = location.normalize(filename);
@@ -122,16 +123,15 @@ Page.prototype.getContext = function() {
mtime: this.mtime,
type: this.type
},
- page: {
+ page: _.extend({}, this.attributes, {
title: article? article.title : null,
- description: this.description,
next: next? next.getContext() : null,
previous: prev? prev.getContext() : null,
level: article? article.level : null,
depth: article? article.depth : 0,
content: this.content,
dir: dir
- }
+ })
},
gitbook.getContext(),
this.book.getContext(),
@@ -165,8 +165,8 @@ Page.prototype.toHTML = function(output) {
.then(function() {
var parsed = fm(that.content);
- // Extend page with the fontmatter attribute
- that.description = parsed.attributes.description || '';
+ // Extract attributes
+ that.attributes = parsed.attributes;
// Keep only the body
that.update(parsed.body);
@@ -223,8 +223,8 @@ Page.prototype.toHTML = function(output) {
// Extract description from page's content if no frontmatter
onDescription: function(description) {
- if (that.description) return;
- that.description = description;
+ if (that.attributes.description) return;
+ that.attributes.description = description;
},
// Convert glossary entries to annotations
diff --git a/test/page.js b/test/page.js
index ae36660..1dd00ba 100644
--- a/test/page.js
+++ b/test/page.js
@@ -9,7 +9,8 @@ describe('Page', function() {
'README.md': ' # Hello World\n\nThis is a description',
'heading.md': '# Hello\n\n## World',
'description.md': '# This is a title\n\nThis is the short description.\n\nNot this one.',
- 'frontmatter.md': '---\ndescription: Hello World\n---\n\n# This is a title\n\nThis is not the description',
+ 'frontmatter/description.md': '---\ndescription: Hello World\n---\n\n# This is a title\n\nThis is not the description',
+ 'frontmatter/var.md': '---\ntest: Hello World\n---\n\n{{ page.test }}',
'links.md': '[link](hello.md) [link 2](variables/page/next.md) [readme](README.md)',
'links/relative.md': '[link](../hello.md) [link 2](/variables/page/next.md) [readme](../README.md)',
@@ -142,14 +143,23 @@ describe('Page', function() {
});
describe('Font-Matter', function() {
- it('should extratc page description from front matter', function() {
- var page = book.addPage('frontmatter.md');
+ it('should extract page description from front matter', function() {
+ var page = book.addPage('frontmatter/description.md');
return page.toHTML(output)
.then(function() {
page.description.should.equal('Hello World');
});
});
+
+ it('should extend page attributes with custom properties', function() {
+ var page = book.addPage('frontmatter/var.md');
+
+ return page.toHTML(output)
+ .then(function() {
+ page.content.should.equal('<p>Hello World</p>\n');
+ });
+ });
});
describe('Code Blocks', function() {