Syntax Highlighter TinyMCE plugin for Umbraco

I decided that selecting "Preformatted" in Umbraco's Richtext control and then editing the HTML to include the brush I wanted every time I inserted some code into my blog was getting a little tedious.

I'd already downloaded and configured my templates for using SyntaxHighlighter, but I wanted to go the next step and actually have a nice interface to be able to paste the code into, choose a couple of options and let it be done.

So here it is.  You can download the tinymce plugin from here.  You'll also want to download the SyntaxHighlighter as well and set that up as you like it.  Instructions for setting up SyntaxHighlighter can be found on Alexis' website here.

Credits

I found the basis for this on Nawaf's Blog.  As such I can't in anyway claim this as my own.  All I've done is updated it so that it uses the latest SyntaxHighlighter and made sure it works with a current version of TinyMCE.  And integrated it with Umbraco.  I've also updated the language list and the list of display options.

Installing the Plugin into Tinymce

Unzip the package into the tinymce plugins directory.  You should see the following layout:

tinymce_plugin_dir

then it's simply a matter of customising your tinymce initialization to inlude codehighlighting in your advanced theme.  Something like this would do, customise if how you like:

<!--
tinyMCE.init({
 theme : "advanced",
 theme_advanced_toolbar_location : "top",


    plugins : 'preview,codehighlighting',

    theme_advanced_toolbar_align : "right",
    theme_advanced_buttons1_add : " fontselect,fontsizeselect,zoom",
    theme_advanced_buttons2_add : "preview,separator,forecolor,backcolor",
    theme_advanced_buttons3_add_before : "tablecontrols, codehighlighting"
   
});
// -->


The important lines to take note of here are the ones containing codehighlighting.  That is, the plugins line and the theme_advanced_buttons3_add_before.

Additional Umbraco Specific Steps

Adding any additional functionality to the Richtext Editor in Umbraco is a matter of jumping through hoops, and has been written up many a time, so I'll just put a specific example here.

Firstly, open up the tinyMceConfig.config file found in your umbraco installation.  If you haven't already, install the excellent Config Tree Umbraco Package, as it is an excellent tool and makes this step a breeze.

    
    
    <command>
      <umbracoAlias>mceCodehighlighting</umbracoAlias>
      <icon>../umbraco_client/tinymce3/plugins/codehighlighting/img/codehighlight.gif</icon>
      <tinyMceCommand value="" userInterface="true" frontendCommand="codehighlighting">codehighlighting</tinyMceCommand>
      <priority>16</priority>
    </command>

..

  <plugins>
    <plugin loadOnFrontend="true">paste</plugin>
    <plugin loadOnFrontend="true">inlinepopups</plugin>

..

    <plugin loadOnFrontend="false">codehighlighting</plugin>
  </plugins>

Notice the priority - it's best to keep it unique, and determines the placement of the button in the toolbar.  Save your changes, and then go to the web.config file and save it without making any changes to force Umbraco to load the new configuration.

Notice also that I'm adding the codehighlighting plugin to the end of the list of plugins further down the config file.

Once you have done that, head over to the RichText editor datatype and select your new button:

tinymce_plugin_richtext

You should now be able to use it when editing your content.  Choosing the new Code button will bring up a popup allowing you to paste in your code and set the language and a few options to alter the final output.

tinymce_plugin_popupThat's pretty much all there is to it.  If you have any comments or suggestions, please feel free to leave me a note and I'll try to get back to you as soon as possible.

Enjoy!

4 comments for “Syntax Highlighter TinyMCE plugin for Umbraco”

  1. Gravatar of AndresAndres
    Posted Tuesday, December 07, 2010 at 7:46:51 AM

    hi i wonder, do you know where i can find the tinymce initialization file, or do i have to create a new one? if so, wich is the best way to do it, thanks a lot for help!!!

  2. Posted Wednesday, December 08, 2010 at 11:31:43 AM

    Hi Andres,
    The easiest way to find the tinymce configuration file for Umbraco is to install the Config Tree package using the Package Manager. It lists all of the configuration files in the config directory and allows you to edit them.

  3. Posted Wednesday, December 08, 2010 at 10:45:02 PM

    Exactly where should i write the below code? Which file in Unbraco 4.5.2?

    tinyMCE.init({ ... });

  4. Gravatar of Robert FosterRobert Foster
    Posted Thursday, December 09, 2010 at 10:43:35 AM

    Hi Pinal,
    I should clarify: with Umbraco, you only need to update the configuration file for tinyMCE - it's found in the config directory of your umbraco installation - the tinyMCE.init() is dynamically generated from that by umbraco.

    Hope this helps...
    Rob.

Post a comment