---
heading: 'Markdown code block syntax'
title: "Markdown Code block's basic and advanced syntaxes"
date: '2021-09-09'
lastmod: '2024-11-11'
icon: 'Markdown'
draft: false
summary: 'A detailed overview of Markdown code block syntaxes, covering both basic and advanced use cases.'
tags: ['markdown', 'code-block', 'syntax']
---

## Basic syntax

Use triple backticks to create a code block in markdown

````
```language
your code goes here
```
````

Example

````
```js
function debounce(func, timeout = 300) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => { func.apply(this, args); }, timeout);
  };
}
```
````

will appear as:

```js
function debounce(func, timeout = 300) {
  let timer
  return (...args) => {
    clearTimeout(timer)
    timer = setTimeout(() => {
      func.apply(this, args)
    }, timeout)
  }
}
```

## Filename

Add `:filename.ext` after `language` to render with filename label on the top of block

Example:

````
```java:GetRowValue.java
private String[] getRowValue(Integer i, List<BillDetail> billDetail) {
  String[] rowValue = new String[5];
  rowValue[0] = (i).toString();
  i = i - 1;

  rowValue[1] = billDetail.get(i).getTenSanPham();
  rowValue[2] = billDetail.get(i).getSoLuong().toString();
  rowValue[3] = new DecimalFormat("#,###").format(Integer.parseInt(billDetail.get(i).getDonGiaSanPham()));
  rowValue[4] = new DecimalFormat("#,###").format(billDetail.get(i).getThanhTien());
  return rowValue;
}
```
````

will appear as:

```java:GetRowValue.java
private String[] getRowValue(Integer i, List<BillDetail> billDetail) {
  String[] rowValue = new String[5];
  rowValue[0] = (i).toString();
  i = i - 1;

  rowValue[1] = billDetail.get(i).getProductName();
  rowValue[2] = billDetail.get(i).getQuantity().toString();
  rowValue[3] = new DecimalFormat("#,###").format(Integer.parseInt(billDetail.get(i).getProductPrice()));
  rowValue[4] = new DecimalFormat("#,###").format(billDetail.get(i).getSubTotal());

  return rowValue;
}
```

## Line highlighting and line-numbers

- Add `{numbers}` property after `language` to highlight line
- Add `showLineNumbers` property after `language` to render with line numbers

Example:

````
```html:index.html {1,4-6,10} showLineNumbers
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello world</title>
  </head>
  <body>
    <h1>Hello world</h1>
  </body>
</html>
```
````

will appear as:

```html:index.html {1,4-6,10} showLineNumbers
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Hello world</title>
  </head>
  <body>
    <h1>Hello world</h1>
  </body>
</html>
```
