This page looks best with JavaScript enabled
⚠️

【Hugo】zzoのコードブロックをカスタマイズ

 ·  ☕ 6 分で読めます
✏️

zzoとは?

Hugoのテーマの一つ。
猫が可愛い。
最初から多言語対応していたりカラー選択ができたり、リッチコンテンツやマークダウンの設定がされているのが特徴。

Hugo Theme Zzo | Hugo Themes

とはいえ……

rubycode

(´-`).。oO(Codeとかrbとかの表示はいらないよなあ)
(´-`).。oO(拡張子を識別できなかったときの表示が格好悪いな……)

Qiita のように、必要なときだけヘッダーが表示されるようにしたかったので、試行錯誤してみました。

シンタックスハイライトの拡張子の判定

Hugoのシンタックスハイライトは、ファイル名の.の最後を読み取っている。
VScodeのマークダウンのシンタックスハイライトはファイル名の前の:を読み取っている。
というわけで、基本的にエディタで書くときは:の前、表示は.の後を見ている。

今回は、どちらも書くという想定で、マークダウンのときに読み取る拡張子:ファイル名..記事上でハイライトにしたい拡張子にする。
そして、記事上では:の前や..以降の文字列は表示させない。

完成品

多少、タイトルの表示などもアレンジした。

rubycode

実装

元コードはそれぞれ、
themes/zzo/layouts/partials/script/codeblock-script.html
themes/zzo/assets/sass/pages/_single.scss
からコピーする。
以下のように対応したディレクトリに保存することで既存のテーマをカスタマイズできる。

なお、既にいろいろカスタマイズしているので、参考にする場合は元コードと見比べてください。
また、実装の汚さはプライスレスです。動けばいい精神です。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<script>
  "use strict";
  // ====================== markdown code block ======================
  function wrap(el, wrapper) {
    el.parentNode.insertBefore(wrapper, el);
    wrapper.appendChild(el);
  }

  (function () {
    var singleContentsElem = document.querySelector(".single__contents");
    singleContentsElem
      ? singleContentsElem
          .querySelectorAll("pre > code")
          .forEach(function (elem) {
            var dataLang = elem.getAttribute("data-lang");

            // タイトルなし
            if (!dataLang) {
              // ```の後に何もないとき
              elem.parentElement.setAttribute("data-lang", "none");
              return;
            } else if (!dataLang.includes(".")) {
              // 言語のみ指定のとき
              elem.className = "language-" + dataLang;
              elem.removeAttribute("data-lang");
              return;
            }

            // タイトルあり
            elem.parentElement.parentElement.parentElement.setAttribute(
              "data-titled",
              true
            );
            var code = null;
            var codeTitle = null;
            if (dataLang.includes("..")) {
              // 拡張子以外の言語を強制する場合
              code = dataLang.split("..")[1];
              codeTitle = dataLang.split("..")[0].split(":")[1];
            } else if (dataLang.includes(":")) {
              // 普通の拡張子のとき
              code = dataLang.split(":")[0];
              codeTitle = dataLang.split(":")[1];
            } else {
              // :がなくて拡張子のみのとき
              code = dataLang.split(".")[1];
              codeTitle = dataLang;
            }
            elem.className = "language-" + code;
            elem.setAttribute("data-lang", codeTitle);
          })
      : null;
  })();

  var langCodeElem = document.querySelectorAll(".language-code");
  langCodeElem
    ? langCodeElem.forEach(function (elem) {
        var newElem = document.createElement("span");
        newElem.className = "copy-to-clipboard";
        newElem.setAttribute("title", "Copy to clipboard");
        elem.append(newElem);
      })
    : null;
  // =================================================================
</script>
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
.single {
  padding: 1rem;
  position: relative;
  width: 100%;
  overflow-wrap: break-word;

  &__title {
    font-size: 2.5rem;
    font-weight: 900;
    font-family: $title-font;
    line-height: 3rem;
    overflow-wrap: break-word;
    margin-top: 1.5rem;
    margin-bottom: 0.5rem;
    
    @include themify($themes) {
      color: themed("single-header-title-color");
    }

    &[data-ani="true"] {
      @include animation('slide-in-down .2s .3s 1 ease-in both');
    }
  }

  &__shorttitle {
    font-size: 1.5rem;
    font-family: $title-font;
    overflow-wrap: break-word;
    margin-bottom: 0.25rem;
    opacity: 0.7;

    @include themify($themes) {
      color: themed("single-header-title-color");
    }
  }

  &__meta {
    font-size: 0.8rem;
    margin-bottom: 2rem;

    @include flexbox();
    @include align-items(center);
    @include flex-wrap(wrap);
    @include flex-grow(1);
    @include themify($themes) {
      color: themed('meta-color');
    }
    @media only screen and (max-width: 769px) {
      @include flexbox();
      @include flex-direction(column);
      @include align-items(flex-start);
    }
  }

  &__infos {
    margin-right: 0.5rem;

    @include flexbox();
    @include flex-wrap(wrap);
  }

  &__info {
    word-break: keep-all;
    padding: 0 0.125rem;
  }

  &__nojs {
    width: 100%;
    color: red;
    padding: 0.5rem 0;
  }

  &__contents {
    &[data-ani="true"] {
      @include animation('slide-in-left-little .2s .6s 1 ease-in both');
    }
    
    &--gallery {
      overflow: hidden;
    }

    & > p > a {
      text-decoration: underline;
    }

    margin: 1rem 0;
    font-size: 1rem;
    line-height: 1.7;
    width: inheirt;
    max-width: inherit;
    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
      position: relative;      
      line-height: 1.25;
      font-family: $title-font;
    }

    h1,
    h2 {
      padding: 0;
      color: inherit;
      font-weight: 900;
      text-rendering: optimizeLegibility; 

      @include themify($themes) {
        color: themed("single-contents-title-color");
      }
    }

    h3,
    h4,
    h5,
    h6 {
      @include themify($themes) {
        color: themed("single-contents-subtitle-color");
      }
    }

    h1 {
      font-size: 2.6rem;
      margin: 3.5rem 0 1.75rem 0;
    }

    h2 {
      font-size: 2.2rem;
      margin: 3.2rem 0 1.5rem 0;
    }

    h3 {
      font-size: 1.8rem;
      margin: 2.8rem 0 1.25rem 0;
    }

    h4 {
      font-size: 1.5rem;
      margin: 2.4rem 0 1rem 0;
    }

    h5 {
      font-size: 1.2rem;
      margin: 2rem 0 0.8rem 0;
    }

    h6 {
      font-size: 1rem;
      margin: 1.5rem 0 0.5rem 0;
    }

    pre {
      padding: 34px 12px 8px;
      overflow: auto;
      border-radius: 0.34rem;
      line-height: 1.5;
      font-size: 13.8px;
      direction: ltr;
      
      @include themify($themes) {
        @include webkit-scrollbars(themed('custom-scrollbar-foreground-color'), themed('custom-scrollbar-background-color'));
        @include moz-scrollbars(themed('custom-scrollbar-foreground-color'), themed('custom-scrollbar-background-color'));
      }
    }

    pre:not(.chroma) {
      position: relative;
    }

    div.chroma {
      position: relative;

      @include on-event {
        .copy-to-clipboard {
          opacity: 1;
        }
      }
    }
    
    pre > code:not([class^=language]) {
      &::before {
        position: absolute;
        top: 0;
        left: 0;
        padding: 2px 7px;
        width: 100%;
        height: 30px;
        z-index: z('content');
        line-height: 30px;
        font-size: 13.8px;
        font-family: $title-font;
        font-weight: bold;
        display: inline-block;
        text-transform: capitalize;      
        content: 'Code';

        @include themify($codeblock) {
          color: themed('content-pre-header-color');
          background: themed('content-pre-header-background-color');
        }
      }
    }

    p > code:not([class^=language]) {
      padding: 0.25rem 0.5rem;
      @include themify($codeblock) {
        color: themed('content-code-color');
      }
    }

    code + .copy-to-clipboard {
      position: absolute;
      right: 4px;
      top: 5px;
      border-radius: 2px;
      z-index: z('clipboard');
      @include transition(all, 0.2s, ease);
    }

    .copy-to-clipboard {
      background-position: 50% 50%;
      background-size: 16px 16px;
      background-repeat: no-repeat;
      width: 27px;
      height: 1.45rem;
      top: -1px;
      vertical-align: middle;
      position: relative;      
      margin-left: -0.2rem;
      cursor: pointer;
      border-radius: 0 2px 2px 0;
      margin-bottom: 1px;
      opacity: 0;

      @include clippy();
      @include transition(all, 0.2s, ease);
    }
    .copy-to-clipboard:hover {
      @include translateY(-0.1rem);
    }
    .chroma .copy-to-clipboard {
      position: absolute;
      right: 4px;
      top: 5px;      
      border-radius: 2px;
      z-index: z('clipboard');
      @include transition(all, 0.2s, ease);
    }
    .chroma .copy-to-clipboard:hover {
      @include translateY(-0.1rem);
    }
    .language-code .copy-to-clipboard {
      position: absolute;
      right: 4px;
      top: 5px;      
      border-radius: 2px;
      z-index: z('clipboard');
      @include transition(all, 0.2s, ease);
    }
    .language-code .copy-to-clipboard:hover {
      @include translateY(-0.1rem);
    }
    .highlight > .copy-to-clipboard {
      position: absolute;
      right: 4px;
      top: 5px;      
      border-radius: 2px;
      z-index: z('clipboard');
      @include transition(all, 0.2s, ease);
    }
    .highlight > .copy-to-clipboard:hover {
      @include translateY(-0.1rem);
    }

    blockquote {
      font-size: 1.05rem;
      line-height: 1.75;
      color: inherit;
      opacity: 0.8;
      position: relative;

      code:not([class^=language]) {
        padding: 3px 7px;
        @include themify($codeblock) {
          color: themed('content-code-color');
        }
      }
    }

    img {
      display: block;
      border-radius: 0.25rem;
      margin: 0 auto;
    }

    p {
      margin: 0 0 1.75rem 0;
      padding: 0;      

      code:not([class^=language]) {
        padding: 3px 7px;
        @include themify($codeblock) {
          color: themed('content-code-color');
        }
      }
    }

    ul {
      margin-right: 0;
      margin-top: 0;
      padding: 0;
      list-style: disc outside none;
    }

    &[data-dir="rtl"] {
      li {
        margin-right: 2rem;
      }

      blockquote {
        margin: 0 1.5rem 1.75rem 1.75rem;
        padding: 0 1.42188rem 0 0;

        @include themify($themes) {
          border-right: 0.32813rem solid themed("single-blockquote-border-color");
        }
      }
    }

    &[data-dir="ltr"] {
      li {
        margin-left: 2rem;
      }

      blockquote {
        margin: 0 1.75rem 1.75rem 1.5rem;
        padding: 0 0 0 1.42188rem;

        @include themify($themes) {
          border-left: 0.32813rem solid themed("single-blockquote-border-color");
        }
      }
    }

    li {
      margin-bottom: calc(1rem / 2);

      & > code {
        padding: 2px 7px;
      }

      code:not([class^=language]) {
        @include themify($codeblock) {
          color: themed('content-code-color');
        }
      }

      p {
        margin-bottom: 0.5rem;
      }
    }

    hr {
      margin: 0 0 calc(1.25rem - 1px) 0;
      padding: 0;
      border: none;
      height: 1px;
      @include themify($themes) {
        background: themed("single-hr-background-color");
      }
    }

    dl dt::after {
        content: ':';
    }

    dd {
      display: block;
      margin-inline-start: 40px;
    }

    .anchor {
      cursor: pointer;
    }

    a.footnote-ref {
      font-size: 0.75rem;
      font-weight: bold;
      margin-left: 3px;

      &::before {
        content: "[";
      }

      &::after {
        content: "]";
      }
    }

    .table-wrapper {
      overflow-x: auto;      

      > table {
        max-width: 100%;
        margin: 10px 0;
        border-spacing: 0;
        box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.125);

        
        @include themify($codeblock) {
          thead {
            background: themed('content-pre-header-background-color');
          }

          th {
            color: themed('content-pre-header-color');            
          }

          th, td {
            padding: 0.25rem 0.5rem;
            border: 1px double themed('content-pre-border-background-color');
          }
        }
      }

      code:not([class^=language]) {
        padding: 3px 7px;
        @include themify($codeblock) {
          color: themed('content-code-color');
        }
      }
    }
  }

  &__tags {
    list-style-type: none;
    @include flexbox();
    @include align-items(center);
    @include flex-wrap(wrap);
    @include flex-grow(1);
  }

  &__tag {
    padding: 0 0.25rem;
  }
}

code, pre {  
  padding: .5rem 0;
  line-height: 1.5;
  font-size: 13.8px;
  font-family: $code-font-stack;
  overflow: auto;

  a {
    text-decoration: none !important;
  }
}

*:not(.chroma) {
  code, pre {
    @include themify($codeblock) {
      color: themed('content-pre-color');
      background: themed('content-pre-background-color');
    }
  }

  code {
    padding: 0;
  }
}

code:not([class^=language]) {  
  padding: 3px 0;
  border-radius: 4px;
  @include themify($codeblock) {
    color: themed('content-code-color');
  }
}

pre:not(.chroma) {
  overflow: auto;
}

.single__contents > .language-code, li > .language-code {
  overflow-x: auto;
  position: relative;
  margin: 1rem 0;
  direction: ltr;

  @include on-event {
    .copy-to-clipboard {
      opacity: 1;
    }
  }

  &::after {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    padding: 2px 7px;
    width: 100%;
    height: 30px;
    z-index: z('toc');
    border-top-left-radius: 0.25rem;
    border-top-right-radius: 0.25rem;
    content: '';

    @include themify($codeblock) {
        color: themed('content-pre-header-color');
        background: themed('content-pre-header-background-color');
    }
  }
}

.highlight {
  position: relative;

  @include on-event {
    .copy-to-clipboard {
      opacity: 1;
    }
  }
}

.highlight > .chroma {
  margin: 1em 0;
  border-radius: 5px;
  box-shadow: 1px 1px 2px rgba(0,0,0,0.125);
  z-index: z('content');
  overflow-x: auto;
  direction: ltr;
  position: relative;

  @include themify($codeblock) {
    color: themed('content-pre-color');
    background: themed('content-pre-background-color');
  }

  code {
    padding: 0;
  }
  
  code[data-lang] {
    &::before {
      position: absolute;
      top: 0;
      left: 0;
      z-index: z('grid');
      padding: 2px 7px;
      width: 100%;
      height: 30px;
      line-height: 1.9;
      font-size: 13.8px;
      font-family: $title-font;
      font-weight: bold;
      display: inline-block;
      text-transform: capitalize;      
      content: attr(data-lang);

      @include themify($codeblock) {
        color: themed('content-pre-header-color');
        background: themed('content-pre-header-background-color');
      }
    }
  }

  table, tr, td {
    margin: 0;
    padding: 0;
    width: 100%;
    border-collapse: collapse;
  }

  .lntd {
    &:first-child {
      width: 10px;
      @include no-select;

      pre {
        margin: 0;
        padding: 34px 4px 6px;
      }
    }

    &:last-child {
      vertical-align: top;

      pre {
        margin: 0;
        padding: 34px 4px 6px;
      }
    }
  }

  /* LineNumbersTable */  
  .lnt {
    @include themify($codeblock) {
      color: themed('content-pre-number-color');
    }
  }

  table.lntable {
    overflow-x: auto;
    @include themify($themes) {
      @include webkit-scrollbars(themed('custom-scrollbar-foreground-color'), themed('custom-scrollbar-background-color'));
      @include moz-scrollbars(themed('custom-scrollbar-foreground-color'), themed('custom-scrollbar-background-color'));
    }
  }
}

li .highlight > .chroma {
  .lnt:first-child {
    padding: 0 0.3rem;
  }
  .lnt:not(:first-child) {
    padding: 0 0.3rem;
  }
}

td:not(.lntd) {
  code {
    padding: 2px 7px !important;
  }
}

table:not(.lntable) {
  td {
    code {
      padding: 2px 7px !important;
    }
  }
}

一覧表示

```
何もなし
```
何もなし
1
2
3
4
5
6
```rb
# 拡張子指定のみ(rb)
puts "test_#{num}"
```
# 拡張子指定のみ(rb)
puts "test_#{num}"
```rb:test.log
ハイライトがない拡張子
```
ハイライトがない拡張子
1
2
3
4
5
6
```rb:test.rb
# 拡張子・言語指定あり
puts "test_#{num}"
```
# 拡張子・言語指定あり
puts "test_#{num}"
1
2
3
4
5
6
```test.rb
# 拡張子のみ
puts "test_#{num}"
```
# 拡張子のみ
puts "test_#{num}"
1
2
3
4
5
6
```rb:テスト..rb
# 言語指定のみ
puts "test_#{num}"
```
# 言語指定のみ
puts "test_#{num}"
1
2
3
4
5
6
```rb:test.erb..rb
# 拡張子を強制する
<%= render '_comment' %>
```
# 拡張子を強制する
<%= render '_comment' %>

シンタックスハイライト非表示時はワンクリックでクリップボードにコピーができませんが、そこはまあいいでしよう……。

参考

Share on

END
END
@aiandrox

 
目次