My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and index you

Add Code Block in Blogs

If you want to put some HTML, JavaScript or code of any programming language in your blogger blog posts then entering that as a simple text will mix it with plain text and that would be pretty hard for readers to differentiate between plain text and code block. To make the code look different from plain text you need to highlight code block.

We can use SyntaxHighlighter tool to insert colored code snippets on a web page using client-side javascript only. This tool is useful for users of BLOGGER because we do not have server side resource to parse and highlight the code. For implementing this tool on blooger/blogspot site, we need to do some changes in the template of blog.


Below are the steps to setup:

1. Take the backup of existing template.

2. Download the files from SyntaxHighlighter.

3. Now extract the contents of the package and upload the Scripts and Styles folder to any host or website which can be linked from your blog.A free choice is, of course, Google Docs.

4. Login to BLOGGER, go to: "Settings > Template > Edit HTML", add the following code after the tag:

</div></div> <!-- end outer-wrapper -->

<!-- You need to replace "amitkumarmca04.blogspot.com" with your [YOUR HOST] address -->

<link href='http://amitkumarmca04.blogspot.com/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script src='http://amitkumarmca04.blogspot.com/shCore.js' type='text/javascript'/>

<script src='http://amitkumarmca04.blogspot.com/shBrushCpp.js' type='text/javascript'/>
<script src='http://amitkumarmca04.blogspot.com/shBrushCSharp.js' type='text/javascript'/>
<script src='http://amitkumarmca04.blogspot.com/shBrushCss.js' type='text/javascript'/>
<script src='http://amitkumarmca04.blogspot.com/shBrushJava.js' type='text/javascript'/>
<script src='http://amitkumarmca04.blogspot.com/shBrushJScript.js' type='text/javascript'/>
<script src='http://amitkumarmca04.blogspot.com/shBrushSql.js' type='text/javascript'/>
<script src='http://amitkumarmca04.blogspot.com/shBrushXml.js' type='text/javascript'/>

<script class='javascript'>

dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');

</script>



5. Now you are all done to start posting source codes. You can use the
 
to enclose your source-codes, and give the name="code" attribute and class="CSharp" for your CSharp source.

For example:



<pre name="code" class="CSharp">
public class AmitKumar {
public AmitKumar() {
Response.Write("SyntaxHighlighter Example");
}
}
</pre>


will look like:



Public class AmitKumar
{
public AmitKumar()
{
Response.Write("SyntaxHighlighter Example");
}
}


Another example of code classes are:
For C#(C Sharp) code: class="CSharp"
For Java code: class="java"
For Python code: class="python"
For C/C++ code: class="Cpp"
For Cascading Style Sheets code: class="CSS"
For Java Script code: class="JScript"
For Sql code: class="Sql"
For Xml code: class="Xml"

The list of supported languages and their aliases can be found at the syntaxhighlighter wiki

Note: If you are posting XML/HTML, anything which has < or >, replace them with < and >

Comments

Unknown said…
I have tried to follow your post but must be missing something. I uploaded the files to google docs but have no idea how change the code to reference those files instead of yours!
Amit Kumar said…
@evilgenius:

You need to replace
http://amitkumarmca04.blogspot.com with your url like http://evilgenius.com
I tried to follow your example but it did not work for me. May be I am missing something. You are telling to add the code at the end of tag. Which tag? Should I add it in at end of header section or body section or at the end of HTML root tag?

Popular posts from this blog

Sitecore GraphQL Queries

Twenty 20 Masti:

Sitecore Experience Edge GraphQL Queries