Code Blocks in Your Content
One of the most common needs when documenting software is to write code blocks. The main element for this is programlisting
.
To add a programlisting
element:
-
Position the cursor at the start of the line where you want to add the code block.
-
Press Alt + Enter ⏎ (Windows) or Command ⌘ + Enter ⏎ (Mac) to display the Element Context Menu.
-
Search for the
programlisting
element and select it.When you add a
programlisting
element, a code snippet box appears in the topic. -
Add your code inside the code snippet box.
The programlisting
element is sometimes referred to as "verbatim", as the code you add inside the element will be rendered exactly how it appears in the editor. For example, it will keep spaces and line breaks to represent the code properly. Some other elements also work like this, such as
screen
and literallayout
.
Here, a javascript function has been added inside a programlisting
element. This is how the code looks in the output.
function myFunction() { 1 var x = "", i; for (i=0; i<5; i++) { x = x + "The number is " + i + "<br>"; } document.getElementById("demo").innerHTML = x; 2 }
In the sample shown , a code block is added inside the programlisting element. The javascript function contains (1) on the first line and (2) on the last line. These are callouts, which you can use to Annotate Code Blocks.