Jump to content

Project:Code samples

From mediawiki.org

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 copy attribute.

For example:

<syntaxhighlight lang="text" copy>
Copy me
</syntaxhighlight>

results in:

Copy me

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