A simple approach

My friend pointed out to me that the formatting on my code blocks is terrible, just grey and boring. It used to be ok before my redesign, but I missed it out this time around. Anyway, I think I found the one I used to use before. Google Code Prettify. Just add a reference to the javascript file:

<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>

To use it you can add the class "prettyprint" to your pre tags like this:

<pre class="prettyprint">
</pre>

I'm lazy

Then I realised it would be a pain to have to go through all of my posts for the last 12 months and add this class to each pre tag, so I edited the partial view in Umbraco which renders the content grid. I made it replace any opening pre tags with a pre tag with the class of prettyprint.

The partial is in Views > Partials Views > Grid > Editors > Rte

You can change it to this:

@model dynamic
@using Umbraco.Web.Templates

@Html.Raw(TemplateUtilities.ParseInternalLinks(Model.value.ToString().Replace("<pre>","<pre class=\"prettyprint\">")))

Now my pre tags are formatted automatically and I don't need to add a class to them.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post

How to create a dynamic image from multiple images using ImageProcessor

This post shows you how I created a handler to create dynamic header images for my umbraco website u…

Read Post

How to send Slack messages programmatically using C#

In this post I show you how you can send Slack messages programmatically using C#

Read Post

How to fix the SVG 404 error when using Umbraco and Azure Blob Storage

In this post I show you how to solve the SVG 404 error when working with Umbraco and Azure Blob Stor…

Read Post