UNPKG

1.35 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs')
4var yaml = require('yaml-js')
5var path = require('path')
6var _ = require('lodash')
7var util = require('../util')
8
9
10var PAGE_FORMATS = ['.md', '.html']
11
12
13function Page(attrs) {
14 var fpath = attrs.fpath
15
16 this.site = attrs.site
17 this.ext = path.extname(fpath)
18
19 this.slug = path.basename(fpath, this.ext)
20 this.path = path.relative(this.site.cwd, fpath)
21 this.title = util.capitalize(this.slug)
22
23 if (this.validFormat && fs.existsSync(fpath)) {
24 var content = fs.readFileSync(fpath, this.site.encoding)
25 var parts = content.split('---')
26
27 if (parts.length >= 3) {
28 _.extend(this, yaml.load(parts[1]))
29 this.content = parts.slice(2).join('---')
30 this.excerpt = this.content.slice(0, this.content.indexOf('\n\n'))
31 }
32 else {
33 this.content = content
34 }
35 }
36
37 this.url = '/' + (this.path.indexOf(path.sep) >= 0 ? path.dirname(this.path) : '')
38
39 this.dest = path.join(this.site.dest, this.site.baseurl.slice(1), this.path.replace(/\.\w+$/, '.html'))
40}
41
42Object.defineProperties(Page.prototype, {
43 validFormat: {
44 get: function() {
45 return PAGE_FORMATS.indexOf(this.ext) >= 0
46 }
47 },
48
49 valid: {
50 get: function() {
51 return this.validFormat
52 }
53 },
54
55 publishable: {
56 get: function() {
57 return this.valid
58 }
59 }
60})
61
62module.exports = Page