forked from ritz078/embed-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgist.js
More file actions
58 lines (48 loc) · 1.68 KB
/
Copy pathgist.js
File metadata and controls
58 lines (48 loc) · 1.68 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
import regex from '../regex'
import { embed } from '../../helpers'
export default class Gist {
constructor(input, output, options, embeds) {
this.input = input;
this.output = output;
this.options = options;
this.embeds = embeds;
this.regex = regex.gist;
this.service = 'gist';
if(typeof this.options.input !== 'string'){
this.options.input.addEventListener('rendered', () => {
this.load()
})
}
}
template(match) {
return `<div class="ejs-gist" data-src="${match}"></div>`
}
load() {
let gists = this.options.input.getElementsByClassName('ejs-gist');
for (let i = 0; i < gists.length; i++) {
let gistFrame = document.createElement("iframe");
gistFrame.setAttribute("width", "100%");
gistFrame.id = `ejs-gist-${i}`;
let zone = gists[i];
zone.innerHTML = "";
zone.appendChild(gistFrame);
// Create the iframe's document
let url = gists[i].getAttribute('data-src');
url = url.indexOf('http') === -1 ? `https://${url}` : url;
let gistFrameHTML = `<html><base target="_parent"/><body onload="parent.document.getElementById('ejs-gist-${i}').style.height=parseInt(document.body.scrollHeight)+20+'px'"><script type="text/javascript" src="${url}.js"></script></body></html>`;
// Set iframe's document with a trigger for this document to adjust the height
let gistFrameDoc = gistFrame.document;
if (gistFrame.contentDocument) {
gistFrameDoc = gistFrame.contentDocument;
} else if (gistFrame.contentWindow) {
gistFrameDoc = gistFrame.contentWindow.document;
}
gistFrameDoc.open();
gistFrameDoc.writeln(gistFrameHTML);
gistFrameDoc.close();
}
}
process() {
return embed(this);
}
}