Browse Source

Merge pull request #268 from slr/tasty-markdown

fix of post formatting []()
master
miguelfreitas 10 years ago
parent
commit
a0f3ae5776
  1. 25
      js/twister_formatpost.js

25
js/twister_formatpost.js

@ -198,30 +198,36 @@ function dmDataToConversationItem(dmData, localUser, remoteUser) {
// convert message text to html, featuring @users and links formating. // convert message text to html, featuring @users and links formating.
function htmlFormatMsg(msg, mentions) { function htmlFormatMsg(msg, mentions) {
function getStrStart(str, startPoint, stopChars, stopCharsTrailing) { function getStrStart(str, startPoint, stopChars, isStopCharMustExist, stopCharsTrailing) {
for (var i = startPoint; i > -1; i--) { for (var i = startPoint; i > -1; i--) {
if (stopChars.indexOf(str[i]) > -1) if (stopChars.indexOf(str[i]) > -1)
break; break;
} }
if (i !== -1 || !isStopCharMustExist) {
for (i += 1; i < startPoint + 1; i++) { for (i += 1; i < startPoint + 1; i++) {
if (stopCharsTrailing.indexOf(str[i]) === -1) if (stopCharsTrailing.indexOf(str[i]) === -1)
break; break;
} }
} else
i = startPoint + 1;
return i; return i;
} }
function getStrEnd(str, startPoint, stopChars, stopCharsTrailing) { function getStrEnd(str, startPoint, stopChars, isStopCharMustExist, stopCharsTrailing) {
for (var i = startPoint; i < str.length; i++) { for (var i = startPoint; i < str.length; i++) {
if (stopChars.indexOf(str[i]) > -1) if (stopChars.indexOf(str[i]) > -1)
break; break;
} }
if (i !== str.length || !isStopCharMustExist) {
for (i -= 1; i > startPoint - 1; i--) { for (i -= 1; i > startPoint - 1; i--) {
if (stopCharsTrailing.indexOf(str[i]) === -1) if (stopCharsTrailing.indexOf(str[i]) === -1)
break; break;
} }
} else
i = startPoint - 1;
return i; return i;
} }
@ -433,13 +439,12 @@ function htmlFormatMsg(msg, mentions) {
msg = markdown(escapeHtmlEntities(msg), msg = markdown(escapeHtmlEntities(msg),
'`', 'samp'); // kind of monospace, sequence of chars inside will be escaped from markup '`', 'samp'); // kind of monospace, sequence of chars inside will be escaped from markup
for (i = 0; i < msg.length - 7; i++) { for (i = 0; i < msg.length - 7; i++) {
if (msg.slice(i, i + 2) === '](') { if (msg.slice(i, i + 2) === '](') {
// FIXME there can be text with [] inside [] or links with () wee need to handle it too // FIXME there can be text with [] inside [] or links with () wee need to handle it too
j = getStrStart(msg, i - 1, '[', ''); j = getStrStart(msg, i - 1, '[', true, '');
if (j < i) { if (j < i) {
k = getStrEnd(msg, i + 2, ')', ''); k = getStrEnd(msg, i + 2, ')', true, '');
if (k > i + 1) { if (k > i + 1) {
html.push($('#external-page-link-template')[0].outerHTML html.push($('#external-page-link-template')[0].outerHTML
.replace(/\bid\s*=\s*"[^]*?"+/ig, '') // $().removeAttr('id') .replace(/\bid\s*=\s*"[^]*?"+/ig, '') // $().removeAttr('id')
@ -463,7 +468,7 @@ function htmlFormatMsg(msg, mentions) {
} }
} else if (msg.slice(i, i + 4).toLowerCase() === 'http') { } else if (msg.slice(i, i + 4).toLowerCase() === 'http') {
if (msg.slice(i + 4, i + 7) === '://' && stopCharsRight.indexOf(msg[i + 7]) === -1) { if (msg.slice(i + 4, i + 7) === '://' && stopCharsRight.indexOf(msg[i + 7]) === -1) {
j = getStrEnd(msg, i + 7, stopCharsRight, stopCharsTrailingUrl); j = getStrEnd(msg, i + 7, stopCharsRight, false, stopCharsTrailingUrl);
if (j > i + 6) { if (j > i + 6) {
str = msg.slice(i, j + 1); str = msg.slice(i, j + 1);
// FIXME we're trying to not interact with DOM, coz' we want to run really fast [to hell of RegExps] // FIXME we're trying to not interact with DOM, coz' we want to run really fast [to hell of RegExps]
@ -479,7 +484,7 @@ function htmlFormatMsg(msg, mentions) {
i = i + strEncoded.length - 1; i = i + strEncoded.length - 1;
} }
} else if (msg.slice(i + 4, i + 8).toLowerCase() === 's://' && stopCharsRight.indexOf(msg[i + 8]) === -1) { } else if (msg.slice(i + 4, i + 8).toLowerCase() === 's://' && stopCharsRight.indexOf(msg[i + 8]) === -1) {
j = getStrEnd(msg, i + 8, stopCharsRight, stopCharsTrailingUrl); j = getStrEnd(msg, i + 8, stopCharsRight, false, stopCharsTrailingUrl);
if (j > i + 7) { if (j > i + 7) {
str = msg.slice(i, j + 1); str = msg.slice(i, j + 1);
html.push($('#external-page-link-template')[0].outerHTML html.push($('#external-page-link-template')[0].outerHTML
@ -499,9 +504,9 @@ function htmlFormatMsg(msg, mentions) {
for (i = 1; i < msg.length - 1; i++) { for (i = 1; i < msg.length - 1; i++) {
if (msg[i] === '@' && stopCharsLeft.indexOf(msg[i - 1]) === -1 if (msg[i] === '@' && stopCharsLeft.indexOf(msg[i - 1]) === -1
&& stopCharsTrailing.indexOf(msg[i - 1]) === -1 && stopCharsRight.indexOf(msg[i + 1]) === -1) { && stopCharsTrailing.indexOf(msg[i - 1]) === -1 && stopCharsRight.indexOf(msg[i + 1]) === -1) {
j = getStrStart(msg, i - 1, stopCharsLeft, stopCharsTrailing); j = getStrStart(msg, i - 1, stopCharsLeft, false, stopCharsTrailing);
if (j < i) { if (j < i) {
k = getStrEnd(msg, i + 1, stopCharsRight, stopCharsTrailing); k = getStrEnd(msg, i + 1, stopCharsRight, false, stopCharsTrailing);
if (k > i) { if (k > i) {
str = msg.slice(j, k + 1); str = msg.slice(j, k + 1);
html.push($('#external-page-link-template')[0].outerHTML html.push($('#external-page-link-template')[0].outerHTML
@ -540,7 +545,7 @@ function htmlFormatMsg(msg, mentions) {
for (i = 0; i < msg.length - 1; i++) { for (i = 0; i < msg.length - 1; i++) {
if (msg[i] === '#' && msg[i + 1] !== '#' && stopCharsRight.indexOf(msg[i + 1]) === -1) { if (msg[i] === '#' && msg[i + 1] !== '#' && stopCharsRight.indexOf(msg[i + 1]) === -1) {
j = getStrEnd(msg, i + 1, stopCharsRightHashtags, stopCharsTrailing); j = getStrEnd(msg, i + 1, stopCharsRightHashtags, false, stopCharsTrailing);
if (j > i) { if (j > i) {
str = msg.slice(i + 1, j + 1); str = msg.slice(i + 1, j + 1);
html.push($('#hashtag-link-template')[0].outerHTML html.push($('#hashtag-link-template')[0].outerHTML

Loading…
Cancel
Save