Skip to content

Commit b3a9ec3

Browse files
committed
feat(inline): Twitter inline embedding support added
1 parent 31f3793 commit b3a9ec3

6 files changed

Lines changed: 191 additions & 141 deletions

File tree

dist/embed.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js

Lines changed: 84 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/embed.es6

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ const helper = require('./modules/video/helper.es6');
150150
[output, embeds] = (new Image(input, output, options, embeds).process());
151151

152152
if (options.tweetsEmbed && build.TWITTER) {
153-
this.twitter = new Twitter(input, options, embeds);
154-
embeds = options.tweetsEmbed ? await (this.twitter.process()) : output;
153+
this.twitter = new Twitter(input,output, options, embeds);
154+
[output, embeds] = await (this.twitter.process());
155155
}
156156

157157
let result = utils.createText(output, embeds);

src/js/modules/twitter/twitter.es6

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const utils = require('../utils.es6');
22

33
class Twitter {
4-
constructor(input, options, embeds) {
4+
constructor(input,output, options, embeds) {
55
this.input = input;
6+
this.output = output;
67
this.options = options;
78
this.embeds = embeds;
89
this.regex = /https:\/\/twitter\.com\/\w+\/\w+\/\d+/gi;
10+
this.service = 'twitter'
911

1012
this.load = this.load.bind(this)
1113
this.options.element.addEventListener('rendered', this.load, false);
@@ -41,15 +43,30 @@ class Twitter {
4143

4244
async process() {
4345
try {
44-
let match;
45-
while ((match = utils.matches(this.regex, this.input)) !== null) {
46-
let data = await this.tweetData(match[0]);
47-
this.embeds.push({
48-
text: data.html,
49-
index: match.index
50-
})
46+
if (!utils.ifInline(this.options, this.service)) {
47+
let regexInline = this.options.link ? new RegExp(`([^>]*${this.regex.source})<\/a>`, 'gi') : new RegExp(`([^\\s]*${this.regex.source})`, 'gi')
48+
let match;
49+
while ((match = utils.matches(regexInline, this.output)) !== null) {
50+
let url = this.options.link ? match[0].slice(0, -4) : match[0]
51+
let data = await this.tweetData(url)
52+
let text = data.html
53+
if (this.options.link) {
54+
this.output = !this.options.inlineText ? this.output.replace(match[0], text + '</a>') : this.output.replace(match[0], match[0] + text)
55+
} else {
56+
this.output = !this.options.inlineText ? this.output.replace(match[0], text) : this.output.replace(match[0], match[0] + text)
57+
}
58+
}
59+
} else {
60+
let match;
61+
while ((match = utils.matches(this.regex, this.input)) !== null) {
62+
let data = await this.tweetData(match[0]);
63+
this.embeds.push({
64+
text: data.html,
65+
index: match.index
66+
})
67+
}
5168
}
52-
return this.embeds;
69+
return [this.output, this.embeds];
5370

5471
} catch (error) {
5572
console.log(error);

0 commit comments

Comments
 (0)