Use jQuery to convert HTML within code tags to character entities
2 comments
4415 days since last revision. Details are possibly out of date.
Here’s a quick tip. I wanted to create a document that contained code snippets and used Prism for the code highlighting. To do that easily, I needed all the pre > code tags to have their contents converted from actual code (that the browser will use to render) into text (so the tags show).
For this site I use a plugin that takes care of this, however, for a standard document I used a snippet of jQuery (this assumes that your code is wrapped in a pre
and code
):
$(document).ready(function() {
$('code').each(function() {
$(this).text($(this).html());
});
});
Hey Ben,
Thx for the snippet. What plugin are you using out of curiosity? I find it incredibly annoying to keep having to run HTML through a tool like Postable every time I want to put some markup in a blog post.