Ian Marshall logo

Ian Marshall

Render Code

I wanted a way to add code to web pages without having to preformat less-than and greater-than symbols. And I realized the only way to do that was to insert the entire chunk of code as a comment. Which, of course, left me with one question.

Can I access the text of a comment with JavaScript? Yes. Yes, I can.

Here's the funny part, though: If the code has HTML comment symbols in it, then this doesn't work. But really, how often does that happen? Almost never.

This very code, however, is one of those "almost never" modules. So my own render-code.js cannot render its own code since it specifically requires the <!-- and --> strings.

So, for this code—and this code only!—I have to provide alternate means of displaying the code in entirety.

render-code.js external link

Note about the current version:

Syntax highlighting is available for HTML and CSS. JavaScript highlighting is not yet implemented and still a work in progress. (Lots of edge cases…go figure.)