forked from ritz078/embed-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncEmbed.js
More file actions
96 lines (86 loc) · 2.7 KB
/
Copy pathasyncEmbed.js
File metadata and controls
96 lines (86 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { matches, ifInline } from '../modules/utils'
/**
* This is a private function which is used to get the actual text to be replaced for
* a particular url in inline embedding. This returns a promise
* @param {object} _ reference to this
* @param {function} urlToText The function that converts url to replaceable text
* @param {object} match object containing info of matching string
* @return {Promise} resolves to the text
*/
function getInlineData(_, urlToText, match) {
let url = (_.options.link ? match[0].slice(0, -4) : match[0]) || match[1];
if (_.options.served.indexOf(url) >= 0) return Promise.resolve(null);
return new Promise((resolve) => {
urlToText(_, match, url).then((text) => {
if (!text) return resolve();
_.options.served.push(url);
resolve(text);
})
})
}
/**
* A helper function for inline embedding
* @param _
* @param urlToText
* @returns Promise
*/
function inlineAsyncEmbed(_, urlToText) {
let regexInline = _.options.link ? new RegExp(`([^>]*${_.regex.source})<\/a>`, 'gi') : new RegExp(`([^\\s]*${_.regex.source})`, 'gi');
let match, promises = [];
while ((match = matches(regexInline, _.output)) !== null)
promises.push(getInlineData(_, urlToText, match));
return new Promise((resolve) => {
if (matches.length)
Promise.all(promises).then((data) => {
let i = 0;
_.output = _.output.replace(regexInline, (match) => {
if (_.options.link)
return !_.options.inlineText ? data[i] + '</a>' : match + data[i++];
else
return !_.options.inlineText ? data[i] : match + data[i++];
});
resolve(_.output)
});
else
resolve(_.output)
})
}
function getNormalData(_, urlToText, match) {
let url = match[0];
if (_.options.served.indexOf(url) >= 0) return;
return new Promise((resolve) => {
urlToText(_, match, url, true).then(function (text) {
if (!text) resolve();
_.options.served.push(url);
_.embeds.push({
text : text,
index: match.index
});
resolve()
})
})
}
/**
* A helper function for normal embedding
* @param {object} _
* @param {function} urlToText
* @return {Promise}
*/
function normalAsyncEmbed(_, urlToText) {
let match, promises = [];
while ((match = matches(_.regex, _.input)) !== null)
promises.push(getNormalData(_, urlToText, match));
return new Promise(function (resolve) {
Promise.all(promises).then(function () {
resolve(_.embeds)
});
})
}
export default function asyncEmbed(_, urlToText) {
return new Promise(function (resolve) {
if (ifInline(_.options, _.service))
inlineAsyncEmbed(_, urlToText).then((output) => resolve([output, _.embeds]));
else
normalAsyncEmbed(_, urlToText).then((embeds) => resolve([_.output, embeds]))
})
}