-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (79 loc) · 2.22 KB
/
index.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var Ractive = require('ractive')
require('string.prototype.endswith')
Ractive.DEBUG = false
function cbIfErr(onErr, noErr) {
return function (err) {
if (err && !err.notFound) onErr(err)
else noErr.apply(null, [].slice.call(arguments, 1))
}
}
function formatButlerPostList(posts) {
return posts.reverse().filter(function(post) {
return typeof post.metadata.title === 'string'
}).map(function(post) {
return {
title: post.metadata.title,
filename: post.filename.replace(/\.md$/, '')
}
})
}
function ViewModel(butler, renderer, ractiveTemplate, ractiveData) {
var ractive = new Ractive({
el: '',
template: ractiveTemplate,
data: (ractiveData ? Object.create(ractiveData) : {})
})
function getPostListFromButler(cb) {
cb = cb || function () {}
butler.getPosts(cbIfErr(cb, function (posts) {
var postList = formatButlerPostList(posts)
ractive.set('postList', postList)
cb(null, postList)
}))
}
//If not has post list: get it from butler
//return the post list
function getCurrentPostList(cb) {
cb = cb || function () {}
var postList = ractive.get('postList')
if (postList) {
process.nextTick(function () {
cb(null, postList)
})
} else {
getPostListFromButler(cb)
}
}
function setCurrent(key, cb) {
cb = cb || function () {}
var file = key.endsWith('.md') ? key : (key + '.md')
butler.getPost(file, cbIfErr(cb, function (post) {
var date = post.metadata.date
if (date && date.toDateString) date = date.toDateString()
getCurrentPostList(function () {
renderer.renderPost(post, cbIfErr(cb, function (html) {
ractive.set({
html: html,
current: post.filename,
date: date,
page: post.metadata.title
})
cb(null, ractive.toHTML())
}))
})
}))
}
function onPostChanged(key, newValue, oldValue) { //oldValue does nothing right now
function titleHasChanged(postListItem) {
return postListItem.filename === key && (postListItem.title !== newValue.metadata.title)
}
var postList = ractive.get('postList')
if (postList && postList.some(titleHasChanged)) {
getPostListFromButler()
}
}
butler.on('post changed', onPostChanged)
butler.on('index changed', getCurrentPostList)
return setCurrent
}
module.exports = ViewModel