<html><head><link rel="canonical" href="https://www.jetbrains.com/help/writerside/code.html.md/" data-react-helmet="true"/></head><body># Code

With Writerside, you can format pieces of text as code, insert blocks of code, command input and output examples, and compare pieces of code.

## Inline code

Use inline formatting to wrap a piece of text as a variable, method, or function name, a command or option.

To insert some inline code, use the  [&lt;code&gt;](semantic-markup-reference.html#code)  element. In Markdown, use single backticks ```.

Semantic markup:

```XML
Call the <code>getAllItems()</code> method.
```

Markdown:

```PLAINTEXT
Call the `getAllItems()` method.
```

Call the `getAllItems()` method.

## Code blocks

Use code blocks to provide samples of source code, configuration files, command-line interaction, or REST API calls.

To add a code block, use the  [&lt;code-block&gt;](semantic-markup-reference.html#code-block)  element and specify the language in the `lang` attribute. In Markdown, use triple backticks ````` and specify the language directly after the opening backticks.

Semantic markup:

```XML
<code-block lang="java">
    class MyClass {
        public static void main(String[] args) {
            System.out.println("Hello, World");
        }
    }
</code-block>
```

Markdown:

```PLAINTEXT
```java
class MyClass {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
```
```

Result:

```JAVA
class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}
```

Your readers can copy the contents of the code block by clicking the ![Code-block Copy button icon](https://resources.jetbrains.com/help/img/writerside/copy-code-block.svg) icon.

&gt; **Note:**
&gt; Writerside uses [Prism](https://prismjs.com/) for code highlighting in the produced output. For a full list of supported languages, see [Supported languages](https://prismjs.com/#supported-languages).
&gt;
&gt;
&gt;
&gt; For highlighting and other assistance features in the editor, Writerside requires the corresponding language support plugin.

### Soft wraps

By default, Writerside renders code blocks without wrapping long lines. As a result, code blocks with long lines have a horizontal scrollbar. You can enable soft wraps for code blocks by setting  `[&lt;code-soft-wrap&gt;](buildprofiles-xml.html#code-soft-wrap)`  element in  `[buildprofiles.xml](buildprofiles-xml.html)`  to `true`.

```XML
<code-soft-wrap>true</code-soft-wrap>
```

This adds a switcher to code blocks in the rendered output, allowing readers to enable or disable soft wraps.

### XML and HTML code

If you need to provide a sample of XML or HTML code, wrap the contents of the code block with a [CDATA](https://en.wikipedia.org/wiki/CDATA) section. This will prevent Writerside from processing tags. You can also use the corresponding [intention action](intention-actions.html#wrap-with-cdata) for this.

```XML
<code-block lang="xml">
    <![CDATA[
        <some-tag>text in tag</some-tag>
    ]]>
</code-block>
```

&gt; **Tip:**
&gt; Writerside automatically wraps code blocks with CDATA if you type or paste code with special characters.

Alternatively, you can escape the `&lt;` and `&gt;` characters using `&lt;` and `&gt;`.

```PLAINTEXT
<code-block lang="xml">
&lt;some-tag&gt;text in tag&lt;/some-tag&gt;
</code-block>
```

### Links in code blocks

Specify links to topics and external URLs from code blocks as `[[[text|URL]]]`.

Semantic markup:

```XML
<code-block lang="java">
    class MyClass {
        public static void [[[main(String[] args)|https://en.wikipedia.org/wiki/Entry_point]]] {
            System.out.println("Hello, World");
        }
    }
</code-block>
```

Markdown:

```PLAINTEXT
```java
class MyClass {
public static void [[[main(String[] args)|https://en.wikipedia.org/wiki/Entry_point]]] {
System.out.println("Hello, World");
}
}
```
```

Result:

```JAVA
class MyClass {
    public static void [[[main(String[] args)|https://en.wikipedia.org/wiki/Entry_point]]] {
        System.out.println("Hello, World");
    }
}
```

To disable links and render `[[[text|URL]]]` as is, set [disable-links="true"](semantic-markup-reference.html#code-block_disable-links).

### Reference code from file

You can add files with code samples to a dedicated directory in your documentation project and use them in multiple code blocks. This is better than adding the same code samples literally and provides a single place for managing your code samples.

Procedure:

1. Specify the name of the directory with code samples in the  [&lt;snippets&gt;](writerside-cfg.html#snippets)  element of  `[writerside.cfg](writerside-cfg.html)` .

```
<snippets src="codeSnippets">
```

&gt; **Note:**
&gt; This directory must be in the [documentation project](projects.html). Although it can be a [symlink](https://en.wikipedia.org/wiki/Symbolic_link) to another directory, keep in mind that the files must be available during the build.

2. Add a file with code to the code snippets directory, for example:

```KOTLIN
@Test
fun testSum() {
    val expected = 42
    assertEquals(expected, testSample.sum(40, 2))
}

@Test
fun testMultiply() {
    val expected = 42
    assertEquals(expected, testSample.multiply(21, 2))
}
```

3. Use the [src](semantic-markup-reference.html#code-block_src) attribute to specify the file name:

Semantic markup:

```XML
<code-block lang="kotlin" src="newTest.kt">
```

Markdown:

```PLAINTEXT
```kotlin
```
{ src="newTest.kt" }
```

Result:

```KOTLIN
@Test
fun testSum() {
    val expected = 42
    assertEquals(expected, testSample.sum(40, 2))
}

@Test
fun testMultiply() {
    val expected = 42
    assertEquals(expected, testSample.multiply(21, 2))
}
```

Although referencing files from a dedicated directory is more convenient, you do not need to do it this way. Instead of registering the snippets directory, you can reference files using a path relative to the current topic file:

Semantic markup:

```XML
<code-block lang="kotlin" src="../code-samples/newTest.kt">
```

Markdown:

```PLAINTEXT
```kotlin
```
{ src="../code-samples/newTest.kt" }
```

You can also specify which parts of the code from the file you want in a code block. For example, use the [include-lines](semantic-markup-reference.html#code-block_include-lines) attribute to specify lines to include:

Semantic markup:

```XML
<code-block lang="kotlin" src="newTest.kt" include-lines="3">
```

Markdown:

```PLAINTEXT
```kotlin
```
{ src="newTest.kt" include-lines="3" }
```

Result:

```KOTLIN
val expected = 42
```

Alternatively, use the [include-symbol](semantic-markup-reference.html#code-block_include-symbol) attribute to specify the symbol to include, such as a function:

Semantic markup:

```XML
<code-block lang="kotlin" src="newTest.kt" include-symbol="testMultiply">
```

Markdown:

```PLAINTEXT
```kotlin
```
{ src="newTest.kt" include-symbol="testMultiply" }
```

Result:

```KOTLIN
@Test
fun testMultiply() {
    val expected = 42
    assertEquals(expected, testSample.multiply(21, 2))
}
```

### Compare code blocks

Use the  [&lt;compare&gt;](semantic-markup-reference.html#compare)  element to demonstrate the difference between two code blocks, for example, before and after refactoring.

```XML
<compare>
    <code-block lang="kotlin">
        if (true) {
            doThis()
        }
    </code-block>
    <code-block lang="kotlin">
        if (true) doThis()
    </code-block>
</compare>
```

Result:

Before:

```KOTLIN
if (true) {
    doThis()
}
```

After:

```KOTLIN
if (true) doThis()
```

</code-block></code-block></code-block></code-block></snippets></body></html>