tweet-patch2.0.5 • Public • Published
Convert plain-text back into twitter-ready markup.
Version 2.0 of this module no longer attempts to locate/use the
data.entities objects from the Twitter API returned data.
It will, however, use the
data.entities.urls array from the Twitter data (if available) to determine if there is a media
url attached to the end of the plain-text tweet by the Twitter API. You can use the
stripTrailingUrl option to remove this
trailing media url if you are serving plain-text tweets without embedded media support.
$ npm install --save tweet-patch
Option 01: Pass in a string with urls, hashtags and user-mentions:
var tweetPatch = ;;
Option 02: Pass in a Twitter Object from the Twitter API:
var tweetPatch = ;const tweetObj =text: '@SomeUser, go check out this #awesome #thing !'entities:urls: ...// ...;
Both examples above would result in the following return string (formatted for readability):
'<a href="https://twitter.com/SomeUser">@SomeUser</a> go, check out this <a href="https://twitter.com/hashtag/awesome">#awesome</a> <a href="https://twitter.com/hashtag/thing">#thing</a> <a href="http://t.co/a01234">http://t.co/a01234</a>!'
The tweet object returned from the Twitter API, or a string containing hashtags, urls and user-mentions.
Pass in an object and the key:value pairs will be assigned to the anchor tags that are created (uses obj-to-property-string). Pass in a string, and that string will be assigned to the url anchor tags as-is.
// Using an object;//=> '<a href="">#hi</a> <a href="" class="myClass"></a>'// Using a string;//=> '<a href="">#hi</a> <a href="" class="myClass"></a>'
This is only used if you have supplied a data object returned from the Twitter API, and that object has the
property. The reason for this is because we only want to strip the trailing media url if you are serving plain-text tweets,
(no media support) and you want to clean up the tweet text. This option only works when you have supplied a Tweet object with
entities.urls property to ensure that we aren't deleting actual urls put there by the tweet author.
This is only used if you have supplied a data object returned from the Twitter API, and the Twitter API has supplied
html property on that object for you.
- Some tweets will be formatted in a way that makes it impossible to accurately parse the data. For example, if a url is
typed inside of parenthesis:
(http://t.co/foo)tweet-patch will see the closing parenthesis as part of the url. For more information, see issue #8.
MIT @ Michael Wuergler