Help:Contributing/code

From Wikibase
Revision as of 12:27, 18 August 2026 by SeedBot (talk | contribs) (Create code-content guide (SyntaxHighlight enabled))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Languages: English · français · Esperanto

Code appears on this wiki in two places: as content on wiki pages (this page) and as data (code-snippet items created via Special:AddCodeSnippet). This page covers the former. The official MediaWiki formatting help covers the basics; this page covers what is special about code.

Code blocks on wiki pages

Description You type You get
Plain preformatted block — text exactly as typed, no wiki markup interpreted, HTML escaped. Safe for pasting raw code. <pre>print("hello")</pre>
print("hello")
Syntax-highlighted block — Pygments highlighting and a copy button (top right of the block). <syntaxhighlight lang="python">print("hello")</syntaxhighlight>
print("hello")
Inline code — monospace within a sentence. <code>print("hello")</code> print("hello")

Which languages can be highlighted

Any Pygments lexer alias works as the lang attribute — use the lowercase canonical name:

  • sparql — SPARQL queries (this wiki’s query service)
  • python, javascript, bash, sql, json, wikitext — commonly used
  • Full list: Pygments lexers (use the alias column)

The same language vocabulary is used by code-snippet items: a code item’s language is a Pygments language item, so the lang attribute on wiki pages and the language item in the data layer stay consistent.

Options for highlighted blocks

  • line — show line numbers
  • start — first line number (with line)
  • highlight — highlight specific lines (comma-separated)
  • inline — render as inline code instead of a block

Full parameter reference: SyntaxHighlight extension (official).

Differences from Markdown

  • There are no fenced code blocks — ``` does nothing. Use <pre> or <syntaxhighlight>.
  • <code> is raw HTML: the text inside is not escaped. Type &lt; for a literal <, &gt; for >, &amp; for &.
  • In <pre> and <syntaxhighlight> wiki markup is not interpreted — {{, [[ and quotes appear literally, which is what you want for code.
  • Blocks larger than 100 kB lose highlighting automatically (a safety guard).

House rules for code content

  • Prefer <syntaxhighlight lang="…"> for anything syntax-highlightable; use <pre> for config dumps and logs where colours add noise.
  • Use the canonical Pygments language name — the same vocabulary as the code-snippet items.
  • For the knowledge base itself, create code-snippet items via Special:AddCodeSnippet instead of dumping code into wiki pages: the item carries the language, description and provenance statements. See the contribution guide.
  • Cite your sources — code items should state where the snippet comes from (reference URL, stated in, retrieved).

See also