|
1 | 1 | const utils = require('../utils.es6'); |
2 | 2 |
|
3 | 3 | class Twitter { |
4 | | - constructor(input, options, embeds) { |
| 4 | + constructor(input,output, options, embeds) { |
5 | 5 | this.input = input; |
| 6 | + this.output = output; |
6 | 7 | this.options = options; |
7 | 8 | this.embeds = embeds; |
8 | 9 | this.regex = /https:\/\/twitter\.com\/\w+\/\w+\/\d+/gi; |
| 10 | + this.service = 'twitter' |
9 | 11 |
|
10 | 12 | this.load = this.load.bind(this) |
11 | 13 | this.options.element.addEventListener('rendered', this.load, false); |
@@ -41,15 +43,30 @@ class Twitter { |
41 | 43 |
|
42 | 44 | async process() { |
43 | 45 | 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 | + } |
51 | 68 | } |
52 | | - return this.embeds; |
| 69 | + return [this.output, this.embeds]; |
53 | 70 |
|
54 | 71 | } catch (error) { |
55 | 72 | console.log(error); |
|
0 commit comments