Browse Source

Merge pull request #31 from chrplace/formatpost

Find first link, hashtag or mention using a regex for post formatting
master
miguelfreitas 11 years ago
parent
commit
82025ef9ba
  1. 35
      twister_formatpost.js

35
twister_formatpost.js

@ -141,12 +141,20 @@ function dmDataToConversationItem(dmData, localUser, remoteUser) { @@ -141,12 +141,20 @@ function dmDataToConversationItem(dmData, localUser, remoteUser) {
// todo: hashtags
function htmlFormatMsg( msg, output, mentions ) {
var tmp;
var match = null;
var index;
var reAll = new RegExp("(#|@|http[s]?://)");
var reHttp = new RegExp("http[s]?://");
msg = escapeHtmlEntities(msg);
while( msg != undefined && msg.length ) {
var atindex = msg.indexOf("@");
if( atindex != -1 ) {
output.append(msg.substr(0, atindex));
tmp = msg.substr(atindex+1);
match = reAll.exec(msg);
if( match ) {
if( match[0] == "@" ) {
output.append(msg.substr(0, match.index));
tmp = msg.substr(match.index+1);
var username = _extractUsername(tmp);
if( username.length ) {
if( mentions.indexOf(username) < 0 )
@ -161,14 +169,9 @@ function htmlFormatMsg( msg, output, mentions ) { @@ -161,14 +169,9 @@ function htmlFormatMsg( msg, output, mentions ) {
}
}
var httpindex = msg.indexOf("http://");
var httpsindex = msg.indexOf("https://");
if (httpsindex != -1) {
httpindex = httpsindex;
}
if( httpindex != -1 ) {
output.append(msg.substr(0, httpindex));
tmp = msg.substring(httpindex);
if( reHttp.exec(match[0]) ) {
output.append(msg.substr(0, match.index));
tmp = msg.substring(match.index);
var space = tmp.indexOf(" ");
var url;
if( space != -1 ) url = tmp.substring(0,space); else url = tmp;
@ -184,10 +187,9 @@ function htmlFormatMsg( msg, output, mentions ) { @@ -184,10 +187,9 @@ function htmlFormatMsg( msg, output, mentions ) {
}
}
var hashindex = msg.indexOf("#");
if( hashindex != -1 ) {
output.append(msg.substr(0, hashindex));
tmp = msg.substr(hashindex+1);
if( match[0] == "#" ) {
output.append(msg.substr(0, match.index));
tmp = msg.substr(match.index+1);
var hashtag = _extractUsername(tmp);
if( hashtag.length ) {
var hashtagLinkTemplate = $("#hashtag-link-template").clone(true);
@ -199,6 +201,7 @@ function htmlFormatMsg( msg, output, mentions ) { @@ -199,6 +201,7 @@ function htmlFormatMsg( msg, output, mentions ) {
continue;
}
}
}
output.append(msg);
msg = "";

Loading…
Cancel
Save