WordPressのテーマ「Cocoon」にはhighlight.jsが組み込まれており、ソースコードのハイライト表示ができます。ここでは、Cocoonの標準機能を使用したソースコードのハイライト表示のやり方を紹介します。プラグインの導入は不要です。
事前準備(Cocoonの設定)
WordPressの管理画面から「Cocoon設定」を開き、以下の設定を行います。
- 「コード」タブを選択
- 「ソースコードをハイライト表示」のチェックをON
- 「行番号表示」はお好みでONに。ONにすると行番号が表示されます。
- ライブラリは「軽量版」「全て」のどちらかを選択。
メジャーな言語は「軽量版」に含まれていますが、マイナーな言語を使う場合は「全て」にチェックを入れます。「軽量版」に含まれている言語は、ページ下の一覧を見てください。 - 「変更をまとめて保存」ボタンを押し、設定を完了。

記事の編集
ソースコードをハイライト表示するには、ブロックエディタで「コード」ブロックを挿入します。
コードブロック挿入後にソースコードを書き、ブロックの設定で「言語」の設定をします。
適した言語がない場合は「高度な設定」→「追加cssクラス」にhighlight.jsで定義されている言語のAliasを指定することで指定言語のハイライト表示が可能になります。
cssクラスに指定可能なAliasの一覧は下の表を見てください。
ハイライト表示可能な言語の一覧
highlight.js の languages フォルダにあるソースコード名が class に指定する言語の名前になっています。
highlight.js:https://github.com/highlightjs/highlight.js/tree/main/src/languages
highlight.jsでサポートしている言語は以下の通りです。(2022.01.28 時点)
「言語」は言語の名称、「Alias」はブロックエディタの追加CSSに指定可能な名称、「軽量版に含まれる」が○のものはCocoon設定のライブラリ「軽量版」に含まれる言語です。○が付いていない言語を使いたい場合は、Cocoon設定でライブラリに「全て」を選択してください。
言語の一覧(2022.01.28 時点)
| 言語 | Alias(Classに指定する文字列) | 軽量版に含まれる |
|---|---|---|
| 1C | 1c | |
| 4D | 4d | |
| ABAP | sap-abap, abap | |
| ABNF | abnf | |
| Access logs | accesslog | |
| Ada | ada | |
| Arduino (C++ w/Arduino libs) | arduino, ino | |
| ARM assembler | armasm, arm | |
| AVR assembler | avrasm | |
| ActionScript | actionscript, as | |
| Alan IF | alan, i | |
| Alan | ln | |
| AngelScript | angelscript, asc | |
| Apache | apache, apacheconf | ○ |
| AppleScript | applescript, osascript | |
| Arcade | arcade | |
| AsciiDoc | asciidoc, adoc | |
| AspectJ | aspectj | |
| AutoHotkey | autohotkey | |
| AutoIt | autoit | |
| Awk | awk, mawk, nawk, gawk | |
| Bash | bash, sh, zsh | ○ |
| Basic | basic | |
| BBCode | bbcode | |
| Blade (Laravel) | blade | |
| BNF | bnf | |
| Brainfuck | brainfuck, bf | |
| C# | csharp, cs | ○ |
| C | c, h | |
| C++ | cpp, hpp, cc, hh, c++, h++, cxx, hxx | ○ |
| C/AL | cal | |
| Cache Object Script | cos, cls | |
| CMake | cmake, cmake.in | |
| Coq | coq | |
| CSP | csp | |
| CSS | css | ○ |
| Cap’n Proto | capnproto, capnp | |
| Chaos | chaos, kaos | |
| Chapel | chapel, chpl | |
| Cisco CLI | cisco | |
| Clojure | clojure, clj | |
| CoffeeScript | coffeescript, coffee, cson, iced | ○ |
| CpcdosC+ | cpc | |
| Crmsh | crmsh, crm, pcmk | |
| Crystal | crystal, cr | |
| cURL | curl | |
| Cypher (Neo4j) | cypher | |
| D | d | |
| Dafny | dafny | |
| Dart | dart | |
| Delphi | dpr, dfm, pas, pascal | |
| Diff | diff, patch | ○ |
| Django | django, jinja | |
| DNS Zone file | dns, zone, bind | |
| Dockerfile | dockerfile, docker | |
| DOS | dos, bat, cmd | |
| dsconfig | dsconfig | |
| DTS (Device Tree) | dts | |
| Dust | dust, dst | |
| Dylan | dylan | |
| EBNF | ebnf | |
| Elixir | elixir | |
| Elm | elm | |
| Erlang | erlang, erl | |
| Excel | excel, xls, xlsx | |
| Extempore | extempore, xtlang, xtm | |
| F# | fsharp, fs | |
| FIX | fix | |
| Fortran | fortran, f90, f95 | |
| G-Code | gcode, nc | |
| Gams | gams, gms | |
| GAUSS | gauss, gss | |
| GDScript | godot, gdscript | |
| Gherkin | gherkin | |
| Glimmer and EmberJS | hbs, glimmer, html.hbs, html.handlebars, htmlbars | |
| GN for Ninja | gn, gni | |
| Go | go, golang | |
| Grammatical Framework | gf | |
| Golo | golo, gololang | |
| Gradle | gradle | |
| Groovy | groovy | |
| GSQL | gsql | |
| HTML, XML | xml, html, xhtml, rss, atom, xjb, xsd, xsl, plist, svg | ○ |
| HTTP | http, https | ○ |
| Haml | haml | |
| Handlebars | handlebars, hbs, html.hbs, html.handlebars | |
| Haskell | haskell, hs | |
| Haxe | haxe, hx | |
| High-level shader language | hlsl | |
| Hy | hy, hylang | |
| Ini, TOML | ini, toml | ○ |
| Inform7 | inform7, i7 | |
| IRPF90 | irpf90 | |
| JSON | json | ○ |
| Java | java, jsp | ○ |
| JavaScript | javascript, js, jsx | ○ |
| Jolie | jolie, iol, ol | |
| Julia | julia, julia-repl | |
| Kotlin | kotlin, kt | |
| LaTeX | tex | |
| Leaf | leaf | |
| Lean | lean | |
| Lasso | lasso, ls, lassoscript | |
| Less | less | |
| LDIF | ldif | |
| Lisp | lisp | |
| LiveCode Server | livecodeserver | |
| LiveScript | livescript, ls | |
| Lua | lua | |
| Makefile | makefile, mk, mak, make | ○ |
| Markdown | markdown, md, mkdown, mkd | ○ |
| Mathematica | mathematica, mma, wl | |
| Matlab | matlab | |
| Maxima | maxima | |
| Maya Embedded Language | mel | |
| Mercury | mercury | |
| mIRC Scripting Language | mirc, mrc | |
| Mizar | mizar | |
| MKB | mkb | |
| Mojolicious | mojolicious | |
| Monkey | monkey | |
| Moonscript | moonscript, moon | |
| N1QL | n1ql | |
| NSIS | nsis | |
| Never | never | |
| Nginx | nginx, nginxconf | ○ |
| Nim | nim, nimrod | |
| Nix | nix | |
| Object Constraint Language | ocl | |
| OCaml | ocaml, ml | |
| Objective C | objectivec, mm, objc, obj-c, obj-c++, objective-c++ | ○ |
| OpenGL Shading Language | glsl | |
| OpenSCAD | openscad, scad | |
| Oracle Rules Language | ruleslanguage | |
| Oxygene | oxygene | |
| PF | pf, pf.conf | |
| PHP | php | ○ |
| Papyrus | papyrus, psc | |
| Parser3 | parser3 | |
| Perl | perl, pl, pm | ○ |
| Pine Script | pine, pinescript | |
| Plaintext | plaintext, txt, text | |
| Pony | pony | |
| PostgreSQL & PL/pgSQL | pgsql, postgres, postgresql | |
| PowerShell | powershell, ps, ps1 | |
| Processing | processing | |
| Prolog | prolog | |
| Properties | properties | ○ |
| Protocol Buffers | protobuf | |
| Puppet | puppet, pp | |
| Python | python, py, gyp | ○ |
| Python profiler results | profile | |
| Python REPL | python-repl, pycon | |
| Q# | qsharp | |
| Q | k, kdb | |
| QML | qml | |
| R | r | |
| Razor CSHTML | cshtml, razor, razor-cshtml | |
| ReasonML | reasonml, re | |
| Rebol & Red | redbol, rebol, red, red-system | |
| RenderMan RIB | rib | |
| RenderMan RSL | rsl | |
| RiScript | risc, riscript | |
| Roboconf | graph, instances | |
| Robot Framework | robot, rf | |
| RPM spec files | rpm-specfile, rpm, spec, rpm-spec, specfile | |
| Ruby | ruby, rb, gemspec, podspec, thor, irb | ○ |
| Rust | rust, rs | |
| SAS | SAS, sas | |
| SCSS | scss | |
| SQL | sql | ○ |
| STEP Part 21 | p21, step, stp | |
| Scala | scala | |
| Scheme | scheme | |
| Scilab | scilab, sci | |
| Shape Expressions | shexc | |
| Shell | shell, console | ○ |
| Smali | smali | |
| Smalltalk | smalltalk, st | |
| SML | sml, ml | |
| Solidity | solidity, sol | |
| Splunk SPL | spl | |
| Stan | stan, stanfuncs | |
| Stata | stata | |
| Structured Text | iecst, scl, stl, structured-text | |
| Stylus | stylus, styl | |
| SubUnit | subunit | |
| Supercollider | supercollider, sc | |
| Svelte | svelte | |
| Swift | swift | |
| Tcl | tcl, tk | |
| Terraform (HCL) | terraform, tf, hcl | |
| Test Anything Protocol | tap | |
| Thrift | thrift | |
| TP | tp | |
| Transact-SQL | tsql | |
| Twig | twig, craftcms | |
| TypeScript | typescript, ts | |
| Unicorn Rails log | unicorn-rails-log | |
| VB.Net | vbnet, vb | |
| VBA | vba | |
| VBScript | vbscript, vbs | |
| VHDL | vhdl | |
| Vala | vala | |
| Verilog | verilog, v | |
| Vim Script | vim | |
| X# | xsharp, xs, prg | |
| X++ | axapta, x++ | |
| x86 Assembly | x86asm | |
| XL | xl, tao | |
| XQuery | xquery, xpath, xq | |
| YAML | yml, yaml | ○ |
| ZenScript | zenscript, zs | |
| Zephir | zephir, zep |



コメント