Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Add autoLinkWithJSON(), which auto-links tweets using JSON from API
Browse files Browse the repository at this point in the history
  • Loading branch information
keita committed Mar 30, 2012
1 parent cc77786 commit 5882ff1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/conformance.html
Expand Up @@ -35,6 +35,10 @@
return function(test) {
return twttr.txt.autoLink(test.text, {suppressNoFollow: true, suppressDataScreenName: true});
};
case "json":
return function(test) {
return twttr.txt.autoLinkWithJSON(test.text, JSON.parse(test.json), {suppressNoFollow: true, suppressDataScreenName: true});
}
}
case "extract":
switch (section) {
Expand Down
23 changes: 23 additions & 0 deletions twitter-text.js
Expand Up @@ -575,6 +575,29 @@ if (typeof twttr === "undefined" || twttr === null) {
return result;
};

twttr.txt.autoLinkWithJSON = function(text, json, options) {
// concatenate all entities
var entities = [];
for (var key in json) {
entities = entities.concat(json[key]);
}
// map JSON entity to twitter-text entity
for (var i = 0; i < entities.length; i++) {
entity = entities[i];
if (entity.screen_name) {
// this is @mention
entity.screenName = entity.screen_name;
} else if (entity.text) {
// this is #hashtag
entity.hashtag = entity.text;
}
}
// modify indices to UTF-16
twttr.txt.modifyIndicesFromUnicodeToUTF16(text, entities);

return twttr.txt.autoLinkEntities(text, entities, options);
};

twttr.txt.extractHtmlAttrsFromOptions = function(options) {
var htmlAttrs = "";
for (var k in options) {
Expand Down

0 comments on commit 5882ff1

Please sign in to comment.