Markdown/Github: syntax highlighting of code block

2020-05-18 04:38发布

In Github/MD, if we want to enable code block when it is a child of list, we need to intent it by 8 spaces.

But how to make that code block has the syntax highlighting feature?

The following code does not work as expected...

    1. foo

             ```python
                print 'bar'
             ```

    2. bar

3条回答
狗以群分
2楼-- · 2020-05-18 05:13

To get code blocks with syntax highlighting embedded happily in a list, embed the markup lines that come before and after the code block to the appropriate level of indenting for an additional paragraph, then proceed as normal. For example:

1. lorem ipsum

  ```ruby
resources :dolor
  ```

   1. sit amet

      ```ruby
resources :elit
      ```

   1. sed do

1. eiusmod

indents each code block to the appropriate depth and maintains the integrity of the indexes.

查看更多
何必那么认真
3楼-- · 2020-05-18 05:15
```python
print 'bar'
```

without spaces should work: from GitHub help page:

Just wrap your code blocks in ``` and you won't need to indent manually to trigger a code block.


As illustrated in hilz's answer below, you need to indent the ```` with the same indentation level + 2 spaces than your list.
The content of the code block doesn't need to be indented.

1. foo

  ````python
print 'bar'
  ````

  1.

    ````python
print 'bar'
    ````

See this gist as an example:

indented code block

查看更多
虎瘦雄心在
4楼-- · 2020-05-18 05:25

Nowadays, you have to do the following:

1. lorem ipsum
  ```perl
  use strict;
  ```
2. dolor sit amet
      ```perl
      use warnings;
      ```
   1. consectetur adipiscing elit
   1. sed do
1. eiusmod

That is, make sure your syntax-highlighted code starts in the same column as the backticks. You also have to help the numbering out a little bit, because it appears to lose count after code blocks.

See also: https://gist.github.com/therealbstern/9cb0dfc7f0f4b76a062247676aed341b

查看更多
登录 后发表回答