Add method toText to Page

This commit is contained in:
Samy Pessé
2016-06-07 12:51:35 +02:00
parent 22da5aac11
commit b566711ee1
2 changed files with 44 additions and 0 deletions
+28
View File
@@ -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');
});
});
});
+16
View File
@@ -1,4 +1,5 @@
var Immutable = require('immutable');
var matter = require('gray-matter');
var File = require('./file');
@@ -31,6 +32,21 @@ Page.prototype.getDir = function() {
return this.get('dir');
};
/**
* 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}