Language: EN

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

How to use code snippets in Markdown

Code snippets are useful for inserting portions of code within the 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 the 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`

Rendered it will look like this,

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

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

Code Snippets in Block

The block code syntax is suitable when we need to include larger 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

```

For example, the following Markdown snippet,

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

greet("John")
```

Rendered will be like this,

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

greet("John")

Syntax Highlighting

Syntax highlighting is a feature that enhances 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 functionality, but rather provided by the program you are using.

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