Styling code in Blogger posts
On this plattform (Blogger) I wanted to style the code blocks.
I found one javascript formatter called highlight.js, and it can be found on this link:https://highlightjs.org/ .
To implement this I had to go to the admin-part. Select the menu [Themes], and under the selected theme it is a button named [Edit Html].
And in the window with the hml-code I paste in the following in the <head> section:
In line 2 you can change
This will apply style to all the
For this blog theme I have added an override style in [themes] [Customize] [Advanced] [CSS] like this:
This is becuse the font-size is a bit too large for code-examples.
And since I had i little problem with adding a code-block inside the pre-bloc for examples I descided to just use pre-tags. So the script for editing is then:
I found one javascript formatter called highlight.js, and it can be found on this link:https://highlightjs.org/ .
To implement this I had to go to the admin-part. Select the menu [Themes], and under the selected theme it is a button named [Edit Html].
And in the window with the hml-code I paste in the following in the <head> section:
<script src='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js'/> <link href='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles//vs.min.css' rel='stylesheet'/> <script>hljs.initHighlightingOnLoad();</script>
In line 2 you can change
vs.min.css
with other styles like github.min.css
or the darker vs2015.min.css
or ocean.min.css
. This will apply style to all the
<pre><code>//nnnn</code></pre>
.For this blog theme I have added an override style in [themes] [Customize] [Advanced] [CSS] like this:
pre { font-size: 90%!important;0 line-height:95%!important; }
This is becuse the font-size is a bit too large for code-examples.
And since I had i little problem with adding a code-block inside the pre-bloc for examples I descided to just use pre-tags. So the script for editing is then:
<script src='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js'/> <link href='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles//vs.min.css' rel='stylesheet'/> <script> document.addEventListener("DOMContentLoaded", (event) => { document.querySelectorAll("pre").forEach((block) => { hljs.highlightBlock(block); }); }); </script>
Comments
Post a Comment