como-usar-fragmentos-de-codigo-en-markdown

How to use code snippets in Markdown

  • 2 min

Code snippets are useful for inserting portions of code within text. We can perform the insertion inline or in its own block.

Syntax for Inline Code

Inline code snippets are useful for inserting small portions of code within text. For example, when mentioning a function or a keyword, you can highlight the code snippet directly in the text.

To include an inline code snippet in Markdown, use a single backtick (`) before and after the code.

For example, if we have the following markdown,

By doing `x = 10` we assign the value 10 to the variable `x`
Copied!

It will be rendered like this,

For example, x = 10 assigns the value 10 to the variable x.

In the rendering, the inline code snippet x = 10 appears with a monospaced font and the style defined for code snippets.

Block Code Snippets

The block code syntax is suitable when we need to include more extensive blocks of code, such as complete functions or long code examples.

To add a code block, we use three backticks (```) before and after the code block.

It is also possible to specify the programming language immediately after the first three backticks to enable syntax highlighting.

```language

code

```
Copied!

For example, the following Markdown snippet,

```python
def greet(name):
    print(f"Hello, {name}!")

greet("John")
```
Copied!

Rendered will be like this,

def greet(name):
    print(f"Hello, {name}!")

greet("Luis")
Copied!

Syntax Highlighting

Syntax highlighting is a feature that improves code readability by applying specific colors and styles to different elements of the programming language (such as keywords, variables, and comments).

It is not a standard Markdown feature, but rather is provided by the program you are using.

In the rendering, the code block will use the indicated language to display syntax highlighting, if the viewing platform supports highlighting for that language.