import Immutable from "immutable" ; import yaml from "js-yaml" ; import File from "./file" ; import { hashString } from "./hash" ; class Page extends Immutable . Record ( { file : new File ( ) , attributes : Immutable . Map ( ) , content : String ( ) , dir : String ( "ltr" ) , } ) { getFile ( ) { return this . get ( "file" ) ; } getAttributes ( ) { return this . get ( "attributes" ) ; } getContent ( ) { return this . get ( "content" ) ; } getDir ( ) { return this . get ( "dir" ) ; } return { string } toText ( ) { const attrs = this . getAttributes ( ) ; const content = this . getContent ( ) ; if ( attrs . size === 0 ) { return content ; } const frontMatter = `---\n${ yaml . safeDump ( attrs . toJS ( ) , { skipInvalid : true } ) }---\n\n` ; return frontMatter + ( content || "" ) ; } return { string } getPath ( ) { return this . getFile ( ) . getPath ( ) ; } param { File } file return { Page } static createForFile ( file ) { return new Page ( { file : 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 ( json . file ) , attributes : Immutable . Map ( json . atributes ) , content : json . content , dir : json . dir , } ) ; } static toJSON ( page ) { return page . toJS ( ) ; } hash ( ) { return hashString ( JSON . stringify ( this . toJS ( ) ) ) ; } } export default Page ;