Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In a WIKI site, I'd like to allow users to input <pre> tags.

My goal is to use a syntax highlighter to build a small KB for he IT team. How can I setup the rich editor to easily produces snippets like this :

<pre class="language-powershell">
$site = Get-SPSite http://url
$owner = $site.Owner
</pre>

I'm using SP2010 enterprise, and the Enterprise Wiki template

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

For SharePoint 2010 you could create a custom action to add a button to the ribbon which opens a new page to paste you source code into. Once the user closes the window some Javascript wraps the code into the pre tags and inserts it at the position of the cursor.

UPDATE: You could use combination of the zoombldr.aspx page (used to edit the HTML source) and JavaScript code which is used to insert a hyperlink into a Wiki page. That's how I did it (even though mine was for 2007 those haven't changed a lot). Just add a button to the ribbon via custom action which pops up the zoombldr.aspx page and let the user paste the source into it. Once the page is closed the JavaScript grabs the content and insert it into a wrapped pre element.

UPDATE2: AFAIK there is no official SharePoint API to insert stuff into a rich-text field. However, here's a piece of JavaScript I just gobbled together (not tested!) to open a dialog and insert the returned code into the rich-text field:

function RTE_InsertSourceCode(strBaseElementID)
{
    var docEditor=RTE_GetEditorDocument(strBaseElementID);
    var currentContent=new Array;
    currentContent.value="";
    currentContent.dir=docEditor.dir;
    var variables=RTE_GetEditorInstanceVariables(strBaseElementID);
    var returnValue=showModalDialog(
        variables.aSettings.urlWebRoot+'/_layouts/zoombldr.aspx',
        currentContent,
        'dialogHeight: 550px; dialogWidth:500px; help: no; status:no; resizable: yes');
    if (returnValue != "")
    {
        returnValue = "<pre class=\"language-powershell\">" + returnValue + "</pre>";
        RTE_GetSelection(strBaseElementID).pasteHTML(returnValue);
    }
    var docTextRange=docEditor.body.createTextRange();
    docTextRange.scrollIntoView(false);
    if (RTE_UseDynamicHeightSizing(strBaseElementID))
    {
        RTE_DocEditor_AdjustHeight(strBaseElementID);
    }
}

All you have to do now is create your custom action and you should be all set.

For SharePoint 2007 you need to hack into the rich-text editor (note: works only in IE) function RTE_FullHtmlToolBarDefinitionFactory to add a button to the toolbar which then does the same as above.

I've been doing that some time ago for a SharePoint 2007 Blog and Wiki using the Google Code Prettifier project and it worked so far (with some issues in old IE 6/7 browsers, though).

share|improve this answer
    
I'm using SP2010 enterprise, and the Enterprise Wiki template. Is there any SP specific API to inject the html tag ? or should I use a standard div-editable insertion ? –  Steve B Mar 16 '12 at 15:30
    
I've retagged your question if you don't mind –  Sig Weber Mar 16 '12 at 17:06
    
Thanks for the sample code. I'll take a close look at this solution. –  Steve B Mar 19 '12 at 8:36
add comment

I'd look into adding this to your webpart. It's lite weight, free, and fully customizable: http://ckeditor.com/

share|improve this answer
2  
It might be helpful if you included some information/links about integrating ckeditor with SharePoint and how this would solve Steve's problem. –  Kit Menke Mar 16 '12 at 15:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.