Whether you code for a living, write technical documentation, or have a nerdy blog, you'll want to have a pretty way to display code in your blogs. Syntax highlighting, like how an IDE highlights markup and code, can be done in JavaScript and CSS, when rendering to the browser. In 5-30 minutes, you could be quickly styling code blocks like a professional!
We'll be using Syntax Highlighter, in this blog. Of course, alternatives are available, but won't be covered. Feel free to explore your options.
Using CloudFlare
- Navigate to Blogger.
- Go to Theme, and click Edit HTML
- Scroll to the bottom of the html, just before the body end tag. Copy the following markup into the end of the body element.
<link href='//cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shCore.min.css' rel='stylesheet' type='text/css'/> <script src='//cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.min.js' type='text/javascript'/> <script src='//cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shAutoloader.min.js' type='text/javascript'/> <!-- Add your choice of theme, here. --> <!-- Add your choice of syntax brushes, here. --> <script type='text/javascript'> SyntaxHighlighter.config.bloggerMode = true; SyntaxHighlighter.all(); </script>
-
Add a theme from CloudFlare. For example, if you want the default theme, use the following:
<link href='//cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shThemeDefault.min.css' rel='stylesheet' text='text/css'/>
-
Then, add syntax brushes from CloudFlare for the programming languages you typically use. For example, this blog uses the "xml" brush to highlight html markup, throughout the post:
<script src='//cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushXml.min.js' type='text/javascript'/>
- Click "Save Theme"
-
When writing your blog, use the pre element to wrap your code, and add the brush class to it. For customization, like highlighting lines, take a look at configuration. Also, make sure to html encode your code (like < in place of <, below).
<pre class="brush: xml"> <!-- ... ---> </pre>
Building Our Own
Follow the directions, here. Or, if those are too complicated, I'll be writing a step-by-step guide, in a future post.
Comments
Post a Comment