forked from chrisdiana/cms.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcms.js
838 lines (777 loc) · 23.6 KB
/
cms.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/**
* CMS.js v2.0.0
* Copyright 2018 Chris Diana
* https://chrisdiana.github.io/cms.js
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
var CMS = function(config) {
'use strict';
var container;
var messageContainer;
this.ready = false;
this.routes = {};
this.collections = {};
this.filteredCollections = {};
// Config defaults
var defaults = {
elementId: null,
layoutDirectory: null,
defaultView: null,
errorLayout: null,
mode: 'SERVER',
github: null,
types: [],
plugins: [],
frontMatterSeperator: '---',
listAttributes: ['tags'],
dateParser: /\d{4}-\d{2}(?:-\d{2})?/,
extension: '.md',
sort: undefined,
markdownEngine: null,
debug: false,
messageClassName: 'cms-messages',
onload: function() {},
onroute: function() {},
};
// Messages
var msg = {
NO_FILES_ERROR: 'ERROR: No files in directory',
ELEMENT_ID_ERROR: 'ERROR: No element ID or ID incorrect. Check "elementId" parameter in config.',
DIRECTORY_ERROR: 'ERROR: Error getting files. Make sure there is a directory for each type in config with files in it.',
GET_FILE_ERROR: 'ERROR: Error getting the file',
LAYOUT_LOAD_ERROR: 'ERROR: Error loading layout. Check the layout file to make sure it exists.',
NOT_READY_WARNING: 'WARNING: Not ready to perform action',
};
/**
* Templating function that renders HTML templates.
* @function
* @param {string} text - HTML text to be evaluated.
* @returns {string} Rendered template with injected data.
*/
function Templater(text) {
return new Function(
'data',
'var output=' +
JSON.stringify(text)
.replace(/<%=(.+?)%>/g, '"+($1)+"')
.replace(/<%(.+?)%>/g, '";$1\noutput+="') +
';return output;'
);
}
/**
* Markdown renderer.
* @function
* @returns {string} Rendered markdown content as HTML.
*/
function Markdown() {
this.rules = [
{regex: /(#+)(.*)/g, replacement: header}, // headers - fix link anchor tag regex
{regex: /!\[([^[]+)\]\(([^)]+)\)/g, replacement: '<img src=\'$2\' alt=\'$1\'>'}, // image
{regex: /\[([^[]+)\]\(([^)]+)\)/g, replacement: '<a href=\'$2\'>$1</a>'}, // hyperlink
{regex: /(\*\*|__)(.*?)\1/g, replacement: '<strong>$2</strong>'}, // bold
{regex: /(\*|_)(.*?)\1/g, replacement: '<em>$2</em>'}, // emphasis
{regex: /~~(.*?)~~/g, replacement: '<del>$1</del>'}, // del
{regex: /:"(.*?)":/g, replacement: '<q>$1</q>'}, // quote
{regex: /```[a-z]*\n[\s\S]*?\n```/g, replacement: blockCode}, // block code
{regex: /&&&[a-z]*\n[\s\S]*?\n&&&/g, replacement: jsCode}, // js code - fix
{regex: /`(.*?)`/g, replacement: '<code>$1</code>'}, // inline code
{regex: /\n\*(.*)/g, replacement: ulList}, // ul lists
{regex: /\n[0-9]+\.(.*)/g, replacement: olList}, // ol lists
{regex: /\n(>|>)(.*)/g, replacement: blockquote}, // blockquotes
{regex: /\n-{5,}/g, replacement: '\n<hr />'}, // horizontal rule
{regex: /\n([^\n]+)\n/g, replacement: para}, // add paragraphs
{regex: /<\/ul>\s?<ul>/g, replacement: ''}, // fix extra ul
{regex: /<\/ol>\s?<ol>/g, replacement: ''}, // fix extra ol
{regex: /<\/blockquote><blockquote>/g, replacement: '\n'} // fix extra blockquote
];
this.render = function (text) {
text = '\n' + text + '\n';
this.rules.forEach(function (rule) {
text = text.replace(rule.regex, rule.replacement);
});
return text.trim();
};
function para (text, line) {
var trimmed = line.trim();
if (/^<\/?(ul|ol|li|h|p|bl)/i.test(trimmed)) {
return '\n' + line + '\n';
}
return '\n<p>' + trimmed + '</p>\n';
}
function ulList (text, item) {
return '\n<ul>\n\t<li>' + item.trim() + '</li>\n</ul>';
}
function olList (text, item) {
return '\n<ol>\n\t<li>' + item.trim() + '</li>\n</ol>';
}
function blockquote (text, tmp, item) {
return '\n<blockquote>' + item.trim() + '</blockquote>';
}
function jsCode (text) {
text = text.replace(/```/gm, '');
return '<script type="text/javascript">' + text.trim() + '</script>';
}
function blockCode (text) {
text = text.replace(/```/gm, '');
return '<pre>' + text.trim() + '</pre>';
}
function header (text, chars, content) {
var level = chars.length;
return '<h' + level + '>' + content.trim() + '</h' + level + '>';
}
}
/**
* AJAX Get utility function.
* @function
* @async
* @param {string} url - URL of the request.
* @param {function} callback - Callback after request is complete.
*/
function get(url, callback) {
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
callback(req.response, false);
} else {
callback(req, req.statusText);
}
}
};
req.send();
}
/**
* Extend utility function for extending objects.
* @function
* @param {object} target - Target object to extend.
* @param {object} opts - Options to extend.
* @param {function} callback - Callback function after completion.
* @returns {object} Extended target object.
*/
function extend(target, opts, callback) {
var next;
if (typeof opts === 'undefined') {
opts = target;
}
for (next in opts) {
if (Object.prototype.hasOwnProperty.call(opts, next)) {
target[next] = opts[next];
}
}
if (callback) {
callback();
}
return target;
}
/**
* Utility function for getting a function name.
* @function
* @param {function} func - The function to get the name
* @returns {string} Name of function.
*/
function getFunctionName(func) {
var ret = func.toString();
ret = ret.substr('function '.length);
ret = ret.substr(0, ret.indexOf('('));
return ret;
}
/**
* Checks if the file URL with file extension is a valid file to load.
* @function
* @param {string} fileUrl - File URL
* @returns {boolean} Is valid.
*/
function isValidFile(fileUrl, extension) {
var ext = fileUrl.split('.').pop();
return (ext === extension.replace('.', '') || ext === 'html') ? true : false;
}
/**
* Get URL paths without parameters.
* @function
* @returns {string} URL Path
*/
function getPathsWithoutParameters() {
return window.location.hash.split('/').map(function(path) {
if (path.indexOf('?') >= 0) {
path = path.substring(0, path.indexOf('?'));
}
return path;
}).filter(function(path) { return path !== '#'; });
}
/**
* Get URL parameter by name.
* @function
* @param {string} name - Name of parameter.
* @param {string} url - URL
* @returns {string} Parameter value
*/
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[[]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
/**
* Load template from URL.
* @function
* @async
* @param {string} url - URL of template to load.
* @param {object} data - Data to load into template.
* @param {function} callback - Callback function
*/
function loadTemplate(url, data, callback) {
get(url, function(success, error) {
if (error) callback(success, error);
callback(Templater(success)(data), error);
});
}
/**
* Renders the layout into the main container.
* @function renderLayout
* @async
* @param {string} layout - Filename of layout.
* @param {object} data - Data passed to template.
*/
function renderLayout(layout, data) {
container.innerHTML = '';
var url = [config.layoutDirectory, '/', layout, '.html'].join('');
loadTemplate(url, data, function(success, error) {
if (error) {
handleMessage(msg['LAYOUT_LOAD_ERROR']);
} else {
container.innerHTML = success;
}
});
}
/**
* Get Github URL based on configuration.
* @function
* @param {string} type - Type of file.
* @returns {string} GIthub URL
*/
function getGithubUrl(type, gh) {
var url = [gh.host, 'repos', gh.username, gh.repo, 'contents',
type + '?ref=' + config.github.branch];
if (gh.prefix) url.splice(5, 0, gh.prefix);
return url.join('/');
}
/**
* Formats date string to d/m/yyyy.
* @param {string} dateString - Date string to convert.
* @returns {string} Formatted date string
*/
function formatDate(dateString) {
var date = new Date(dateString);
date.setDate(date.getDate() + 1);
return [date.getDate(),
(date.getMonth() + 1),date.getFullYear()].join('/');
}
/**
* Creates message container element
* @function
* @param {string} classname - Container classname.
*/
function createMessageContainer(classname) {
messageContainer = document.createElement('div');
messageContainer.className = classname;
messageContainer.innerHTML = 'DEBUG';
messageContainer.style.background = 'yellow';
messageContainer.style.position = 'absolute';
messageContainer.style.top = '0px';
document.body.appendChild(messageContainer);
}
/**
* Handle messages
* @function
* @param {string} message - Message.
* @returns {string} message
* @description
* Used for debugging purposes.
*/
function handleMessage(message) {
if (config.debug) messageContainer.innerHTML = message;
return message;
}
/**
* Represents a file collection.
* @constructor
* @param {string} type - The type of file collection (i.e. posts, pages).
* @param {object} layout - The layouts of the file collection type.
*/
var FileCollection = function(type, layout) {
this.type = type;
this.layout = layout;
this.files = [];
this[type] = this.files;
};
FileCollection.prototype = {
/**
* Initialize file collection.
* @method
* @async
* @param {function} callback - Callback function
*/
init: function(callback) {
this.getFiles(function(success, error) {
if (error) handleMessage(msg['DIRECTORY_ERROR']);
this.loadFiles(function(success, error) {
if (error) handleMessage(msg['GET_FILE_ERROR']);
callback();
}.bind(this));
}.bind(this));
},
/**
* Get file list URL.
* @method
* @param {string} type - Type of file collection.
* @returns {string} URL of file list
*/
getFileListUrl: function(type, config) {
return (config.mode === 'GITHUB') ? getGithubUrl(type, config.github) : type;
},
/**
* Get file URL.
* @method
* @param {object} file - File object.
* @returns {string} File URL
*/
getFileUrl: function(file, mode) {
return (mode === 'GITHUB') ? file['download_url'] : file.getAttribute('href');
},
/**
* Get file elements.
* @param {object} data - File directory or Github data.
* @returns {array} File elements
*/
getFileElements: function(data) {
var fileElements;
// Github Mode
if (config.mode === 'GITHUB') {
fileElements = JSON.parse(data);
}
// Server Mode
else {
// convert the directory listing to a DOM element
var listElement = document.createElement('div');
listElement.innerHTML = data;
// get the links in the directory listing
fileElements = [].slice.call(listElement.getElementsByTagName('a'));
}
return fileElements;
},
/**
* Get files from file listing and set to file collection.
* @method
* @async
* @param {function} callback - Callback function
*/
getFiles: function(callback) {
get(this.getFileListUrl(this.type, config), function(success, error) {
if (error) callback(success, error);
// find the file elements that are valid files, exclude others
this.getFileElements(success).forEach(function(file) {
var fileUrl = this.getFileUrl(file, config.mode);
if (isValidFile(fileUrl, config.extension)) {
this.files.push(new File(fileUrl, this.type, this.layout.single));
}
}.bind(this));
callback(success, error);
}.bind(this));
},
/**
* Load files and get file content.
* @method
* @async
* @param {function} callback - Callback function
*/
loadFiles: function(callback) {
var promises = [];
// Load file content
this.files.forEach(function(file, i) {
file.getContent(function(success, error) {
if (error) callback(success, error);
promises.push(i);
file.parseContent();
// Execute after all content is loaded
if (this.files.length == promises.length) {
callback(success, error);
}
}.bind(this));
}.bind(this));
},
/**
* Search file collection by attribute.
* @method
* @param {string} attribute - Attribue in file to search.
* @param {string} search - Search query.
* @returns {object} File object
*/
search: function(attribute, search) {
this[this.type] = this.files.filter(function(file) {
var attr = file[attribute].toLowerCase().trim();
return attr.indexOf(search.toLowerCase().trim()) >= 0;
});
},
/**
* Reset file collection files.
* @method
*/
resetSearch: function() {
this[this.type] = this.files;
},
/**
* Get files by tag.
* @method
* @param {string} query - Search query.
* @returns {array} Files array
*/
getByTag: function(query) {
this[this.type] = this.files.filter(function(file) {
if (query && file.tags) {
return file.tags.some(function(tag) {
return tag === query;
});
}
});
},
/**
* Get file by permalink.
* @method
* @param {string} permalink - Permalink to search.
* @returns {object} File object.
*/
getFileByPermalink: function(permalink) {
return this.files.filter(function(file) {
return file.permalink === permalink;
})[0];
},
/**
* Renders file collection.
* @method
* @async
* @returns {string} Rendered layout
*/
render: function() {
return renderLayout(this.layout.list, this);
},
};
/**
* Represents a file.
* @constructor
* @param {string} url - The URL of the file.
* @param {string} type - The type of file (i.e. posts, pages).
* @param {object} layout - The layout templates of the file.
*/
var File = function(url, type, layout) {
this.url = url;
this.type = type;
this.layout = layout;
this.html = false;
this.content;
this.name;
this.extension;
this.title;
this.excerpt;
this.date;
this.datetime;
this.author;
this.body;
this.permalink;
this.tags;
};
File.prototype = {
/**
* Get file content.
* @method
* @async
* @param {function} callback - Callback function.
* @description
* Get the file's HTML content and set the file object html
* attribute to the file content.
*/
getContent: function(callback) {
get(this.url, function(success, error) {
if (error) callback(success, error);
this.content = success;
// check if the response returns a string instead
// of an response object
if (typeof this.content === 'string') {
callback(success, error);
}
}.bind(this));
},
/**
* Parse front matter.
* @method
* @description
* Overrides post attributes if front matter is available.
*/
parseFrontMatter: function() {
var yaml = this.content.split(config.frontMatterSeperator)[1];
if (yaml) {
var attributes = {};
yaml.split(/\n/g).forEach(function(attributeStr) {
var attribute = attributeStr.split(':');
attribute[1] && (attributes[attribute[0].trim()] = attribute[1].trim());
});
extend(this, attributes, null);
}
},
/**
* Set list attributes.
* @method
* @description
* Sets front matter attributes that are specified as list attributes to
* an array by splitting the string by commas.
*/
setListAttributes: function() {
config.listAttributes.forEach(function(attribute) {
if (this.hasOwnProperty(attribute) && this[attribute]) {
this[attribute] = this[attribute].split(',').map(function(item) {
return item.trim();
});
}
}.bind(this));
},
/**
* Sets filename.
* @method
*/
setFilename: function() {
this.name = this.url.substr(this.url.lastIndexOf('/'))
.replace('/', '')
.replace(config.extension, '');
},
/**
* Sets permalink.
* @method
*/
setPermalink: function() {
this.permalink = ['#', this.type, this.name].join('/');
},
/**
* Set file date.
* @method
* @description
* Check if filename has date otherwise use the date
* in the front matter.
*/
setDate: function() {
var dateRegEx = new RegExp(config.dateParser);
if (this.date) {
this.datetime = new Date(this.date);
this.date = formatDate(this.date);
} else if (dateRegEx.test(this.url)) {
this.date = dateRegEx.exec(this.url);
this.datetime = new Date(this.date);
this.date = formatDate(this.date);
}
},
/**
* Set file body.
* @method
* @description
* Sets the body of the file based on content after the front matter.
*/
setBody: function() {
var html = this.content
.split(config.frontMatterSeperator)
.splice(2)
.join(config.frontMatterSeperator);
if (this.html) {
this.body = html;
} else {
if (config.markdownEngine) {
this.body = config.markdownEngine(html);
} else {
var md = new Markdown();
this.body = md.render(html);
}
}
},
/**
* Parse file content.
* @method
* @description
* Sets all file attributes and content.
*/
parseContent: function() {
this.setFilename();
this.setPermalink();
this.parseFrontMatter();
this.setListAttributes();
this.setDate();
this.setBody();
},
/**
* Renders file.
* @method
* @async
*/
render: function() {
return renderLayout(this.layout, this);
},
};
/**
* Sort method for file collections.
* @method
* @param {string} type - Type of file collection.
* @param {function} sort - Sorting function.
*/
this.sort = function(type, sort) {
if (this.ready) {
this.collections[type][type].sort(sort);
this.collections[type].render();
} else {
handleMessage(msg['NOT_READY_WARNING']);
}
};
/**
* Search method for file collections.
* @method
* @param {string} type - Type of file collection.
* @param {string} attribute - File attribute to search.
* @param {string} search - Search query.
*/
this.search = function(type, attribute, search) {
if (this.ready) {
this.collections[type].search(attribute, search);
this.collections[type].render();
} else {
handleMessage(msg['NOT_READY_WARNING']);
}
};
/**
* Router
* @method
* @description
* Sets up router for file collections to render collections
* and files based on URL hash.
*/
this.route = function() {
var paths = getPathsWithoutParameters();
var type = paths[0];
var filename = paths[1];
var collection = this.collections[type];
var query = getParameterByName('query') || '';
var tag = getParameterByName('tag') || '';
this.routes[type] = function() {
// Default view
if (!type) {
window.location = ['#', config.defaultView].join('/');
}
// List and single views
else {
if (filename) {
// Single view
var permalink = ['#', type, filename.trim()].join('/');
collection.getFileByPermalink(permalink).render();
} else if (collection) {
// List view
if (query) {
// Check for queries
collection.search('title', query);
} else if (tag) {
// Check for tags
collection.getByTag(tag);
} else {
// Reset search
collection.resetSearch();
}
collection.render();
} else {
// Error view
renderLayout(config.errorLayout, {});
}
}
// onroute event
config.onroute();
};
return this.routes[type]();
};
/**
* Register plugins.
* @method
* @description
* Set up plugins based on user configuration.
*/
this.registerPlugins = function() {
config.plugins.forEach(function(plugin) {
var name = getFunctionName(plugin);
if (!this[name]) {
this[name] = plugin;
}
}.bind(this));
};
/**
* Initialize file collections
* @method
* @async
*/
this.initFileCollections = function(callback) {
var promises = [];
var types = [];
// setup collections and routes
config.types.forEach(function(type) {
this.collections[type.name] = new FileCollection(type.name, type.layout);
types.push(type.name);
}.bind(this));
// init collections
types.forEach(function(type, i) {
this.collections[type].init(function() {
promises.push(i);
// reverse order to display newest posts first for post types
if (type.indexOf('post') === 0) {
this.collections[type][type].reverse();
}
// Execute after all content is loaded
if (types.length == promises.length) {
callback();
}
}.bind(this));
}.bind(this));
};
/**
* Init
* @method
* @description
* Initializes the application based on the configuration. Sets up up config object,
* hash change event listener for router, and loads the content.
*/
this.init = function() {
// set config
config = Object.assign({}, defaults, config);
// create message container element if debug mode is enabled
if (config.debug) {
createMessageContainer(config.messageClassName);
}
// check for hash changes
window.addEventListener('hashchange', this.route.bind(this));
if (config.elementId) {
// setup container
container = document.getElementById(config.elementId);
if (container) {
// setup file collections
this.initFileCollections(function() {
// start router by manually triggering hash change
window.dispatchEvent(new HashChangeEvent('hashchange'));
// register plugins and run onload events
this.ready = true;
this.registerPlugins();
config.onload();
}.bind(this));
} else {
handleMessage(msg['ELEMENT_ID_ERROR']);
}
} else {
handleMessage(msg['ELEMENT_ID_ERROR']);
}
};
// Initialize
this.init();
};