Help:Contributing/code: Difference between revisions
Marked this version for translation |
Carve out <code> for plain inline tokens without markup-significant characters, aligning with content-creation AGENTS.md (via update-page on MediaWiki MCP Server) |
||
| Line 29: | Line 29: | ||
<blockquote><translate> | <blockquote><translate> | ||
<!--T:12--> | <!--T:12--> | ||
You may see <tvar name="code"><syntaxhighlight lang="text" inline><code>…</code></syntaxhighlight></tvar> used for inline code by others. It does not escape HTML: text such as <tvar name="lt"><syntaxhighlight lang="text" inline><</syntaxhighlight></tvar> or <tvar name="amp"><syntaxhighlight lang="text" inline>&</syntaxhighlight></tvar> inside it is read as markup. Prefer <tvar name="shi"><syntaxhighlight lang="text" inline><syntaxhighlight inline></syntaxhighlight></tvar> | You may see <tvar name="code"><syntaxhighlight lang="text" inline><code>…</code></syntaxhighlight></tvar> used for inline code by others. It does not escape HTML: text such as <tvar name="lt"><syntaxhighlight lang="text" inline><</syntaxhighlight></tvar> or <tvar name="amp"><syntaxhighlight lang="text" inline>&</syntaxhighlight></tvar> inside it is read as markup. For short plain tokens with none of those characters (<tvar name="ex"><syntaxhighlight lang="text" inline>#</syntaxhighlight></tvar>, <tvar name="cmd"><syntaxhighlight lang="text" inline>quarto render</syntaxhighlight></tvar>) <tvar name="code2"><syntaxhighlight lang="text" inline><code></syntaxhighlight></tvar> is fine and lighter to render. Prefer <tvar name="shi"><syntaxhighlight lang="text" inline><syntaxhighlight inline></syntaxhighlight></tvar> whenever the snippet contains <tvar name="lt2"><syntaxhighlight lang="text" inline><</syntaxhighlight></tvar>, <tvar name="gt"><syntaxhighlight lang="text" inline>></syntaxhighlight></tvar> or <tvar name="amp2"><syntaxhighlight lang="text" inline>&</syntaxhighlight></tvar> (it escapes content) or you want syntax highlighting; never use <tvar name="code3"><syntaxhighlight lang="text" inline><code></syntaxhighlight></tvar> for blocks — use <tvar name="pre2"><syntaxhighlight lang="text" inline><pre></syntaxhighlight></tvar> or <tvar name="sh2"><syntaxhighlight lang="text" inline><syntaxhighlight></syntaxhighlight></tvar>. | ||
</translate></blockquote> | </translate></blockquote> | ||
Revision as of 16:47, 19 August 2026
Code appears on this wikibase 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.
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="sparql">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5
|
| Syntax-highlighted inline code — monospace within a sentence. | <syntaxhighlight lang="python" inline>print("hello")</syntaxhighlight> | print("hello")
|
Anything inside <syntaxhighlight> or <pre></pre> will not be interpreted. They are rendered literally.
You may see
<code>…</code>used for inline code by others. It does not escape HTML: text such as<or&inside it is read as markup. For short plain tokens with none of those characters (#,quarto render)<code>is fine and lighter to render. Prefer<syntaxhighlight inline>whenever the snippet contains<,>or&(it escapes content) or you want syntax highlighting; never use<code>for blocks — use<pre>or<syntaxhighlight>.
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)
Options for highlighted blocks
| Option | You type | You get |
|---|---|---|
line — show line numbers
|
<syntaxhighlight lang="sparql" line>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 . }</syntaxhighlight> |
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
start="{int}" — first line number (with line)
|
<syntaxhighlight lang="sparql" line start="5">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 . }</syntaxhighlight> |
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
highlight — highlight specific lines (comma-separated)
|
<syntaxhighlight lang="sparql" highlight="1,3">SELECT ?item WHERE { ?item wdt:P1 wd:Q6 . }</syntaxhighlight> |
SELECT ?item WHERE {
?item wdt:P1 wd:Q6 .
}
|
inline — render as inline code instead of a block
|
<syntaxhighlight lang="sparql" inline>LIMIT</syntaxhighlight> | LIMIT
|
copy — adds a button that copies the block
|
<syntaxhighlight lang="sparql" copy>SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5</syntaxhighlight> | SELECT ?item WHERE { ?item wdt:P1 wd:Q6 } LIMIT 5
|
inlineandcopyare mutually exclusive.
Full parameter reference: SyntaxHighlight extension parameters (official).
Differences from Markdown
| Markdown habit | On this wiki |
|---|---|
Fenced code blocks (```)
|
There are no fenced blocks — ``` does nothing. Use <pre> or <syntaxhighlight>.
|
Literal [[ and {{ inside a fenced block
|
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.
See also
- MediaWiki syntax 101 (official)
- SyntaxHighlight extension (official)
- Pygments lexers — full language list
- Help:Contributing — contributing to this wiki