diff options
Diffstat (limited to 'packages/gitbook/src/models/page.js')
-rw-r--r-- | packages/gitbook/src/models/page.js | 103 |
1 files changed, 51 insertions, 52 deletions
diff --git a/packages/gitbook/src/models/page.js b/packages/gitbook/src/models/page.js index dd60298..e2ab977 100644 --- a/packages/gitbook/src/models/page.js +++ b/packages/gitbook/src/models/page.js @@ -1,70 +1,69 @@ -const Immutable = require('immutable'); +const { Record, Map } = require('immutable'); const yaml = require('js-yaml'); const File = require('./file'); -const Page = Immutable.Record({ - file: File(), - +const DEFAULTS = { + file: File(), // Attributes extracted from the YAML header - attributes: Immutable.Map(), - + attributes: Map(), // Content of the page - content: String(), - + content: String(), // Direction of the text - dir: String('ltr') -}); - -Page.prototype.getFile = function() { - return this.get('file'); -}; - -Page.prototype.getAttributes = function() { - return this.get('attributes'); + dir: String('ltr') }; -Page.prototype.getContent = function() { - return this.get('content'); -}; +class Page extends Record(DEFAULTS) { + getFile() { + return this.get('file'); + } -Page.prototype.getDir = function() { - return this.get('dir'); -}; + getAttributes() { + return this.get('attributes'); + } -/** - * Return page as text - * @return {String} -*/ -Page.prototype.toText = function() { - const attrs = this.getAttributes(); - const content = this.getContent(); + getContent() { + return this.get('content'); + } - if (attrs.size === 0) { - return content; + getDir() { + return this.get('dir'); } - const frontMatter = '---\n' + yaml.safeDump(attrs.toJS(), { skipInvalid: true }) + '---\n\n'; - return (frontMatter + content); -}; + /** + * Return page as text + * @return {String} + */ + toText() { + const attrs = this.getAttributes(); + const content = this.getContent(); -/** - * Return path of the page - * @return {String} -*/ -Page.prototype.getPath = function() { - return this.getFile().getPath(); -}; + if (attrs.size === 0) { + return content; + } -/** - * Create a page for a file - * @param {File} file - * @return {Page} -*/ -Page.createForFile = function(file) { - return new Page({ - file - }); -}; + const frontMatter = '---\n' + yaml.safeDump(attrs.toJS(), { skipInvalid: true }) + '---\n\n'; + return (frontMatter + content); + } + + /** + * Return path of the page + * @return {String} + */ + getPath() { + return this.getFile().getPath(); + } + + /** + * Create a page for a file + * @param {File} file + * @return {Page} + */ + static createForFile(file) { + return new Page({ + file + }); + } +} module.exports = Page; |