Skip to content

Instantly share code, notes, and snippets.

@lx78WyY0J5
Forked from craveytrain/gistFetch.html
Created December 10, 2022 17:24
Show Gist options
  • Save lx78WyY0J5/99fddf6bb8193ad29d4fd1b5b31c8fd4 to your computer and use it in GitHub Desktop.
Save lx78WyY0J5/99fddf6bb8193ad29d4fd1b5b31c8fd4 to your computer and use it in GitHub Desktop.
Fetching the Gist
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Gist Fetch</title>
</head>
<body>
<h1>Separate Files</h1>
<h2>Contact_form.html</h2>
<a class="gistPlaceholder" href="https://gist.github.com/476405#file_contact_form.html">contact_form.html</a>
<h2>overlay.js</h2>
<a class="gistPlaceholder" href="https://gist.github.com/476405#file_overlay.js">overlay.js</a>
<h2>style.css</h2>
<a class="gistPlaceholder" href="https://gist.github.com/476405#file_style.css">style.css</a>
<h1>Combo File</h1>
<a class="gistPlaceholder" href="https://gist.github.com/476405">Accessible Inline Form Labels</a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="jquery-gistFetch.js"></script>
<script>
$(document).ready(function(){
$('.gistPlaceholder').gistFetch();
});
</script>
</body>
</html>
;(function($) {
// Fetch Gist and drop it into page
$.fn.gistFetch = function(options) {
var opts = $.extend({}, $.fn.gistFetch.defaults, options);
// Duck punch document.write
document.oldWrite = document.write;
document.write = $.fn.gistFetch.docOverwrite;
return this.each(function() {
var $this = $(this);
// Support for the Metadata Plugin.
var o = $.meta ? $.extend({}, opts, $this.data()) : opts,
aURL = this.href.split(/[\/#]/),
gistId = aURL[3],
fileName = (aURL[4]) ? aURL[4].substring(5) : '',
gistURL = o.gistURL + gistId + '.js',
snipId = gistId + fileName;
gistURL += (fileName) ? '?file=' + fileName : '';
// Store a pointer to the gist place holder
$(document).data(snipId, $this);
$.getScript(gistURL);
});
};
$.fn.gistFetch.docOverwrite = function ( /* String */ markup) {
$gist = $(markup);
// If the code in the document.write starts with a link and is from gist.github.com
if (markup.substring(1, 5).toLowerCase() === 'link' && $gist.attr('href').match('gist.github.com')) {
if (!$(document).data('gisted')) {
$('head').append(markup);
$(document).data('gisted', true);
}
// If the code in document.write has a class of 'gist' assume it's a gist
} else if ($gist.hasClass('gist')) {
var gistId = $gist.attr('id').substring('5'),
fileName = $gist.find('.gist-meta a:eq(1)').text(),
snipId = gistId + fileName,
$link = $(document).data(snipId);
// If the pointer for the gistId and fileName exists (meaning file specfic gist)
if (typeof $link !== 'undefined') {
$link.replaceWith($gist);
$(document).removeData(snipId);
// Else assumes gist link of whole gist with no fileName specified
} else {
$(document).data(gistId).replaceWith($gist);
$(document).removeData(gistId);
}
// Else just run plain old document.write
} else {
document.oldWrite.apply(document, arguments);
}
};
// default options
$.fn.gistFetch.defaults = {
gistURL: 'https://gist.github.com/'
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment