Using code highlighting with MediaWiki pages

From DiscordDigital Wiki
Revision as of 23:12, 27 February 2021 by Discorddigital (talk | contribs)
Jump to navigation Jump to search

Prologue

We're using an extension called SyntaxHighlight
This guide will show you how to use it and how to find the names for the languages, so SyntaxHighlight knows what it's highlighting.

HTML Syntax

Without line numbers

This one is useful if you expect the reader to copy the code.

<syntaxhighlight lang="apacheconf">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?title=$1 [NC,L,QSA]
</syntaxhighlight>
With line numbers

This one can be useful if you want to refer to a line of code.

<syntaxhighlight lang="apacheconf" line>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?title=$1 [NC,L,QSA]
</syntaxhighlight>

You can also specify on which line to start counting.

<syntaxhighlight lang="apacheconf" line start="55">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?title=$1 [NC,L,QSA]
</syntaxhighlight>

Finding language names

You can do so by going to this page: https://pygments.org/docs/lexers/

You'll want to search on the page for the language you want to highlight. Then look for the "Short names" and pick one.

Insert the short name in the "lang" parameter and the text inside the <syntaxhighlight> block will be highlighted accordingly.