-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkleft.js
225 lines (191 loc) · 6.58 KB
/
markleft.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
// markleft.js
// (c) 2013, Jean-Christophe Hoelt, Fovea.cc
// markleft may be freely distributed under the MIT license.
// For all details and documentation:
// https://github.com/Fovea/markleft
(function () {
'use strict';
// Save a reference to the global object (`window` in the browser, `exports`
// on the server).
var root = this;
// The top-level namespace. All public methods will
// be attached to this. Exported for both the browser and the server.
var markleft;
if (typeof exports !== 'undefined') {
markleft = exports;
} else {
markleft = root.markleft = {};
}
// List of Markleft plugins. Each plugin is an object with the
// following fields:
// - name: text name of the plugin
// - transform: function which takes a text as parameter and
// returns the patched text in HTLM.
//
// User is free to add his own plugins to this array,
// using the registerPlugin method.
markleft.plugins = [];
// Our public API
// --------------
// Library version
markleft.VERSION = '0.3.0';
var runPlugin = function (plug, htmlList) {
var newList = [];
for (var j = 0; j < htmlList.length; ++j) {
var html = htmlList[j];
// If the item is HTML, do not transform.
if (html[0] !== '<') {
if (plug.transform) {
html = plug.transform(html);
}
}
else if (plug.transformHTML) {
html = plug.transformHTML(html);
}
// Add the result to the new list.
if (typeof html === 'string') {
newList.push(html);
}
else {
newList = newList.concat(html);
}
}
return newList;
};
// Get a row text, turn it to richer HTML
markleft.toHTML = function (txt) {
// First escape the <> characters
var htmlList = [ escapeGtPlugin.transform(txt) ];
// Then run plugins
var plugins = markleft.plugins;
for (var i = 0; i < plugins.length; ++i) {
htmlList = runPlugin(plugins[i], htmlList);
}
// Finally remove HTML comments.
htmlList = runPlugin(noComments, htmlList);
return htmlList.join('');
};
// Register a plugin
markleft.registerPlugin = function (p) {
// Check if plugin is valid.
if (!p.name ||!p.transform) {
console.log('markleft.registerPlugin: Invalid plugin');
return;
}
// Check if plugin is not already installed.
for (var i = 0; i < markleft.plugins.length; ++i) {
if (markleft.plugins[i].name === p.name) {
console.log('markleft.registerPlugin: Plugin ' + p.name + ' already registered');
return;
}
}
// Add the plugin to the list.
markleft.plugins.push(p);
};
// Remove a plugin from the collection.
markleft.unregisterPlugin = function (name) {
if (typeof name.name !== 'undefined') { // In case we're given the plugin instead of its name.
name = name.name;
}
for (var i = 0; i < markleft.plugins.length; ++i) {
if (markleft.plugins[i].name === name) {
markleft.plugins.splice(i, 1);
return;
}
}
console.log('markleft.unregisterPlugin: Plugin ' + name + ' not found');
};
// Markleft default plugins
// ------------------------
// Escape HTML < and > characters
var escapeGtPlugin = {
name: 'gtlt',
transform: function (text) {
return text.replace(/</g, '<').replace(/>/g, '>');
}
};
// escapeGt is run in hard by the toHTML method,
// because after escapeGt is done, we will ignore all elements
// starting with <
// That's why it's not registered (nor unregisterable)
// markleft.registerPlugin(escapeGtPlugin);
// Replace text links with HTML <a> links
var urlPlugin = {
name: 'a',
transform: function (text) {
// Based upon:
// http://stackoverflow.com/questions/37684
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var magicToken = '!#@#!';
var html = text.replace(exp, magicToken + '<a href="$1">$1</a>' + magicToken);
return html.split(magicToken);
}
};
markleft.registerPlugin(urlPlugin);
// Escape & characters
// Has to be done after replacing links.
var escapeAmpPlugin = {
name: 'amp',
transform: function (text) {
return text.replace(/&/g, '&');
}
};
markleft.registerPlugin(escapeAmpPlugin);
// Replace line break with <br/>
var lineBreakPlugin = {
name: 'br',
transform: function (text) {
return text.replace(/\n/g, '<br />');
}
};
markleft.registerPlugin(lineBreakPlugin);
// Replace --- with <hr/>
var hrPlugin = {
name: 'hr',
transform: function (text) {
return text.replace(/<br \/>---<br \/>/g, '<hr />');
}
};
markleft.registerPlugin(hrPlugin);
// Remove HTML comments
var noComments = {
name: 'nocomments',
transform: function (text) {
return text;
},
transformHTML: function (html) {
var exp = /<!--[\s\S]*?-->/g;
return html.replace(exp, '');
}
};
// noComments isn't registered as it's run manually
// after all other plugins are done.
// markleft.registerPlugin(noComments);
// Magic token.
var tok = '#%@%#';
var finalReplace = markleft.finalReplace = function (text, exp, rep) {
var ret = text.replace(exp, tok + '<!-- -->' + rep + tok);
return ret.split(tok);
};
// Replace _Some text_ with <i>Some text</i>
markleft.italic = {
name: 'italic',
transform: function (text) {
var exp = /_([^_]+)_/g;
var rep = tok + '<i>' + tok + '$1' + tok + '</i>' + tok;
var ret = text.replace(exp, rep);
return ret.split(tok);
}
};
// Replace *Some text* with <b>Some text</b>
markleft.bold = {
name: 'bold',
transform: function (text) {
var exp = /\*([^*]+)\*/g;
var rep = tok + '<b>' + tok + '$1' + tok + '</b>' + tok;
var ret = text.replace(exp, rep);
return ret.split(tok);
}
};
return markleft;
}).call(this);