Home > twittertext

twittertext

Twittertext is a project mainly written in JavaScript, based on the View license.

A Node.js module to implement Twitter's text conformance tests

TwitterText is two things. Firstly, it's a copy of Twitter's own text conformance tests rewritten in Javascript suitable for using with Node.js. Secondly, it's a Node.js module that passes all those tests, therefore allowing you to parse Twitter messages.

If that hasn't got you excited perhaps an example will.

// Import the module.
var twittertext = require("twittertext");
// Create a Twitter message.
var message = twittertext.Message("@riggbot Look at this example message with @replies, #hashtags, and mentions: @borgar @bengoldacre @stephenfry");
// Which users were mentioned in the message?
message.mentions;
["riggbot", "replies", "borgar", "bengoldacre", "stephenfry"]
// What hashtags were there?
message.hashtags
["hashtags"]
// Was it a reply to someone?
message.replies
["riggbot"]
// Did it include any URLs?
message.urls
[]

It will even convert the message into HTML for you:

var message = twittertext.Message("Hayley Mustafa writes, why do you look so much like my dad @isaiahmustafa? http://www.youtube.com/watch?v=JvuYcbgZl-U")
message.html
'Hayley Mustafa writes, why do you look so much like my dad @<a class="tweet-url username" href="http://twitter.com/isaiahmustafa">isaiahmustafa</a>? <a href="http://www.youtube.com/watch?v=JvuYcbgZl-U">http://www.youtube.com/watch?v=JvuYcbgZl-U</a>'

There are three caveats.

First, the library doesn't include the "hit highlighting" tests. The hit highlighting tests are for highlighting search terms within a message, and frankly we don't find them either useful or interesting.

Secondly, we don't include support for hashtag indices. Again, they're neither useful nor interesting.

Finally, the library currently fails two tests. They're pretty obscure so you may not even notice the missing functionality (and we are aiming to pass them eventually) but if you're worried you can see them in the code:

  1. Missing test one: auto-link URL within Japanese text
  2. Missing test two: don't allow @ characters in the middle of URLs