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

Loading…
Cancel
Save