Confluence 5.6 has reached end of life
Check out the [latest version] of the documentation
The Code Block macro allows you to display source code in your document with the appropriate syntax highlighting. The code block displays on the page as shown below:
public static void main(String[] args) { System.out.println("Hello World!"); }
To add the Code Block macro to a page:
Speeding up macro entry with autocomplete: To edit an existing macro: Click the macro placeholder and choose Edit. A macro dialog window will open, where you can edit the parameters of the macro.
You type the code block directly into the macro placeholder in the editor. Note that any white space contained in the placeholder is not manipulated in any way by the Code Block macro. This is to provide the writer with flexibility over code indentation.
Parameters are options that you can set to control the content or format of the macro output. Where the parameter name used in Confluence storage format or wikimarkup is different to the label used in the macro browser, it will be listed below in brackets (example
).
Parameter | Default | Description |
---|---|---|
Syntax highlighting | java | Specifies the language (or environment) for syntax highlighting. The default language is Java but you can choose from one of the following languages/environments:
|
Title | none | Adds a title to the code block. If specified, the title will be displayed in a header row at the top of the code block. |
Collapsible | false | If selected, the code macro's content will be collapsed upon visiting or refreshing the Confluence page. Clicking the |
Show line numbers | false | If selected, line numbers will be shown to the left of the lines of code. |
First line number | 1 | When Show line numbers is selected, this value defines the number of the first line of code. |
Theme | Default | Specifies the colour scheme used for displaying your code block. Many of these themes are based on the default colour schemes of popular integrated development environments (IDEs). The default theme is Confluence (also known as Default), which is typically black and coloured text on a blank background. However, you can also choose from one of the following other popular themes:
|
You can configure the Code Block macro to use a specific language and theme by default and also upload new lanuages. You need Confluence Administrator permissions to change the default theme and language and System Administrator permissions to upload new languages.
To set the default appearance of code blocks in your site:
All new code blocks will use the default theme and language unless you specify otherwise. Existing code blocks will be unchanged.
To add an additional language:
Language files must be correctly formatted JavaScript files and adhere to the Custom Brush syntax.
To disable or remove a user-installed language:
The language will no longer appear in the Code Macro.
The following examples are provided for advanced users who want to inspect or edit the underlying markup for a Confluence page.
Macro name: code
Macro body: Accepts plain text.
The following example shows all parameters and a body:
<ac:structured-macro ac:name="code"> <ac:parameter ac:name="title">This is my title</ac:parameter> <ac:parameter ac:name="theme">FadeToGrey</ac:parameter> <ac:parameter ac:name="linenumbers">true</ac:parameter> <ac:parameter ac:name="language">xml</ac:parameter> <ac:parameter ac:name="firstline">0001</ac:parameter> <ac:parameter ac:name="collapse">true</ac:parameter> <ac:plain-text-body><![CDATA[<b>This is my code</b>]]></ac:plain-text-body> </ac:structured-macro>
Below are three examples of the Code Block macro with various optional parameters used.
Description | Markup | What you will get |
---|---|---|
Code block macro with a body and no optional parameters | <ac:structured-macro ac:name="code"> <ac:plain-text-body><![CDATA[this is my code]]></ac:plain-text-body> </ac:structured-macro> | this is my code |
Code block macro with a body and the optional language parameter defined | <ac:structured-macro ac:name="code"> <ac:parameter ac:name="language">html/xml</ac:parameter> <ac:plain-text-body><![CDATA[this is my code]]></ac:plain-text-body> </ac:structured-macro> | this is my code |
Code block macro with a body and optional title, line numbers and language parameters defined | <ac:structured-macro ac:name="code"> <ac:parameter ac:name="title">This is my title</ac:parameter> <ac:parameter ac:name="linenumbers">true</ac:parameter> <ac:parameter ac:name="language">html/xml</ac:parameter> <ac:plain-text-body><![CDATA[this is my code]]></ac:plain-text-body> </ac:structured-macro> | This is my title this is my code |
{code:title=This is my title|theme=FadeToGrey|linenumbers=true|language=html/xml|firstline=0001|collapse=true} This is my code {code}