Code Block Macro
The Code Block macro allows you to display source code in your page with syntax highlighting, like the one below:
Add the Code Block Macro
- From the editor toolbar, choose Insert > Other Macros
- Find and select the required macro
Speed it up with autocomplete: Type { and the beginning of the macro name, to see a list of suggested macros. In this example we're inserting the cheese macro.
To edit an existing macro: Click the macro placeholder and choose Edit. This will open the macro details, so you can edit the macro parameters.
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
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 color scheme used for displaying your code block. Many of these themes are based on the default color schemes of popular integrated development environments (IDEs). The default theme is Confluence (also known as Default), which is typically black and colored text on a blank background. However, you can also choose from one of the following other popular themes:
|
Configure the Code Block macro
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:
- Choose the cog icon , then choose General Configuration
- Choose Configure Code Macro
- Select a Default Theme and Default Language
- Choose Save
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:
- Choose the cog icon , then choose General Configuration
- Choose Configure Code Macro
- Choose Add a new language
- Locate your language file and enter a Name for the new language (this will appear when selecting the language)
- Choose Add
Language files must be correctly formatted JavaScript files and adhere to the Custom Brush syntax.
To disable or remove a user-installed language:
- Choose the cog icon , then choose General Configuration
- Choose Manage Add-ons
- Go to User-installed Add-ons and locate the add-on for your uploaded language - it will appear like this 'Custom Code Macro Highlighting for...'
- Choose Uninstall or Disable
The language will no longer appear in the Code Macro.
Wiki markup example
This example is useful when you want to add a macro outside the editor, for example as custom content in the sidebar, header or footer of a space.
Macro name: code
Macro body: Accepts plain text.
{code:title=This is my title|theme=FadeToGrey|linenumbers=true|language=java|firstline=0001|collapse=true}
This is my code
{code}
Do more with Confluence
Extend Confluence with one of the hundreds of other macros in the Atlassian Marketplace. Some of our most popular include:
- Code Pro for Confluence - Get a real-time view of your code from any source in Confluence.
- Include Bitbucket Server for Confluence - Easily include code snippets in Confluence that sync automatically to Bitbucket Server.
- Markdown Extension for Confluence - Embed markdown from private and public Github & Bitbucket repositories in Confluence.