This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-page.js
155 lines (121 loc) · 4 KB
/
markdown-page.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*this will probably be made browser compatible at some point in time*/
;(function(root){
var marked = require('marked')
, mde = require('markdown-extra')
, S = require('string')
, hl = require('highlight').Highlight
, fs = require('fs')
function MarkdownPage(text) {
this.text = text || '';
this.metadata = {};
this.title = null;
this.markdown = '';
this.metadataConversions = {}
initMetadataConversions.apply(this)
marked.setOptions({gfm: true, pedantic: false, sanitize: true, highlight: function(code, lang) {
return hl(code);
}});
}
MarkdownPage.prototype.genOutput = function() {
var data = '<!--\n'
, _this = this
Object.keys(this.metadata).forEach(function(key) {
if (_this.metadataConversions[key])
data += key + ': ' + _this.metadataConversions[key].serialize(_this.metadata[key]) + '\n';
else
data += key + ': ' + _this.metadata[key] + '\n'
})
data += '-->\n\n\n'
data += this.title + '\n'
data += S('=').repeat(this.title.length) + '\n\n'
data += this.markdown + '\n'
return data;
}
//this is actually just a sync method, but in the future, may call out to other executables or services
MarkdownPage.prototype.parse = function(callback) {
this.metadata = mde.metadata(this.text, parseMetadata);
parseMetadataWithConversions.call(this)
this.title = mde.heading(this.text);
this.markdown = mde.content(this.text);
this.html = marked(this.markdown);
callback(null);
}
MarkdownPage.prototype.slug = function(slugString) {
if (slugString) return S(slugString.toLowerCase()).slugify().s;
if (this.metadata.slug) return this.metadata.slug;
return S(this.title.toLowerCase()).slugify().s;
}
MarkdownPage.prototype.writeFile = function(file, callback) {
var data = this.genOutput()
fs.writeFile(file, data, callback)
}
/*************************
* STATIC CLASS Methods
*************************/
MarkdownPage.create = function(text) {
return new MarkdownPage(text);
}
MarkdownPage.readFile = function(file, callback) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) return callback(err)
var mdp = MarkdownPage.create(data)
mdp.parse(function(err) {
if (err) return callback(err)
callback(null, mdp)
})
})
}
/*************************
* EXPORTS
*************************/
root.MarkdownPage = MarkdownPage;
/*************************
* PRIVATE
*************************/
function initMetadataConversions() {
var mdc = this.metadataConversions
mdc['publish'] = {
deserialize: function (dateString) {
if (dateString)
return new Date(Date.parse(dateString))
else
return dateString
},
serialize: function (date) {
if (date instanceof Date) {
return date.getFullYear() + '-'
+ ('0' + (date.getMonth()+1)).slice(-2) + '-'
+ ('0' + date.getDate())
} else {
return date.toString()
}
}
}
mdc['tags'] = {
deserialize: function (tagString) { //prob not very readable
if (S(tagString).contains(',')) return tagString.split(',').map(function(tag){ return tag.trim() })
else if (S(tagString).contains(' ')) return tagString.split(' ').map(function(tag){ return tag.trim() })
else return [tagString]
},
serialize: function (tagArray) {
return tagArray.join(', ')
}
}
}
function parseMetadata(metaText){
var metaObj = {};
metaText.split('\n').forEach(function(line) {
var data = line.split(':');
metaObj[data[0].trim()] = data[1].trim();
});
return metaObj;
}
function parseMetadataWithConversions() {
var mdc = this.metadataConversions
, _this = this
Object.keys(this.metadata).forEach(function(key) {
if (mdc[key])
_this.metadata[key] = mdc[key].deserialize(_this.metadata[key])
})
}
})(module.exports || this);