-
Notifications
You must be signed in to change notification settings - Fork 8
/
library.js
64 lines (57 loc) · 2.12 KB
/
library.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
"use strict";
var plugin = {};
plugin.parse = function(data, callback) {
var code = /(?:<pre>.*?<\/pre>|<code>.*?<\/code>)/g;
if ( data && typeof data === 'string' ) {
//preview
data = parser(data, code);
} else if ( data.postData && data.postData.content && data.postData.content ) {
//post
data.postData.content = parser(data.postData.content, code);
} else if ( data.userData && data.userData.signature && data.userData.signature ) {
//signature
data.userData.signature = data.userData.signature
//Handle line breaks inside a paragraph.
.replace(/([^>]+)\n/g, "$1<br>")
//Text align left
.replace(/[^`]?<p><-((?:.|\n)*?)<-<\/p>/gm,'<p class="text-left">$1</p>')
//Text align right
.replace(/[^`]?<p>->((?:.|\n)*?)-><\/p>/gm,'<p class="text-right">$1</p>')
//Text align center
.replace(/[^`\n]?<p>->((?:.|\n)*?)<-<\/p>/gm,'<p class="text-center">$1</p>')
//Text align justify
.replace(/[^`]?<p>=>((?:.|\n)*?)<=<\/p>/gm,'<p class="text-justify">$1</p>')
//Underlined text.
.replace(/[^`]?~((?:.|\n)*?)~/g, "<u>$1</u>");
}
callback(null, data);
};
function parser(data, code) {
//create a variable to capture code content
var codesTag = [];
//replace all codes tags by a var we can use in a regex later
data = data.replace(code, function(match){
codesTag.push(match);
return '__CODE__';
});
//do the replace on the whole
data = data
//Handle line breaks inside a paragraph.
.replace(/([^>]+)\n/g, "$1<br>")
//Text align left
.replace(/[^`]?<p><-((?:.|\n)*?)<-<\/p>/gm,'<p class="text-left">$1</p>')
//Text align right
.replace(/[^`]?<p>->((?:.|\n)*?)-><\/p>/gm,'<p class="text-right">$1</p>')
//Text align center
.replace(/[^`\n]?<p>->((?:.|\n)*?)<-<\/p>/gm,'<p class="text-center">$1</p>')
//Text align justify
.replace(/[^`]?<p>=>((?:.|\n)*?)<=<\/p>/gm,'<p class="text-justify">$1</p>')
//Underlined text.
.replace(/[^`]?~((?:.|\n)*?)~/g, "<u>$1</u>");
//replace CODE with previously stocked code content
data = data.replace(/__CODE__/g, function(){
return codesTag.shift();
});
return data;
};
module.exports = plugin;