Wednesday 26 January 2022

Bloggerでコードを行番号付きで表示する方法

他の人たちのブログとか見てて、コードを表示する時にかっこよく表示されているのを見て、自分もやってみたいと思った。
で、ググって色々と調べてBloggerでコードを行番号つきで表示する簡単な方法をメモします。

1.まず、ThemeをHTMLでカスタマイズします。
2.↓のコードを<head>と</head>の間に入れてSaveします。私は<head>の次の行に入れました。
    <!-- Google-code-prettify -->
    <script src='https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js'/>
<!-- /Google-code-prettify -->
    <style>
pre.prettyprint ol.linenums &gt; li {
list-style-type: decimal;
}
</style>
    <style>
  pre.prettyprint ol{ 
  overflow: auto;
    white-space: pre;
  }
</style>

3.そして、ブログを投稿編集時にHTMLで編集するようにして、↓のコードを入れます。
<pre class="prettyprint linenums"> 
test code
test code2
test code3
</pre>

4.投稿を公開またはプレビューでチェック。
あら不思議、以下のような表示になりました。
 
test code
test code2
test code3

↓来て頂きありがとうございます。クリックしていただくと、励みになります。↓
にほんブログ村 PC家電ブログ ピュアオーディオへ
にほんブログ村
にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村

No comments:

Post a Comment