A quick jQuery plugin for asynchronously embedding a gist in an HTML document.
There are a couple of ways to use it. The simpler way is replacing all Gist links in document by their embedded version:
<script>$($.gist);</script>
All links that point to a gist, or gist file will be replaced by the corresponding embedded gist, or gist file.
Gist link replacement can also be called on a container element:
<script>$('#main').gist();</script>
A jQuery selector can be used to filter anchors to replace:
<script>$('a.gist-embed').gist();</script>
By default gist id and file is obtained from anchor urls, but they can be specified as arguments:
<script>$('a#mygist').gist(999085);</script>
<script>$('a#mygist').gist(999085, 'README.mdown');</script>
Going further gist id and file can be provided in an object argument:
<script>$('a#mygist').gist({ id: 999085 });</script>
<script>$('a#mygist').gist({ id: 999085, file: 'README.mdown'});</script>
And as functions too:
<script>$('a.gist').gist({ id: function(){ this.attr('data-gistid') });</script>
<script>$('a.gist').gist({ id: function(){ this.attr('data-gistid') }, file: function(){ this.attr('data-gistfile') }});</script>
Finally, there is a direct method that will insert a gist into the current position in the document:
<script>$.gist(999085)</script>
<script>$.gist(999085, 'README.mdown')</script>
A couple of notes:
-
This works asynchronously, which has the advantage of not blocking the loading of a page. This has the slight disadvantage of "popping" in after it has loaded, but this could be alleviated with animations etc. by creating an event handler for the
gistloaded
event. -
As mentioned, after the gist has been loaded and inserted into the DOM, a
gistloaded
event is triggered on the newly created DOM object. This is to facilitate adding any custom behaviours after the gist has loaded. Potentially this could be things like animations, attaching events etc. -
Currently if you specify an element to 'receive' the gist, that element is replaced, so be cautious of using it again.
Dude this is just awesome way to embed gist in a webpage without hitting performance. 👍
Thanks friend.