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

103
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;
}
} }
}
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; var space = tmp.indexOf(" ");
} var url;
if( httpindex != -1 ) { if( space != -1 ) url = tmp.substring(0,space); else url = tmp;
output.append(msg.substr(0, httpindex)); if( url.length ) {
tmp = msg.substring(httpindex); var extLinkTemplate = $("#external-page-link-template").clone(true);
var space = tmp.indexOf(" "); extLinkTemplate.removeAttr("id");
var url; extLinkTemplate.attr("href",url);
if( space != -1 ) url = tmp.substring(0,space); else url = tmp; extLinkTemplate.text(url);
if( url.length ) { extLinkTemplate.attr("title",url);
var extLinkTemplate = $("#external-page-link-template").clone(true); output.append(extLinkTemplate);
extLinkTemplate.removeAttr("id"); msg = tmp.substr(String(url).length);
extLinkTemplate.attr("href",url); continue;
extLinkTemplate.text(url); }
extLinkTemplate.attr("title",url);
output.append(extLinkTemplate);
msg = tmp.substr(String(url).length);
continue;
} }
}
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); hashtagLinkTemplate.removeAttr("id");
hashtagLinkTemplate.removeAttr("id"); hashtagLinkTemplate.attr("href",$.MAL.hashtagUrl(hashtag));
hashtagLinkTemplate.attr("href",$.MAL.hashtagUrl(hashtag)); hashtagLinkTemplate.text("#"+hashtag);
hashtagLinkTemplate.text("#"+hashtag); output.append(hashtagLinkTemplate);
output.append(hashtagLinkTemplate); msg = tmp.substr(String(hashtag).length);
msg = tmp.substr(String(hashtag).length); continue;
continue; }
} }
} }

Loading…
Cancel
Save