Skip to content

Instantly share code, notes, and snippets.

@jackmakesthings
Forked from LenaicTerrier/twitter-entities.js
Last active July 17, 2017 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackmakesthings/4fb11615d5312e8f161ee9719e7c84e9 to your computer and use it in GitHub Desktop.
Save jackmakesthings/4fb11615d5312e8f161ee9719e7c84e9 to your computer and use it in GitHub Desktop.
Vanilla (no jquery) fork; please comment or fork if you notice issues w/escaped html.
/*
* twitter-entities.js
* This function converts a tweet with "entity" metadata
* from plain text to linkified HTML.
*
* See the documentation here: http://dev.twitter.com/pages/tweet_entities
* Basically, add ?include_entities=true to your timeline call
*
* Based off existing code from Wade Simmons
* Licensed under the MIT license
* http://wades.im/mons
*
* Modified by lénaïc Terrier
* Licensed under the MIT license
*
* Vanilla JS fork by jack gold (@jackmakesthings)
* Licensed under the MIT license
*/
function linkifyEntities(tweet)
{
var frag = document.createElement('div');
function escapeHTML(text) {
frag.textContent = htmlCharsCorrect(text);
return frag.innerHTML;
}
function htmlCharsCorrect(text)
{
text = text.replace(/&/g,'\u0026');
text = text.replace(/>/g,'\u003E');
text = text.replace(/</g,'\u003C');
text = text.replace(/&(quot;|apos;)/g,'\u0022');
text = text.replace(/'+/g,'\u0027');
return text;
}
var
index_map = {},
result = "",
last_i = 0,
i = 0,
end,
func,
emoji;
var ranges = [
'\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
'\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
'\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
];
var emojis = [];
tweet.text = escapeHTML(tweet.text.replace(new RegExp(ranges.join('|'), 'g'), function(match, offset, string){
emojis.push({
offset: offset,
char: match
});
return '\u0091';
}));
if (!(tweet.entities)) {
return escapeHTML(tweet.text);
}
function processUrls(tweet) {
if (!tweet.entities.urls) { return; }
tweet.entities.urls.forEach(function(entry, index, entries) {
index_map[entry.indices[0]] = [entry.indices[1], function(text) {
return '<a href="' + entry.url + '" target="_blank">' + entry.display_url + '</a>';
}]
});
};
function processHashtags(tweet) {
if (!tweet.entities.hashtags) { return; }
tweet.entities.hashtags.forEach(function(entry, index, entries) {
index_map[entry.indices[0]] = [entry.indices[1], function(text) {
return '<a href="http://twitter.com/search/?q=#' + escapeHTML(entry.text) + '" target="_blank">' + text + '</a>';
}]
});
}
function processMentions(tweet) {
if (!tweet.entities.user_mentions) { return; }
tweet.entities.user_mentions.forEach(function(entry, index, entries) {
index_map[entry.indices[0]] = [entry.indices[1], function(text) {
return '<a href="http://twitter.com/' + escapeHTML(entry.screen_name) + '" target="_blank">' + text + '</a>';
}]
});
}
processUrls(tweet);
processHashtags(tweet);
processMentions(tweet);
for (i=0; i < tweet.text.length; ++i) {
var ind = index_map[i];
if (ind) {
end = ind[0];
func = ind[1];
if (i > last_i) {
result += escapeHTML(tweet.text.substring(last_i, i));
}
result += func(tweet.text.substring(i, end));
i = end - 1;
last_i = end;
}
}
if (i > last_i) {
result += escapeHTML(tweet.text.substring(last_i, i));
}
result = result.replace(/\u0091/g, function(match, offset, string){
emoji = emojis.shift();
return '<span class="emoji" data-emoji="u'+emoji.char.charCodeAt(0)+'">'+emoji.char+'</span>'
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment