Skip to content

Commit 675a357

Browse files
committed
fix(twitter): Support for twitter embedding added
1 parent 0b8ad3f commit 675a357

2 files changed

Lines changed: 86 additions & 32 deletions

File tree

src/embed.es6

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,56 @@
1919
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
//SOFTWARE.
2121

22-
import utils from './modules/utils.es6';
23-
import Emoji from './modules/emoticons/emoji.es6';
24-
import Smiley from './modules/emoticons/smiley.es6';
25-
import Url from './modules/url.es6';
26-
import Code from './modules/code.es6';
22+
import utils from './modules/utils.es6';
23+
import Emoji from './modules/emoticons/emoji.es6';
24+
import Smiley from './modules/emoticons/smiley.es6';
25+
import Url from './modules/url.es6';
26+
import Code from './modules/code.es6';
27+
import Twitter from './modules/twitter.es6';
2728

28-
(function () {
29+
(function() {
2930

30-
var defaultOptions = {
31-
link : true,
32-
linkTarget : 'self',
33-
linkExclude : ['pdf'],
34-
emoji : true,
35-
fontIcons : true,
36-
highlightCode : true
37-
};
31+
var defaultOptions = {
32+
link : true,
33+
linkTarget : 'self',
34+
linkExclude : ['pdf'],
35+
emoji : true,
36+
fontIcons : true,
37+
highlightCode : true,
38+
tweetsEmbed : true,
39+
tweetOptions: {
40+
maxWidth : 550,
41+
hideMedia : false,
42+
hideThread : false,
43+
align : 'none',
44+
lang : 'en'
45+
}
46+
};
3847

39-
class EmbedJS {
40-
constructor(options) {
41-
this.options = utils.deepExtend(defaultOptions,options);
42-
this.element = this.options.element;
43-
this.input = this.element.innerHTML;
44-
this.process();
45-
}
48+
class EmbedJS {
49+
constructor(options) {
50+
this.options = utils.deepExtend(defaultOptions, options);
51+
this.element = this.options.element;
52+
this.input = this.element.innerHTML;
53+
this.process();
54+
}
4655

47-
async process(){
48-
let input = this.input;
49-
let options = this.options;
50-
input = options.link ? await (new Url(input, options).process()) : input;
51-
input = options.emoji ? await (new Emoji(input, options).process()) : input;
52-
input = options.fontIcons ? await (new Smiley(input, options).process()) : input;
53-
input = options.highlightCode ? await (new Code(input, options).process()) : input;
56+
async process() {
57+
let input = this.input;
58+
let options = this.options;
59+
input = options.link ? await (new Url(input, options).process()) : input;
60+
input = options.emoji ? await (new Emoji(input, options).process()) : input;
61+
input = options.fontIcons ? await (new Smiley(input, options).process()) : input;
62+
input = options.highlightCode ? await (new Code(input, options).process()) : input;
63+
if (options.tweetsEmbed){
64+
var twitter = new Twitter(input, options);
65+
input = options.tweetsEmbed ? await (twitter.process()) : input;
66+
}
5467

55-
options.element.innerHTML = input;
56-
}
57-
}
68+
options.element.innerHTML = input;
69+
twitter.load();
70+
}
71+
}
5872

59-
window.EmbedJS = EmbedJS;
73+
window.EmbedJS = EmbedJS;
6074
})();

src/modules/twitter.es6

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import utils from './utils.es6';
2+
3+
class Twitter {
4+
constructor(input, options) {
5+
this.input = input;
6+
this.options = options;
7+
this.regex = /https:\/\/twitter\.com\/\w+\/\w+\/\d+/gi;
8+
}
9+
10+
async tweetData(url) {
11+
let config = this.options.tweetOptions;
12+
let apiUrl = `https://api.twitter.com/1/statuses/oembed.json?omit_script=true&url=${url}&maxwidth=${config.maxWidth}&hide_media=${config.hideMedia}&hide_thread=${config.hideThread}&align=${config.align}&lang=${config.lang}`;
13+
let response = await fetchJsonp(apiUrl, {
14+
credentials: 'include'
15+
});
16+
let data = await response.json();
17+
return data;
18+
}
19+
20+
matches() {
21+
let x;
22+
return (x = this.input.match(this.regex)) ? x : null;
23+
}
24+
25+
load() {
26+
twttr.widgets.load(this.options.element);
27+
}
28+
29+
async process() {
30+
var result = this.input;
31+
let matches = utils.getUnique(this.matches());
32+
for (let match of matches) {
33+
let data = await this.tweetData(match);
34+
result += data.html;
35+
}
36+
return result;
37+
}
38+
}
39+
40+
module.exports = Twitter;

0 commit comments

Comments
 (0)