-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowdown-classify.js
51 lines (51 loc) · 1.74 KB
/
showdown-classify.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
(function (extension) {
'use strict';
// UML - Universal Module Loader
// This enables the extension to be loaded in different environments
if (typeof showdown !== 'undefined') {
// global (browser or nodejs global)
extension(showdown);
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['showdown'], extension);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = extension(require('showdown'));
} else {
// showdown was not found so we throw
throw Error('Could not find showdown library');
}
}(function (showdown) {
'use strict';
// Initialize a showdown converter to be used in the extension core
var converter = new showdown.Converter();
// The following method will register the extension with showdown
showdown.extension('classify', function () {
var x = 0;
var ext = {
type: 'lang',
filter: function(text, converter, options) {
var classNAMES = text.match(/\[(.*)--]/g),
newText = text;
if ((x < 3) && (classNAMES != null)) {
++x;
for (var i = 0; i < classNAMES.length; i++) {
var classNAME = classNAMES[i].match(/\[(.*)--]/)[1];
if (classNAME != '') {
var regex = new RegExp("\\["+classNAME+"--]([\\s\\S]*)\\[--"+classNAME+"]"),
textSPLIT = newText.match(regex);
if (textSPLIT != null) {
var textSNIPPET = textSPLIT[1],
html = converter.makeHtml(textSNIPPET);
newText = newText.replace(regex, '<div class="'+classNAME+'">'+html+'</div>');
}
}
}
}
x = 0;
return newText;
}
};
return [ext];
});
}));