Blogger.com: How to embed formatted code snippet in a blog post? [duplicate]
Asked Answered
R

6

6

I have a blog on blogger.com and would like to format my code example in my post.

What do I need to use to format the code snippet in my blog post?

Royalroyalist answered 14/2, 2019 at 20:45 Comment(2)
Check the post out here. I used it create this blogpostMistrust
sagapatra.in/2020/04/…Morphosis
M
2

Here is a Blogger.com blog with a tutorial post on how to do it Syntax Highlighting with Blogger Engine Either try using Hilite for converting the code to embedable one or use Gist - this would be the easiest solution.

If by any chance it's outdated solution for your needs then read this: Formatting code snippets for blogging on Blogger

Add syntax highlighter in your template just above the tag:

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script> 
<script language='javascript'> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>

And then just add the code snippet.

Mayle answered 14/2, 2019 at 20:59 Comment(1)
This works fine, when our blog also uses http instead of https protocol. Chrome blocks all the calls from secure host (https) to http calls. Is there any other choice to use syntax highlighters for https blogs.Ripe
S
2

The answer given by @cdev is correct but outdated. June, 2021 the code that is working for me is:

<link href='https://unpkg.com/@highlightjs/[email protected]/styles/default.min.css' rel='stylesheet'/>
<script src='https://unpkg.com/@highlightjs/[email protected]/highlight.min.js'/>
<script src='https://unpkg.com/@highlightjs/[email protected]/languages/dart.min.js'/>
<script>
    hljs.configure({cssSelector: &quot;code&quot;});
    hljs.highlightAll();
</script>

This previous code has to be added just before the <head> tag.

By the default the highlighted code has to be written under <pre><code></code></pre> if you also want to be able to hightlight code bettwen <code></code> you have to add this line hljs.configure({cssSelector: &quot;code&quot;});

This line <script src='https://unpkg.com/@highlightjs/[email protected]/languages/dart.min.js'/> will make dart code to be highlighted. If the language you are using is not highlighted by default you may need to add a similar line. Check the supported languages

You can see this article in my blog

Sorb answered 8/6, 2021 at 2:27 Comment(0)
T
1

Syntax highlighting depends on what programming language code you are trying to highlight. Different languages will have to be highlighted differently.

It's best to use a third party library to take care of that for you.

Here's a tutorial on how to do that in Blogger: How To Format Code Snippets In Blogger Posts

Torpid answered 19/7, 2020 at 10:31 Comment(0)
D
1

You can use Highlight JS.

Go to themes and edit html, place below codes in .

<link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/atom-one-dark-reasonable.min.css' rel='stylesheet'/>
<script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js'></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/r.min.js'></script>
<script>hljs.initHighlightingOnLoad();</script>

Write your code under <pre><code></code></pre>

Or you can use gist.github.com to create gists and copy embeded code to your blog.

Dibucaine answered 27/1, 2021 at 0:4 Comment(0)
O
0

You can try: Carbon

I'm using it, and it's the best & fast & editable tool that I used for this purpose.

Obtrusive answered 22/5, 2022 at 14:9 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewGrammer
N
-1

Syntax highlighting works perfectly at blogger

But I suggest you to NOT use Blogger, there are better alternatives available now a days (all free like blogger). proof: Search on google "Top Blogging sites", blogger will be seen NO Where.

Nl answered 25/6, 2021 at 2:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.