Skip to content

Commit e31ec03

Browse files
committed
feat(singleEmbed): added options to embed only one service from the detected services
1 parent aa7f4b5 commit e31ec03

10 files changed

Lines changed: 101 additions & 98 deletions

File tree

demo/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ <h1>Loading embed.js in a block</h1>
7474
// openGraphEndpoint:'https://opengraph.io/api/1.0/site/${url}',
7575
openGraphExclude: ['github'],
7676
googleAuthKey: 'AIzaSyCqFouT8h5DKAbxlrTZmjXEmNBjC69f0ts',
77-
inlineEmbed: 'all',
77+
// inlineEmbed: 'all',
7878
marked: true,
7979
highlightCode:true,
8080
codeHighlighter:'prismjs',
8181
link: true,
82+
// singleEmbed:true,
8283
onOpenGraphFetch: function(data) {
8384
data.hybridGraph.success = data.error ? false : true;
8485
return data.hybridGraph;

dist/embed.min.js

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

src/embed.es2015.js

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

src/embed.es2015.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/embed.js

Lines changed: 37 additions & 37 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/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var defaultOptions = {
5959
align : 'none',
6060
lang : 'en'
6161
},
62+
singleEmbed : false,
6263
openGraphEndpoint : null,
6364
openGraphExclude : [],
6465
videoEmbed : true,
@@ -209,11 +210,11 @@ export default class EmbedJS {
209210
}).then(function([output, embeds]){
210211
return ifEmbed(options, 'opengraph') ? new Github(input, output, options, embeds).process() : Promise.resolve([output, embeds]);
211212
}).then(function([output, embeds]){
212-
return options.locationEmbed ? new Gmap(input, output, options, embeds).process() : Promise.resolve([output, embeds])
213+
return options.locationEmbed && ifEmbed(options, 'gmap') ? new Gmap(input, output, options, embeds).process() : Promise.resolve([output, embeds])
213214
}).then(function([output, embeds]){
214215
return ifEmbed(options, 'slideshare') ? new SlideShare(input, output, options, embeds).process() : Promise.resolve([output, embeds]);
215216
}).then(([output, embeds]) => {
216-
if (options.tweetsEmbed) {
217+
if (options.tweetsEmbed && ifEmbed(options,'twitter')) {
217218
this.twitter = new Twitter(input, output, options, embeds);
218219
return this.twitter.process()
219220
} else {

src/js/modules/helper.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ function inlineEmbed(_this){
183183
function normalEmbed(_this){
184184
let match;
185185
while ((match = matches(_this.regex, _this.input)) !== null) {
186-
if (!(_this.options.served.indexOf(match[0]) === -1)) continue;
187-
let text = _this.template(match[0]);
186+
let url = match[0]
187+
if (!(_this.options.served.indexOf(url) === -1) || (_this.options.served.length && _this.options.singleEmbed)) continue;
188+
_this.options.served.push(url)
189+
let text = _this.template(url);
188190
_this.embeds.push({
189191
text : text,
190192
index: match.index
@@ -199,5 +201,5 @@ export function embed(_this){
199201

200202

201203
export function baseEmbed(input, output, embeds, options, regex, service, flag){
202-
return ifEmbed(options, service) || flag ? new Base(input, output, embeds, options, regex, service).process() : [output, embeds]
204+
return ifEmbed(options, service) || (ifEmbed(options, service) && flag) ? new Base(input, output, embeds, options, regex, service).process() : [output, embeds]
203205
}

src/js/modules/map/map.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,33 @@ export default class Gmap {
6161
let match, promises = [],
6262
allMatches = [];
6363
while ((match = matches(this.regex, this.output)) !== null) {
64+
this.options.served.push(match);
6465
let promise = this.options.mapOptions.mode !== 'place' ? Gmap.getCoordinate(match[0]) : Promise.resolve([null, null]);
6566
promises.push(promise);
6667
allMatches.push(match)
6768
}
6869

6970
return new Promise((resolve) => {
70-
if (allMatches.length) { //TODO
71-
Promise.all(promises).then((coordinatesArr) => {
72-
for (var i in promises) {
73-
let [latitude, longitude] = coordinatesArr[i];
74-
let text = Gmap.template((allMatches[i])[0], latitude, longitude, this.options);
75-
if (ifInline(this.options, this.service)) {
76-
this.output = this.output.replace(this.regex, (regexMatch) => {
77-
return `<span class="ejs-location">${Gmap.locationText(regexMatch)}</span>${text}`
78-
})
79-
} else {
80-
this.embeds.push({
81-
text: text,
82-
index: allMatches[i][0].index
83-
});
84-
this.output = this.output.replace(this.regex, (regexMatch) => {
85-
return `<span class="ejs-location">${Gmap.locationText(regexMatch)}</span>`
86-
});
87-
}
71+
Promise.all(promises).then((coordinatesArr) => {
72+
for (var i in promises) {
73+
let [latitude, longitude] = coordinatesArr[i];
74+
let text = Gmap.template((allMatches[i])[0], latitude, longitude, this.options);
75+
if (ifInline(this.options, this.service)) {
76+
this.output = this.output.replace(this.regex, (regexMatch) => {
77+
return `<span class="ejs-location">${Gmap.locationText(regexMatch)}</span>${text}`
78+
})
79+
} else {
80+
this.embeds.push({
81+
text: text,
82+
index: allMatches[i][0].index
83+
});
84+
this.output = this.output.replace(this.regex, (regexMatch) => {
85+
return `<span class="ejs-location">${Gmap.locationText(regexMatch)}</span>`
86+
});
8887
}
89-
resolve([this.output, this.embeds])
90-
})
91-
} else {
88+
}
9289
resolve([this.output, this.embeds])
93-
}
90+
})
9491
})
9592
}
9693
}

src/js/modules/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ export function matches(regex, input) {
9797
* @return {boolean} True if it should be embedded
9898
*/
9999
export function ifEmbed(options, service) {
100+
if (options.singleEmbed && options.served.length) return
100101
return ((options.excludeEmbed.indexOf(service) == -1) || (options.excludeEmbed === 'all'));
101102
}
102103

103104
export function ifInline(options, service) {
104-
return ((options.inlineEmbed.indexOf(service) == -1) || (options.inlineEmbed !== 'all'));
105+
return ((options.inlineEmbed.indexOf(service) >= 0) || (options.inlineEmbed === 'all'));
105106
}
106107

107108
/**

0 commit comments

Comments
 (0)