mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add method toText to Page
This commit is contained in:
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user