Pages

Tuesday, October 13, 2020

Markdown - Embedding Code Snippets In Code Snippet

 When tried to show how to insert code snippets in markdown, I found that I didn't know how to insert code sinppets in code snippet.

Helped by Google, I finally got the solution.

We can embed code snippets into Markdown text by:

  1. intending 4 or more spaces, or a tab as code block
  2. using triple backticks plus a syntax language code delimiter at the beginning and triple backticks at the end of code block

Interactive use of the two ways can help us write a demo of inserting code in markdown.

For example, we can intend tabs before backticks blocks:

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

The result is:

```java
public class MyApp {

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

And We can also intend tabs in backticks blocks:

```shell
    ```java
    public class MyApp {
    
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
        
    }    
    ```
```

The result:

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

The short of this way is that the tabs cannot be eliminated.

Add backticks pairs in backticks pairs will produce wrong format. Em, you can have a try.

Reference

Embedding Code Snippets

End!

No comments:

Post a Comment