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. 107
      twister_formatpost.js

107
twister_formatpost.js

@ -141,62 +141,65 @@ 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] == "@" ) {
var username = _extractUsername(tmp); output.append(msg.substr(0, match.index));
if( username.length ) { tmp = msg.substr(match.index+1);
if( mentions.indexOf(username) < 0 ) var username = _extractUsername(tmp);
mentions.push(username); if( username.length ) {
var userLinkTemplate = $("#msg-user-link-template").clone(true); if( mentions.indexOf(username) < 0 )
userLinkTemplate.removeAttr("id"); mentions.push(username);
userLinkTemplate.attr("href",$.MAL.userUrl(username)); var userLinkTemplate = $("#msg-user-link-template").clone(true);
userLinkTemplate.text("@"+username); userLinkTemplate.removeAttr("id");
output.append(userLinkTemplate); userLinkTemplate.attr("href",$.MAL.userUrl(username));
msg = tmp.substr(String(username).length); userLinkTemplate.text("@"+username);
continue; output.append(userLinkTemplate);
msg = tmp.substr(String(username).length);
continue;
}
} }
}
if( reHttp.exec(match[0]) ) {
var httpindex = msg.indexOf("http://"); output.append(msg.substr(0, match.index));
var httpsindex = msg.indexOf("https://"); tmp = msg.substring(match.index);
if (httpsindex != -1) { var space = tmp.indexOf(" ");
httpindex = httpsindex; var url;
} if( space != -1 ) url = tmp.substring(0,space); else url = tmp;
if( httpindex != -1 ) { if( url.length ) {
output.append(msg.substr(0, httpindex)); var extLinkTemplate = $("#external-page-link-template").clone(true);
tmp = msg.substring(httpindex); extLinkTemplate.removeAttr("id");
var space = tmp.indexOf(" "); extLinkTemplate.attr("href",url);
var url; extLinkTemplate.text(url);
if( space != -1 ) url = tmp.substring(0,space); else url = tmp; extLinkTemplate.attr("title",url);
if( url.length ) { output.append(extLinkTemplate);
var extLinkTemplate = $("#external-page-link-template").clone(true); msg = tmp.substr(String(url).length);
extLinkTemplate.removeAttr("id"); continue;
extLinkTemplate.attr("href",url); }
extLinkTemplate.text(url);
extLinkTemplate.attr("title",url);
output.append(extLinkTemplate);
msg = tmp.substr(String(url).length);
continue;
} }
}
if( match[0] == "#" ) {
var hashindex = msg.indexOf("#"); output.append(msg.substr(0, match.index));
if( hashindex != -1 ) { tmp = msg.substr(match.index+1);
output.append(msg.substr(0, hashindex)); var hashtag = _extractUsername(tmp);
tmp = msg.substr(hashindex+1); if( hashtag.length ) {
var hashtag = _extractUsername(tmp); var hashtagLinkTemplate = $("#hashtag-link-template").clone(true);
if( hashtag.length ) { hashtagLinkTemplate.removeAttr("id");
var hashtagLinkTemplate = $("#hashtag-link-template").clone(true); hashtagLinkTemplate.attr("href",$.MAL.hashtagUrl(hashtag));
hashtagLinkTemplate.removeAttr("id"); hashtagLinkTemplate.text("#"+hashtag);
hashtagLinkTemplate.attr("href",$.MAL.hashtagUrl(hashtag)); output.append(hashtagLinkTemplate);
hashtagLinkTemplate.text("#"+hashtag); msg = tmp.substr(String(hashtag).length);
output.append(hashtagLinkTemplate); continue;
msg = tmp.substr(String(hashtag).length); }
continue;
} }
} }

Loading…
Cancel
Save