starlight_help/src/content/docs/code-blocks.mdx
import {TabItem, Tabs} from "@astrojs/starlight/components";
import FlattenedSteps from "../../components/FlattenedSteps.astro"; import KeyboardTip from "../../components/KeyboardTip.astro"; import NavigationSteps from "../../components/NavigationSteps.astro"; import ZulipTip from "../../components/ZulipTip.astro"; import AdminOnly from "../include/_AdminOnly.mdx"; import CodeBlocksExamples from "../include/_CodeBlocksExamples.mdx"; import CodeBlocksIntro from "../include/_CodeBlocksIntro.mdx"; import StartComposing from "../include/_StartComposing.mdx";
import CodeIcon from "~icons/zulip-icon/code";
<CodeBlocksIntro />Zulip's compose box has a smart code (<CodeIcon />) button, which inserts contextually appropriate code formatting:
```) formatting.`)
formatting.```)
formatting. 1. *(optional)* Select the text you want to format.
1. Click the **code** (<CodeIcon />) icon at the
bottom of the compose box to insert code formatting.
1. *(optional)* To enable syntax highlighting in a code bock, start typing the
name of the desired programming language directly after the initial ` ``` `.
Select the language from the auto-complete suggestions.
</FlattenedSteps>
<ZulipTip>
You can also use the **code** (<CodeIcon />)
icon to remove existing code formatting from the selected text.
</ZulipTip>
<KeyboardTip>
You can also use <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>
to insert code formatting.
</KeyboardTip>
1. To create an inline code span, use single backticks around the text:
`text`
To create a code block, use triple backticks around the text:
```
def f(x):
return x+1
```
To enable syntax highlighting, use triple backticks followed by one or more
letters, and select the desired programming language from the auto-complete
suggestions.
```python
def fib(n):
# TODO: base case
return fib(n-1) + fib(n-2)
```
</FlattenedSteps>
<ZulipTip>
You can also use `~~~` to start code blocks, or just indent the code 4 or more
spaces.
</ZulipTip>
Tagging a code block with a language enables syntax highlighting and (if configured) code playgrounds. Zulip supports syntax highlighting for hundreds of languages.
A code block can be tagged by typing the language name after the fence
(```) that begins a code block, as shown here. Typeahead will
help you enter the name for the language. The Short names values
from the Pygments lexer documentation are the
complete set of values that support syntax highlighting.
``` python
print("Hello world!")
```
Organization administrators can also configure a default language for code blocks, which will be used whenever the code block has no tag.
<AdminOnly /> <FlattenedSteps> <NavigationSteps target="settings/organization-settings" />When a default language is configured, one can use ```text to display
code blocks without any syntax highlighting (e.g., to paste an error message).
Code playgrounds are interactive in-browser development environments that are designed to make it convenient to edit and debug code. Code playgrounds can be configured for any programming language. Zulip code blocks that are tagged with the language will have a button visible on hover that allows you to open the code block in the code playground site.
For example, to configure code a playground for Rust, you could specify the
language and URL template as Rust and https://play.rust-lang.org/?code={code}.
When a code block is labeled as rust (either explicitly or by organization
default), users would get an on-hover option to open the code block in the
specified code playground.
Here is a list of playground URL templates you can use for some popular languages:
https://pythontutor.com/java.html#code={code} or
https://cscircles.cemc.uwaterloo.ca/java_visualize/#code={code}https://pythontutor.com/javascript.html#code={code}https://pythontutor.com/python-compiler.html#code={code}https://pythontutor.com/c.html#code={code}https://pythontutor.com/cpp.html#code={code}https://play.rust-lang.org/?code={code}py3 and py are mapped to
Python. One can use the typeahead (which appears when you type something
or just click on the language field) to look up the Pygments name.code variable in the URL template.
The URL template is required to contain exactly one variable named code.send_tweet could be
used with a "playground" that sends the content of the code block as a Tweet.If you have any trouble setting up a code playground, please contact us with details on what you're trying to do, and we'll be happy to help you out.