Skip to content

Instantly share code, notes, and snippets.

@jamesacklin
Created November 5, 2015 20:32
Show Gist options
  • Save jamesacklin/715da099fe2d496eb678 to your computer and use it in GitHub Desktop.
Save jamesacklin/715da099fe2d496eb678 to your computer and use it in GitHub Desktop.
Modify Jekyll on Github Pages' image & alt-text behavior to work with figures and figcaptions
<script>
/* Pulls images with alt text out of paragraphs, puts them into <figure> elements with <figcaption>s */
var imgs = document.querySelectorAll('article p img');
[].forEach.call(imgs, function(e){
var d = document.createElement('figure');
d.innerHTML = '<img src="' + e.src + '" alt="' + e.alt + '"><figcaption>' + e.alt + '</figcaption>';
e.parentNode.insertBefore(d, e);
e.parentNode.removeChild(e);
});
</script>
@mattfelten
Copy link

e.parentNode.replaceChild(d, e); would cut a line out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment