# code macro in Confluence storage format

URL: https://goingstellar.ro/reference/confluence-storage-format/code-macro/

The code macro shows source code in a monospace block with syntax highlighting. Its body is plain text in a CDATA section, not rich text. In ADF it becomes a codeBlock node, which holds only a language, so the display parameters do not travel.

## Storage format

```xml
<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">python</ac:parameter><ac:parameter ac:name="title">setup.py</ac:parameter><ac:parameter ac:name="linenumbers">true</ac:parameter><ac:parameter ac:name="collapse">true</ac:parameter><ac:parameter ac:name="theme">Midnight</ac:parameter><ac:parameter ac:name="firstline">10</ac:parameter><ac:plain-text-body><![CDATA[print("hi")]]></ac:plain-text-body></ac:structured-macro>
```

## In the editor

- Use the code snippet element from the menu or with the slash command. Per the Atlassian documentation.

## As ADF (Confluence Cloud)

The ADF that Confluence Cloud builds for:

```xml
<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">python</ac:parameter><ac:parameter ac:name="title">setup.py</ac:parameter><ac:parameter ac:name="linenumbers">true</ac:parameter><ac:parameter ac:name="collapse">true</ac:parameter><ac:parameter ac:name="theme">Midnight</ac:parameter><ac:parameter ac:name="firstline">10</ac:parameter><ac:plain-text-body><![CDATA[print("hi")]]></ac:plain-text-body></ac:structured-macro>
```

```json
{
 "type": "doc",
 "content": [
  {
   "type": "expand",
   "attrs": {
    "title": "setup.py"
   },
   "content": [
    {
     "type": "codeBlock",
     "attrs": {
      "language": "python"
     },
     "content": [
      {
       "text": "print(\"hi\")",
       "type": "text"
      }
     ]
    }
   ]
  }
 ],
 "version": 1
}
```

## Examples

Storage format sent:

```xml
<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">python</ac:parameter><ac:parameter ac:name="title">setup.py</ac:parameter><ac:parameter ac:name="linenumbers">true</ac:parameter><ac:parameter ac:name="collapse">true</ac:parameter><ac:parameter ac:name="theme">Midnight</ac:parameter><ac:parameter ac:name="firstline">10</ac:parameter><ac:plain-text-body><![CDATA[print("hi")]]></ac:plain-text-body></ac:structured-macro>
```

Set every documented display option at once. Confluence accepts and stores all six parameters.

Rendered view (HTML):

```html
<div class="code panel pdl conf-macro output-block" data-hasbody="true" data-macro-id="<id>" data-macro-name="code" style="border-width: 1px;">
 <div class="codeHeader panelHeader pdl hide-border-bottom" style="border-bottom-width: 1px;">
  <b class="code-title">setup.py</b><span class="collapse-source expand-control" style="display:none;"><span class="expand-control-icon icon"> </span><span class="expand-control-text">Expand source</span></span><span class="collapse-spinner-wrapper"> </span>
 </div>
 <div class="codeContent panelContent pdl hide-toolbar">
  <pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: python; gutter: true; first-line: 10; theme: Midnight; collapse: true" data-theme="Midnight">print(&quot;hi&quot;)</pre>
 </div>
</div>
```

Storage format after the round trip through ADF:

```xml
<ac:structured-macro ac:name="expand" ac:schema-version="1" ac:local-id="<id>" ac:macro-id="<id>"><ac:parameter ac:name="title">setup.py</ac:parameter><ac:rich-text-body><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="<id>"><ac:parameter ac:name="language">py</ac:parameter><ac:plain-text-body><![CDATA[print("hi")]]></ac:plain-text-body></ac:structured-macro></ac:rich-text-body></ac:structured-macro>
```

Sent to Confluence Cloud on 29 Nov 2026.

Storage format sent:

```xml
<ac:structured-macro ac:name="code"><ac:plain-text-body><![CDATA[no language]]></ac:plain-text-body></ac:structured-macro>
```

Write a block without a language. The block is stored as written and highlighted with the Java brush.

Rendered view (HTML):

```html
<div class="code panel pdl conf-macro output-block" data-hasbody="true" data-macro-id="<id>" data-macro-name="code" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme="Confluence">no language</pre>
</div></div>
```

Storage format after the round trip through ADF:

```xml
<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="<id>"><ac:plain-text-body><![CDATA[no language]]></ac:plain-text-body></ac:structured-macro>
```

Sent to Confluence Cloud on 29 Nov 2026.

Storage format sent:

```xml
<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">bash</ac:parameter><ac:plain-text-body><![CDATA[ls -la]]></ac:plain-text-body></ac:structured-macro>
```

Store a shell command. A named language is kept as given.

Rendered view (HTML):

```html
<div class="code panel pdl conf-macro output-block" data-hasbody="true" data-macro-id="<id>" data-macro-name="code" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: bash; gutter: false; theme: Confluence" data-theme="Confluence">ls -la</pre>
</div></div>
```

Storage format after the round trip through ADF:

```xml
<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="<id>"><ac:parameter ac:name="language">bash</ac:parameter><ac:plain-text-body><![CDATA[ls -la]]></ac:plain-text-body></ac:structured-macro>
```

Sent to Confluence Cloud on 29 Nov 2026.

Storage format sent:

```xml
<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">python</ac:parameter><ac:rich-text-body><p>rich body in code</p></ac:rich-text-body></ac:structured-macro>
```

Put rich text inside a code macro. The body is not used, so the block renders empty.

Rendered view (HTML):

```html
<div class="code panel pdl conf-macro output-block" data-hasbody="false" data-macro-id="<id>" data-macro-name="code" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: python; gutter: false; theme: Confluence" data-theme="Confluence"> </pre>
</div></div>
```

Storage format after the round trip through ADF:

```xml
<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="<id>"><ac:parameter ac:name="language">py</ac:parameter></ac:structured-macro>
```

Sent to Confluence Cloud on 29 Nov 2026.

Storage format sent:

```xml
ADF:{"content": [{"attrs": {"language": "python"}, "content": [{"text": "print(1)", "type": "text"}], "type": "codeBlock"}], "type": "doc", "version": 1}
```

Save an ADF codeBlock with a language. The language comes back as py.

Rendered view (HTML):

```html
<div class="code panel pdl conf-macro output-block" data-hasbody="true" data-macro-id="<id>" data-macro-name="code" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: py; gutter: false; theme: Confluence" data-theme="Confluence">print(1)</pre>
</div></div>
```

Storage format after the round trip through ADF:

```xml
<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="<id>"><ac:parameter ac:name="language">py</ac:parameter><ac:plain-text-body><![CDATA[print(1)]]></ac:plain-text-body></ac:structured-macro>
```

Sent to Confluence Cloud on 29 Nov 2026.

Storage format sent:

```xml
ADF:{"content": [{"attrs": {}, "content": [{"text": "no lang", "type": "text"}], "type": "codeBlock"}], "type": "doc", "version": 1}
```

Save an ADF codeBlock with empty attributes. No language parameter is written.

Rendered view (HTML):

```html
<div class="code panel pdl conf-macro output-block" data-hasbody="true" data-macro-id="<id>" data-macro-name="code" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: java; gutter: false; theme: Confluence" data-theme="Confluence">no lang</pre>
</div></div>
```

Storage format after the round trip through ADF:

```xml
<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="<id>"><ac:plain-text-body><![CDATA[no lang]]></ac:plain-text-body></ac:structured-macro>
```

Sent to Confluence Cloud on 29 Nov 2026.

## What changes in conversion

- ADF codeBlock stores a language and the text. theme, firstline and linenumbers have no place there and are lost.
- A code macro that has a title and collapse set is written to ADF as an expand node holding the code block, so it returns as an expand macro with the title and a bare code macro inside.
- The language python comes back as py. The text is kept in a CDATA plain-text-body.
- A code macro with an ac:rich-text-body instead of a plain-text-body keeps only its language, and the body text is gone.
- Parameter order is rewritten by Confluence on save, so compare values and not text.

## Pitfalls

- Put the code in a CDATA section inside ac:plain-text-body, as every stored example does.
- Do not use ac:rich-text-body for code. The run with rich text produced an empty block.
- A pasted block that needs line numbers or a theme will lose them as soon as the page is saved from the Cloud editor.

## Questions and answers

### What is the default language?

The documentation says Java, and the stored page used the Java brush when no language was given.

### Can the code macro be collapsed?

In storage format yes, with collapse set to true. After conversion it appears as an expand macro around the code.

## Related

- [code macro languages](https://goingstellar.ro/reference/confluence-storage-format/code-languages/)
- [noformat macro](https://goingstellar.ro/reference/confluence-storage-format/noformat/)
- [expand macro](https://goingstellar.ro/reference/confluence-storage-format/expand/)
- [info, note, tip and warning macros](https://goingstellar.ro/reference/confluence-storage-format/info-note-tip-warning/)

## Sources

- [Insert the code block macro (Atlassian Support)](https://support.atlassian.com/confluence-cloud/docs/insert-the-code-block-macro/)
- [Macros that are unavailable or have limited availability in the Cloud editor (Atlassian Support)](https://support.atlassian.com/confluence-cloud/docs/learn-which-macros-are-being-removed/)
- [Atlassian Document Format: document structure (Atlassian Developer)](https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/)

