Browse Source

text markdown with *bold*, ~italic~, _underlined_ and -strikethrough-

master
Simon Grim 9 years ago
parent
commit
fc61d019b6
  1. 143
      js/twister_formatpost.js

143
js/twister_formatpost.js

@ -226,6 +226,133 @@ function htmlFormatMsg(msg, mentions) { @@ -226,6 +226,133 @@ function htmlFormatMsg(msg, mentions) {
return i;
}
function markdown(str, chr, tag) {
function whiteSpace(i, j) {
j++;
for (i += 1; i < j; i++) {
if (p[i].w)
return true;
}
return false;
}
var i, j, t;
var w = false;
var p = [];
// collecting chars position data
for (i = 0; i < str.length; i++) {
if (str[i] === chr) {
for (j = i + 1; j < str.length; j++) {
if (str[j] !== chr)
break;
}
if (whiteSpaces.indexOf(str[i - 1]) === -1 || whiteSpaces.indexOf(str[j]) === -1) {
if (whiteSpaces.indexOf(str[i - 1]) > -1 || i === 0)
t = -1;
else if (whiteSpaces.indexOf(str[j]) > -1 || i === str.length - 1)
t = 1;
else if (stopCharsMarkDown.indexOf(str[i - 1]) > -1) {
if (stopCharsMarkDown.indexOf(str[j]) === -1)
t = -1;
else {
for (t = j + 1; t < str.length; t++) {
if (whiteSpaces.indexOf(str[t]) > -1) {
if ((2 * j - t) < 0 || whiteSpaces.indexOf(str[2 * j - t]) === -1) {
t = 1;
break;
}
} else if (stopCharsMarkDown.indexOf(str[t]) === -1) {
if ((2 * j - t) < 0 || whiteSpaces.indexOf(str[2 * j - t]) > -1) {
t = -1;
break;
}
}
}
if (t = str.length)
t = 1;
}
} else if (stopCharsMarkDown.indexOf(str[j]) > -1)
t = 1;
else
t = 0;
p.push({i: i, k: j - i, t: t, w: w, a: -1, p: -1});
w = false;
}
i = j;
} else if (!w && whiteSpaces.indexOf(str[i]) > -1) {
w = true;
}
}
// calculating dependencies
for (i = 0; i < p.length; i++) {
if (p[i].t < 1 && p[i].a === -1) {
t = i;
for (j = i + 1; j < p.length; j++) {
if (p[i].t === 0 && whiteSpace(i, j)) {
i = j - 1;
break;
} else if (p[j].t < 1 && p[j].a === -1) {
p[t].p = j;
t = j;
} else if (p[j].t === 1 && p[j].a === -1) {
p[i].a = j;
p[j].a = i;
i = j;
break;
}
}
}
}
for (i = 0; i < p.length; i++) {
if (p[i].t === -1 && p[i].a === -1) {
for (j = p[i].p; j > -1; j = p[j].p) {
if (whiteSpace(i, j)) {
i = j - 1;
break;
} else if (p[j].t === 0
&& !(p[j].p > -1 && p[p[j].p].t === 0 && !whiteSpace(j, p[j].p))) {
p[j].a = i;
p[i].a = j;
i = j;
break;
}
}
}
}
// changing the string
for (i = 0; i < p.length; i++) {
if (p[i].a > -1) {
if (p[i].t === -1 || (p[i].t === 0 && p[i].a > i)) {
if (p[i].k > 1)
html.push('<' + tag + '>' + Array(p[i].k).join(chr));
else
html.push('<' + tag + '>');
} else if (p[i].t === 1 || (p[i].t === 0 && p[i].a < i)) {
if (p[i].k > 1)
html.push(Array(p[i].k).join(chr) + '</' + tag + '>');
else
html.push('</' + tag + '>');
} else {
if (p[i].k > 1)
html.push(Array(p[i].k + 1).join(chr));
else
html.push(chr);
}
strEncoded = '>' + (html.length - 1).toString() + '<';
str = str.slice(0, p[i].i) + strEncoded + str.slice(p[i].i + p[i].k);
l = strEncoded.length - p[i].k;
for (j = i + 1; j < p.length; j++)
p[j].i += l;
}
}
return str;
}
function htmlSplitCounter(str) {
html.push('<span class="splited-post-counter">' + str + '</span>');
@ -233,12 +360,13 @@ function htmlFormatMsg(msg, mentions) { @@ -233,12 +360,13 @@ function htmlFormatMsg(msg, mentions) {
}
var mentionsChars = 'abcdefghijklmnopqrstuvwxyz_0123456789';
var stopCharsTrailing = '/\\.,:;?!*%\'"[](){}^|«»…\u201C\u201D\u2026\u2014\u4E00\u3002\uFF0C\uFF1A\uFF1F\uFF01\u3010\u3011';
var stopCharsTrailing = '/\\*.,:;?!%\'"[](){}^|«»…\u201C\u201D\u2026\u2014\u4E00\u3002\uFF0C\uFF1A\uFF1F\uFF01\u3010\u3011';
var stopCharsTrailingUrl = stopCharsTrailing.slice(1);
var whiteSpaces = ' \f\n\r\t\v\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000';
var stopCharsLeft = '<' + whiteSpaces;
var stopCharsRight = '>' + whiteSpaces;
var stopCharsRightHashtags = stopCharsRight + stopCharsTrailing;
var stopCharsMarkDown = '~_-+=<>&' + stopCharsTrailing + whiteSpaces;
var j, str, strEncoded;
var html = [];
@ -340,7 +468,11 @@ function htmlFormatMsg(msg, mentions) { @@ -340,7 +468,11 @@ function htmlFormatMsg(msg, mentions) {
}
}
msg = msg
msg = markdown(markdown(markdown(markdown(msg,
'*', 'b'), // bold
'~', 'i'), // italic
'_', 'u'), // underlined
'-', 's') // striketrough
.replace(/\(\d{1,2}\/\d{1,2}\)$/, htmlSplitCounter)
.replace(/&(?!lt;|gt;)/g, '&amp;') // FIXME in many cases there is no need to escape ampersand in HTML 5
.replace(/"/g, '&quot;')
@ -363,11 +495,8 @@ function htmlFormatMsg(msg, mentions) { @@ -363,11 +495,8 @@ function htmlFormatMsg(msg, mentions) {
msg = msg.replace(/\n/g, '<br />');
// TODO: add options for emotions; msg = $.emotions(msg);
// TODO: add at least basic markdown (optional) with syntax like this:
// *text* -> bold text
// ~text~ -> italic text
// _text_ -> underlined text
// -text- -> strikethrough text
// TODO make markdown optionally mutable ?
// some escaping tag
return msg;
}

Loading…
Cancel
Save