Formatter: Highlight.js
Compatible with: R6.1 Current version: 0.1 Credits: WikiAdmin |
Repo: community/formatter/highlight/hljs.php
https://github.com/highlightjs/highlight.js
Highlight.js Documentation
Highlight.js is a syntax highlighter written in JavaScript. It works in the browser as well as on the server. It can work with pretty much any markup, doesn’t depend on any other frameworks, and has automatic language detection.
1. Usage
Call via (hljs [language])
%%(hljs php) source%%
2. Install
- download highlight.js
- place the
Highlight.js
lib anddefault.min.css
under the lib/highlight.js/ folder - add
hljs.php
highlighter to the formatter/highlight/ folder - add
hljs.tpl
template to the formatter/highlight/template/ folder - link the js and css file in the theme header or add the hljs action to the action/ folder
2.1. Formatter
<?php
// WackoWiki Wrapper for Highlight.js
// https://wackowiki.org/doc/Dev/PatchesHacks/Highlight.js
/*
%%(hljs [language])
content
%%
requires Highlight.js script <https://highlightjs.org/>
The Highlight.js script must be loaded once either via the {{hljs}} action or the theme header.
*/
// comment this out when you've set this statically in the theme header
if (!isset($this->hljs))
{
$this->hljs = true;
$tpl->action_name = 'hljs';
}
if ($options['_default'])
{
$language = $options['_default'];
if (!empty($language))
{
$class = match ($language)
{
'nohighlight' => 'nohighlight',
default => 'language-' . $language
};
}
if ($class)
{
$tpl->class = ' class="' . $class . '"';
}
}
$tpl->text = Ut::html($text);
formatter/highlight/template/hljs.tpl
[ === main === ] [ '' action '' ] <ignore> <!--notypo--> [ ' error ' ] <pre><code[ ' class ' ]>[ ' text | pre ' ]</code></pre> <!--/notypo--> </ignore> [ == action == ] <ignore><!--notypo--><!--action:begin-->[ ' name ' ]<!--action:end--><!--/notypo--></ignore>
2.2. Link to the library and one of the themes
The Highlight.js script and the stylesheet must be loaded once either dynamically via
- the
{{hljs}}
action (formatter sets action call) or - the theme header.
<?php
// add to theme/_common/_header.php (above $tpl->additions)
// highlight.js
$this->add_html('header', '<link rel="stylesheet" href="' . $this->db->base_path . 'js/highlight.js/default.min.css' . '">');
$this->add_html('footer', '<script src="' . $this->db->base_path . 'js/highlight.js/highlight.min.js' . '"></script>');
$this->add_html('footer', '<script>hljs.highlightAll();</script>');
or load them dynamically via a action set once only via formatter when needed.
<?php
if (!defined('IN_WACKO'))
{
exit;
}
// chose your favorite style (e.g. stackoverflow-dark.min.css)
$hljs_css = 'default.min.css';
// local
$hljs_styles = $this->db->base_path . 'js/highlight.js/' . $hljs_css;
$hljs_script = $this->db->base_path . 'js/highlight.js/highlight.min.js';
// CDN (see Highlight.js README.md)
#$hljs_styles = 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/styles/' . $hljs_css;
#$hljs_script = 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/es/highlight.min.js';
// highlight.js
$this->add_html('header', '<link rel="stylesheet" href="' . $hljs_styles . '">');
$this->add_html('footer', '<script src="' . $hljs_script . '"></script>');
$this->add_html('footer', '<script>hljs.highlightAll();</script>');
2.2.1. Fetch via CDN & Subresource Integrity
If you prefer Highlight.js via CDN replace the local resources accordingly.
If you are loading Highlight.js via CDN you may wish to use Subresource Integrity to guarantee that you are using a legimitate build of the library.
To do this you simply need to add the integrity
attribute for each JavaScript file you download via CDN. These digests are used by the browser to confirm the files downloaded have not been modified.
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js" integrity="sha384-d8KTBLFjg2IVNtohjj32TAh8+MOFPukj5WKbkfk83mLz1ctxujsIN0jnKaRw3rW7"></script>
2.3. Use custom CSS files
Chose a style like stackoverflow-dark.min.css
from the styles/
folder of the downloaded highlight.zip and replace default.min.css
with it. Don't forget to change the CSS file name in the action or your theme header accordingly.
2.4. Router configuration
Add the js/highlight.js directory to your router.conf if you hosting your own copy of Highlight.js.
config/router.conf
diff --git a/src/config/router.conf b/src/config/router.conf index 6649fc2..24698d1 100644 --- a/src/config/router.conf +++ b/src/config/router.conf @@ -107,7 +107,7 @@ `^(theme/{}/css|theme/_common|admin/style)/{}$` _ok! // css `^image/(wikiedit/)?{}$` _ok! // icons `^theme/{}/icon/{}$` _ok! // icons -`^js/(lang/|photoswipe/)?{}$` _ok! // js +`^js/(lang/|photoswipe/|highlight.js/)?{}$` _ok! // js `^file/(global/){}$` _ok! // global uploads `^file/thumb/{}$` _ok! // global thumbs `^setup/(image|css)/{}$` _ok! unlock=1 // setup inlines
2.5. CSP
Add the domain of the CDN to style-src
and script-src
in your csp.custom.conf
if you do use a Highlight.js CDN server.
config/csp.custom.conf
style-src 'self' cdn.jsdelivr.net; script-src 'self' 'unsafe-inline' cdn.jsdelivr.net;
3. Supported Formats
The table below shows the full list of languages (and corresponding classes/aliases) supported by Highlight.js. Languages that list a Package below are 3rd party languages and are not bundled with the core library. You can find their repositories by following the links.
Note: The languages available will depend on how you've built or are included the library in your app. For example our default minified web build includes only ~40 popular languages.
# | Language | Aliases | Package |
---|---|---|---|
1 | 1C | 1c | |
2 | 4D | 4d | highlightjs-4d |
3 | ABAP | sap-abap, abap | highlight-sap-abap |
4 | ABNF | abnf | |
5 | Access logs | accesslog | |
6 | Ada | ada | |
7 | Apex | apex | highlightjs-apex |
8 | Arduino (C++ w/Arduino libs) | arduino, ino | |
9 | ARM assembler | armasm, arm | |
10 | AVR assembler | avrasm | |
11 | ActionScript | actionscript, as | |
12 | Alan IF | alan, i | highlightjs-alan |
13 | Alan | ln | highlightjs-alan |
14 | AngelScript | angelscript, asc | |
15 | Apache | apache, apacheconf | |
16 | AppleScript | applescript, osascript | |
17 | Arcade | arcade | |
18 | AsciiDoc | asciidoc, adoc | |
19 | AspectJ | aspectj | |
20 | AutoHotkey | autohotkey | |
21 | AutoIt | autoit | |
22 | Awk | awk, mawk, nawk, gawk | |
23 | Bash | bash, sh, zsh | |
24 | Basic | basic | |
25 | BBCode | bbcode | highlightjs-bbcode |
26 | Blade (Laravel) | blade | highlightjs-blade |
27 | BNF | bnf | |
28 | Brainfuck | brainfuck, bf | |
29 | C# | csharp, cs | |
30 | C | c, h | |
31 | C++ | cpp, hpp, cc, hh, c, h, cxx, hxx | |
32 | C/AL | cal | |
33 | Cache Object Script | cos, cls | |
34 | CMake | cmake, cmake.in | |
35 | COBOL | cobol, standard-cobol | highlightjs-cobol |
36 | Coq | coq | |
37 | CSP | csp | |
38 | CSS | css | |
39 | Cap’n Proto | capnproto, capnp | |
40 | Chaos | chaos, kaos | highlightjs-chaos |
41 | Chapel | chapel, chpl | highlightjs-chapel |
42 | Cisco CLI | cisco | highlightjs-cisco-cli |
43 | Clojure | clojure, clj | |
44 | CoffeeScript | coffeescript, coffee, cson, iced | |
45 | CpcdosC+ | cpc | highlightjs-cpcdos |
46 | Crmsh | crmsh, crm, pcmk | |
47 | Crystal | crystal, cr | |
48 | cURL | curl | highlightjs-curl |
49 | Cypher (Neo4j) | cypher | highlightjs-cypher |
50 | D | d | |
51 | Dafny | dafny | highlightjs-dafny |
52 | Dart | dart | |
53 | Delphi | dpr, dfm, pas, pascal | |
54 | Diff | diff, patch | |
55 | Django | django, jinja | |
56 | DNS Zone file | dns, zone, bind | |
57 | Dockerfile | dockerfile, docker | |
58 | DOS | dos, bat, cmd | |
59 | dsconfig | dsconfig | |
60 | DTS (Device Tree) | dts | |
61 | Dust | dust, dst | |
62 | Dylan | dylan | highlightjs-dylan |
63 | EBNF | ebnf | |
64 | Elixir | elixir | |
65 | Elm | elm | |
66 | Erlang | erlang, erl | |
67 | Excel | excel, xls, xlsx | |
68 | Extempore | extempore, xtlang, xtm | highlightjs-xtlang |
69 | F# | fsharp, fs | |
70 | FIX | fix | |
71 | Flix | flix | highlightjs-flix |
72 | Fortran | fortran, f90, f95 | |
73 | FunC | func | highlightjs-func |
74 | G-Code | gcode, nc | |
75 | Gams | gams, gms | |
76 | GAUSS | gauss, gss | |
77 | GDScript | godot, gdscript | highlightjs-gdscript |
78 | Gherkin | gherkin | |
79 | Glimmer and EmberJS | hbs, glimmer, html.hbs, html.handlebars, htmlbars | highlightjs-glimmer |
80 | GN for Ninja | gn, gni | highlightjs-GN |
81 | Go | go, golang | |
82 | Grammatical Framework | gf | highlightjs-gf |
83 | Golo | golo, gololang | |
84 | Gradle | gradle | |
85 | GraphQL | graphql | |
86 | Groovy | groovy | |
87 | GSQL | gsql | highlightjs-gsql |
88 | HTML, XML | xml, html, xhtml, rss, atom, xjb, xsd, xsl, plist, svg | |
89 | HTTP | http, https | |
90 | Haml | haml | |
91 | Handlebars | handlebars, hbs, html.hbs, html.handlebars | |
92 | Haskell | haskell, hs | |
93 | Haxe | haxe, hx | |
94 | High-level shader language | hlsl | highlightjs-hlsl |
95 | Hy | hy, hylang | |
96 | Ini, TOML | ini, toml | |
97 | Inform7 | inform7, i7 | |
98 | IRPF90 | irpf90 | |
99 | JSON | json | |
100 | Java | java, jsp | |
101 | JavaScript | javascript, js, jsx | |
102 | Jolie | jolie, iol, ol | highlightjs-jolie |
103 | Julia | julia, julia-repl | |
104 | Kotlin | kotlin, kt | |
105 | LaTeX | tex | |
106 | Leaf | leaf | |
107 | Lean | lean | highlightjs-lean |
108 | Lasso | lasso, ls, lassoscript | |
109 | Less | less | |
110 | LDIF | ldif | |
111 | Lisp | lisp | |
112 | LiveCode Server | livecodeserver | |
113 | LiveScript | livescript, ls | |
114 | LookML | lookml | highlightjs-lookml |
115 | Lua | lua | |
116 | Macaulay2 | macaulay2 | highlightjs-macaulay2 |
117 | Makefile | makefile, mk, mak, make | |
118 | Markdown | markdown, md, mkdown, mkd | |
119 | Mathematica | mathematica, mma, wl | |
120 | Matlab | matlab | |
121 | Maxima | maxima | |
122 | Maya Embedded Language | mel | |
123 | Mercury | mercury | |
124 | mIRC Scripting Language | mirc, mrc | highlightjs-mirc |
125 | Mizar | mizar | |
126 | MKB | mkb | highlightjs-mkb |
127 | MLIR | mlir | highlightjs-mlir |
128 | Mojolicious | mojolicious | |
129 | Monkey | monkey | |
130 | Moonscript | moonscript, moon | |
131 | N1QL | n1ql | |
132 | NSIS | nsis | |
133 | Never | never | highlightjs-never |
134 | Nginx | nginx, nginxconf | |
135 | Nim | nim, nimrod | |
136 | Nix | nix | |
137 | Oak | oak | highlightjs-oak |
138 | Object Constraint Language | ocl | highlightjs-ocl |
139 | OCaml | ocaml, ml | |
140 | Objective C | objectivec, mm, objc, obj-c, obj-c, objective-c | |
141 | OpenGL Shading Language | glsl | |
142 | OpenSCAD | openscad, scad | |
143 | Oracle Rules Language | ruleslanguage | |
144 | Oxygene | oxygene | |
145 | PF | pf, pf.conf | |
146 | PHP | php | |
147 | Papyrus | papyrus, psc | highlightjs-papyrus |
148 | Parser3 | parser3 | |
149 | Perl | perl, pl, pm | |
150 | Pine Script | pine, pinescript | highlightjs-pine |
151 | Plaintext | plaintext, txt, text | |
152 | Pony | pony | |
153 | PostgreSQL & PL/pgSQL | pgsql, postgres, postgresql | |
154 | PowerShell | powershell, ps, ps1 | |
155 | Processing | processing | |
156 | Prolog | prolog | |
157 | Properties | properties | |
158 | Protocol Buffers | protobuf | |
159 | Puppet | puppet, pp | |
160 | Python | python, py, gyp | |
161 | Python profiler results | profile | |
162 | Python REPL | python-repl, pycon | |
163 | Q# | qsharp | highlightjs-qsharp |
164 | Q | k, kdb | |
165 | QML | qml | |
166 | R | r | |
167 | Razor CSHTML | cshtml, razor, razor-cshtml | highlightjs-cshtml-razor |
168 | ReasonML | reasonml, re | |
169 | Rebol & Red | redbol, rebol, red, red-system | highlightjs-redbol |
170 | RenderMan RIB | rib | |
171 | RenderMan RSL | rsl | |
172 | RiScript | risc, riscript | highlightjs-riscript |
173 | Roboconf | graph, instances | |
174 | Robot Framework | robot, rf | highlightjs-robot |
175 | RPM spec files | rpm-specfile, rpm, spec, rpm-spec, specfile | highlightjs-rpm-specfile |
176 | Ruby | ruby, rb, gemspec, podspec, thor, irb | |
177 | Rust | rust, rs | |
178 | RVT Script | rvt, rvt-script | highlightjs-rvt-script |
179 | SAS | SAS, sas | |
180 | SCSS | scss | |
181 | SQL | sql | |
182 | STEP Part 21 | p21, step, stp | |
183 | Scala | scala | |
184 | Scheme | scheme | |
185 | Scilab | scilab, sci | |
186 | Shape Expressions | shexc | highlightjs-shexc |
187 | Shell | shell, console | |
188 | Smali | smali | |
189 | Smalltalk | smalltalk, st | |
190 | SML | sml, ml | |
191 | Solidity | solidity, sol | highlightjs-solidity |
192 | Splunk SPL | spl | highlightjs-spl |
193 | Stan | stan, stanfuncs | |
194 | Stata | stata | |
195 | Structured Text | iecst, scl, stl, structured-text | highlightjs-structured-text |
196 | Stylus | stylus, styl | |
197 | SubUnit | subunit | |
198 | Supercollider | supercollider, sc | highlightjs-supercollider |
199 | Svelte | svelte | highlightjs-svelte |
200 | Swift | swift | |
201 | Tcl | tcl, tk | |
202 | Terraform (HCL) | terraform, tf, hcl | highlightjs-terraform |
203 | Test Anything Protocol | tap | |
204 | Thrift | thrift | |
205 | Toit | toit | toit-highlight |
206 | TP | tp | |
207 | Transact-SQL | tsql | highlightjs-tsql |
208 | Twig | twig, craftcms | |
209 | TypeScript | typescript, ts, tsx | |
210 | Unicorn Rails log | unicorn-rails-log | highlightjs-unicorn-rails-log |
211 | VB.Net | vbnet, vb | |
212 | VBA | vba | highlightjs-vba |
213 | VBScript | vbscript, vbs | |
214 | VHDL | vhdl | |
215 | Vala | vala | |
216 | Verilog | verilog, v | |
217 | Vim Script | vim | |
218 | X# | xsharp, xs, prg | highlightjs-xsharp |
219 | X++ | axapta, x++ | |
220 | x86 Assembly | x86asm | |
221 | XL | xl, tao | |
222 | XQuery | xquery, xpath, xq | |
223 | YAML | yml, yaml | |
224 | ZenScript | zenscript, zs | highlightjs-zenscript |
225 | Zephir | zephir, zep |
4. Basic Usage
The bare minimum for using highlight.js on a web page is linking to the library along with one of the themes and calling [highlightAll][1]
:
<link rel="stylesheet" href="/path/to/styles/default.min.css"> <script src="/path/to/highlight.min.js"></script> <script>hljs.highlightAll();</script>
This will find and highlight code inside of <pre><code>
tags; it tries to detect the language automatically. If automatic detection doesn’t work for you, or you simply prefer to be explicit, you can specify the language manually in the using the class
attribute:
<pre><code class="language-html">...</code></pre>
4.1. Plaintext Code Blocks
To apply the Highlight.js styling to plaintext without actually highlighting it, use the plaintext
language:
<pre><code class="language-plaintext">...</code></pre>
4.2. Ignoring a Code Block
To skip highlighting of a code block completely, use the nohighlight
class:
<pre><code class="nohighlight">...</code></pre>
5. Changelog
0.1 Initial version
0.2
6. ToDo
- test formatter and settings
- improve code and documentation
Referring pages: