Project:Code samples

This page describes best practices for including code samples on pages on MediaWiki.org

Using SyntaxHighlight edit

You can use ‎<syntaxhighlight> tags to format code samples with language-specific syntax highlighting. For a list of supported languages and required parameters, see Extension:SyntaxHighlight.

Copy to clipboard edit

To add a copy button to a code sample, add class="tpl-copy".

For example:

<syntaxhighlight lang="text" class="tpl-copy">
Copy me
</syntaxhighlight>

results in:

Copy me

This functionality is provided through MediaWiki:Gadget-site-tpl-copy.js and MediaWiki:Gadget-site-tpl-copy.css.

Using the Codesample template edit

The Codesample template provides helpful features for formatting code samples, such as line numbers, filenames, and highlighting.

For example:

{{Codesample|name=metadata|lang=yaml|scheme=light|line=1|start=5|highlight=1,4|code=
  name: foo-redirect
  namespace: tool-foo
  labels:
    name: foo-redirect
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/permanent-redirect: https://bar.toolforge.org/$1$is_args$args
}}

results in:

metadata
name: foo-redirect
  namespace: tool-foo
  labels:
    name: foo-redirect
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/permanent-redirect: https://bar.toolforge.org/$1$is_args$args