summaryrefslogtreecommitdiffstats
path: root/lib/models
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-06-07 12:51:35 +0200
committerSamy Pessé <samypesse@gmail.com>2016-06-07 12:51:35 +0200
commitb566711ee1caedbafa1d613b05f011e781a78bd2 (patch)
tree697807fb21ad0ecbd8db2c19a0329e9cb882010f /lib/models
parent22da5aac118e58700e19cef7466494cd82bba34a (diff)
downloadgitbook-b566711ee1caedbafa1d613b05f011e781a78bd2.zip
gitbook-b566711ee1caedbafa1d613b05f011e781a78bd2.tar.gz
gitbook-b566711ee1caedbafa1d613b05f011e781a78bd2.tar.bz2
Add method toText to Page
Diffstat (limited to 'lib/models')
-rw-r--r--lib/models/__tests__/page.js28
-rw-r--r--lib/models/page.js16
2 files changed, 44 insertions, 0 deletions
diff --git a/lib/models/__tests__/page.js b/lib/models/__tests__/page.js
new file mode 100644
index 0000000..15a13ad
--- /dev/null
+++ b/lib/models/__tests__/page.js
@@ -0,0 +1,28 @@
+var Immutable = require('immutable');
+var Page = require('../page');
+
+describe('Page', function() {
+
+ describe('toText', function() {
+ it('must not prepend frontmatter if no attributes', function() {
+ var page = Page().merge({
+ content: 'Hello World'
+ });
+
+ expect(page.toText()).toBe('Hello World');
+ });
+
+ it('must prepend frontmatter if attributes', function() {
+ var page = Page().merge({
+ content: 'Hello World',
+ attributes: Immutable.fromJS({
+ hello: 'world'
+ })
+ });
+
+ expect(page.toText()).toBe('---\nhello: world\n---\nHello World\n');
+ });
+ });
+});
+
+
diff --git a/lib/models/page.js b/lib/models/page.js
index 1b0e9f8..3f54f43 100644
--- a/lib/models/page.js
+++ b/lib/models/page.js
@@ -1,4 +1,5 @@
var Immutable = require('immutable');
+var matter = require('gray-matter');
var File = require('./file');
@@ -32,6 +33,21 @@ Page.prototype.getDir = function() {
};
/**
+ * Return page as text
+ * @return {String}
+*/
+Page.prototype.toText = function() {
+ var attrs = this.getAttributes();
+ var content = this.getContent();
+
+ if (attrs.size === 0) {
+ return content;
+ }
+
+ return matter.stringify(content, attrs.toJS());
+};
+
+/**
* Return path of the page
* @return {String}
*/