summaryrefslogtreecommitdiffstats
path: root/lib/models/page.js
blob: 275a03449c155d1aa5ffb3353f8ea889f2783f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var Immutable = require('immutable');
var yaml = require('js-yaml');

var File = require('./file');

var Page = Immutable.Record({
    file:           File(),

    // Attributes extracted from the YAML header
    attributes:     Immutable.Map(),

    // Content of the page
    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');
};

Page.prototype.getContent = function() {
    return this.get('content');
};

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;
    }

    var frontMatter = '---\n' + yaml.safeDump(attrs.toJS(), { skipInvalid: true }) + '---\n\n';
    return (frontMatter + content);
};

/**
 * Return path of the page
 * @return {String}
*/
Page.prototype.getPath = function() {
    return this.getFile().getPath();
};

/**
 * Create a page for a file
 * @param {File} file
 * @return {Page}
*/
Page.createForFile = function(file) {
    return new Page({
        file: file
    });
};

module.exports = Page;