-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathgist-embed.js
46 lines (41 loc) · 1.31 KB
/
gist-embed.js
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
//author: Blair Vanderhoof
//https://github.com/blairvanderhoof/gist-embed
$(function(){
var gistMarkerId = "gist-";
//find all code elements
$("code").each(function(){
var $elem, id, url;
$elem = $(this);
id = $elem.attr("id");
if (!id || id == "" || id.indexOf("gist-") != 0) return; // Only works if the code have id start by gist-
//make block level so loading text shows properly
$elem.css("display","block");
id = id || "";
//get the numeric id from the id attribute of the element holder
id = id.substr(0,gistMarkerId.length) === gistMarkerId ? id.replace(gistMarkerId,"") : null;
//make sure result is a numeric id
if(!isNaN(parseInt(id,10))){
url = "https://gist.github.com/"+id+".json";
//loading
$elem.html("Loading gist "+url+" ...");
//request the json version of this gist
$.ajax({
url: "https://gist.github.com/"+id+".json",
dataType: "jsonp",
timeout:10000,
success: function(response){
//the html payload is in the div property
if(response && response.div){
//add the html to your element holder
$elem.html(response.div);
}
},
error: function(){
$elem.html("Failed loading gist "+url);
}
});
}else{
$elem.html("Failed loading gist with incorrect id format: "+$elem.attr("id"));
}
});
});