92 lines
2.4 KiB
JavaScript
92 lines
2.4 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const immutable_1 = __importDefault(require("immutable"));
|
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
const file_1 = __importDefault(require("./file"));
|
|
const hash_1 = require("./hash");
|
|
class Page extends immutable_1.default.Record({
|
|
file: new file_1.default(),
|
|
// Attributes extracted from the YAML header
|
|
attributes: immutable_1.default.Map(),
|
|
// Content of the page
|
|
content: String(),
|
|
// Direction of the text
|
|
dir: String("ltr")
|
|
}) {
|
|
getFile() {
|
|
return this.get("file");
|
|
}
|
|
getAttributes() {
|
|
return this.get("attributes");
|
|
}
|
|
getContent() {
|
|
return this.get("content");
|
|
}
|
|
getDir() {
|
|
return this.get("dir");
|
|
}
|
|
/**
|
|
* Return page as text
|
|
* @return {string}
|
|
*/
|
|
toText() {
|
|
const attrs = this.getAttributes();
|
|
const content = this.getContent();
|
|
if (attrs.size === 0) {
|
|
return content;
|
|
}
|
|
const frontMatter = `---\n${js_yaml_1.default.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: file
|
|
});
|
|
}
|
|
/**
|
|
* Load a page for a file
|
|
* @param {File} file
|
|
* @param {string} content
|
|
* @return {Page}
|
|
*/
|
|
static loadFile(file, content) {
|
|
return new Page({
|
|
file: file,
|
|
content: content
|
|
});
|
|
}
|
|
static fromJSON(json) {
|
|
return new Page({
|
|
file: new file_1.default(json.file),
|
|
// Attributes extracted from the YAML header
|
|
attributes: immutable_1.default.Map(json.atributes),
|
|
// Content of the page
|
|
content: json.content,
|
|
// Direction of the text
|
|
dir: json.dir
|
|
});
|
|
}
|
|
static toJSON(page) {
|
|
return page.toJS();
|
|
}
|
|
hash() {
|
|
return (0, hash_1.hashString)(JSON.stringify(this.toJS()));
|
|
}
|
|
}
|
|
exports.default = Page;
|