Home/Jira formatting reference
Jira text formatting
{code} macro in Jira: syntax, options and ADF code block
{code} turns a block of text into a monospaced panel with syntax highlighting. It works in wiki markup fields and in comments written through REST API v2; the new editor stores the same thing as a codeBlock node in ADF. With no language given, Jira highlights the block as Java.
Wiki markup
{code}...{code}
{code:python}...{code}
{code:title=Bar.java|borderStyle=solid}...{code}
Markdown shortcuts in the editor
Per the Atlassian documentation, not tested.
| Type | What happens |
|---|---|
``` | Opens a code block in the editor as you type, per the Atlassian documentation. |
`text` | Inline monospace, per the Atlassian documentation. It is not the same thing as a code block. |
Stored as ADF (REST API v3)
The ADF document Jira Cloud stores for ADF:{"content": [{"attrs": {"language": "python"}, "content": [{"text": "print(1)", "type": "text"}], "type": "codeBlock"}], "type": "doc", "version": 1}:
{
"type": "doc",
"version": 1,
"content": [
{
"type": "codeBlock",
"attrs": {
"language": "python"
},
"content": [
{
"type": "text",
"text": "print(1)"
}
]
}
]
}
Examples
Wiki markup (API v2)
{code}
no language
{code}Shows the default. A block with no language is rendered as Java, and the stored ADF records java as the language, so the round trip writes it out.
Rendered by Jira (HTML)
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">no language
</pre>
</div></div>Back to wiki after the round trip
{code:java}no language{code}Sent to Jira Cloud on .
Wiki markup (API v2)
{code:title=Bar.java|borderStyle=solid}
// Some comments here
public String getFoo()
{
return foo;
}
{code}Adds a title bar and a solid border to a snippet. The title survives as a bold line above the block; the border style does not.
Rendered by Jira (HTML)
<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>Bar.java</b></div><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// Some comments here
</span><span class="code-keyword">public</span> <span class="code-object">String</span> getFoo()
{
<span class="code-keyword">return</span> foo;
}
</pre>
</div></div>Back to wiki after the round trip
*Bar.java*
{code:java}// Some comments here
public String getFoo()
{
return foo;
}{code}Sent to Jira Cloud on .
Wiki markup (API v2)
{code:xml}
<test>
<another tag="attribute"/>
</test>
{code}Highlights markup. The leading spaces of the first line are gone after the round trip while the later lines keep theirs.
Rendered by Jira (HTML)
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"> <span class="code-tag"><test></span>
<span class="code-tag"><another tag=<span class="code-quote">"attribute"</span>/></span>
<span class="code-tag"></test></span>
</pre>
</div></div>Back to wiki after the round trip
{code:xml}<test>
<another tag="attribute"/>
</test>{code}Sent to Jira Cloud on .
Wiki markup (API v2)
{code:java|linenumbers=true|collapse=true}
int a = 1;
{code}Tries line numbers and a collapsed block. Both options are accepted without an error, change nothing in the render and are not written back.
Rendered by Jira (HTML)
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-object">int</span> a = 1;
</pre>
</div></div>Back to wiki after the round trip
{code:java}int a = 1;{code}Sent to Jira Cloud on .
Wiki markup (API v2)
{code}
*not bold* inside code
{code}Keeps wiki syntax literal. Asterisks inside the macro stay asterisks.
Rendered by Jira (HTML)
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">*not bold* inside code
</pre>
</div></div>Back to wiki after the round trip
{code:java}*not bold* inside code{code}Sent to Jira Cloud on .
Wiki markup (API v2)
{code}inline code macro{code}Uses the macro on a single line. The result is still a block panel, not inline text; for inline monospace use double curly braces.
Rendered by Jira (HTML)
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">inline code macro</pre>
</div></div>Back to wiki after the round trip
{code:java}inline code macro{code}Sent to Jira Cloud on .
Wiki markup (API v2)
{code}
unclosed codeLeaves the macro open. The panel comes out empty and the text lands in a paragraph after it.
Rendered by Jira (HTML)
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"></pre>
</div></div>
<p>unclosed code</p>Back to wiki after the round trip
{code:java}{code}
unclosed codeSent to Jira Cloud on .
What changes in conversion
- The ADF codeBlock carries a language and the text, nothing else in these probes. Writing it back as wiki always names the language, so {code} becomes {code:java}.
- title is turned into a separate bold paragraph above the block. borderStyle, linenumbers and collapse have no place in the node and are dropped.
- The line break after the opening tag and before the closing tag is removed, so the block comes back on one line with the code inside the braces. Indentation on the first line is lost.
- A codeBlock with no language comes back as {noformat}, not {code}. Per the documentation the node can also hold wrap and hideLineNumbers, which wiki markup cannot express.
Pitfalls
- A language name the renderer does not know produces a red error line inside the panel instead of code. See the code-languages entry for the list Jira prints.
- Do not rely on
linenumbersorcollapse. They are silently ignored. - The closing {code} is required. Without it the macro swallows nothing and the panel is empty.
Questions and answers
What language does {code} use when I give none?
Java. The rendered HTML carries the class code-java and the stored ADF names java.
Related
- {code} languages
- {noformat} macro
- {panel} macro
- Markdown in Jira Cloud
- REST API v2 wiki markup vs v3 ADF