Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Last active December 15, 2015 08:49
Show Gist options
  • Save nicerobot/5233890 to your computer and use it in GitHub Desktop.
Save nicerobot/5233890 to your computer and use it in GitHub Desktop.
d3 gist post-ready updater. For example, including a gist in tumblr blogs.

Add before /body:

<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//gist.googlecode.com/svn/trunk/kares/script.js"></script>
<script src="//gist.googlecode.com/svn/trunk/nicerobot/5233890/replacegista.js"></script>

You can then just add normal markdown references to a gist like this gist and it'll find them and replace them with the embedded gist. Example tumblr post.

I looked at soemarko/1395926 but I didn't want to have to use a div since Markdown already inserts a link. Also, it degrades nicely in case something with the scripts breaks. Lastly, D3 is much more fun than jQuery :P

// TODO chain onload in case it's already defined.
window.onload = function() {
d3.selectAll('a[href*="gist.github.com"]')
.data(function(d,i) { return [this[i].href + '.js']; })
.attr('gist',function(d,i) {return i;})
.each(function(d,i) {
d3.select(this.parentNode)
.insert('div','a[gist="'+i+'"]')
.attr('id','gist'+i);
script({ src: d, append:'gist'+i });
this.remove();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment