Markdown/Github:作为列表子项的代码块的语法突出显示

发布于 2024-11-13 15:58:02 字数 220 浏览 2 评论 0原文

在Github/MD中,如果我们想在列表的子级时启用代码块,我们需要用8个空格来表示它。

但是如何让该代码块具有语法高亮功能呢?

以下代码无法按预期工作...

    1. foo

             ```python
                print 'bar'
             ```

    2. bar

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-11-20 15:58:02
```python
print 'bar'
```

没有空格应该可以工作:来自 GitHub 帮助页面

只需将代码块括在 ``` 中,您无需手动缩进即可触发代码块


hilz中所示回答下面,您需要使用与列表相同的缩进级别 + 2 个空格来缩进 ````。
代码块的内容不需要缩进

1. foo

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

  1.

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

请参阅此要点作为示例

缩进代码块

```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

计㈡愣 2024-11-20 15:58:02

要使带有语法突出显示的代码块愉快地嵌入到列表中,请将代码块之前和之后的标记行嵌入到附加段落的适当缩进级别,然后照常进行。例如:

1. lorem ipsum

  ```ruby
resources :dolor
  ```

   1. sit amet

      ```ruby
resources :elit
      ```

   1. sed do

1. eiusmod

将每个代码块缩进到适当的深度并保持索引的完整性。

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.

话少心凉 2024-11-20 15:58:02

现在,您必须执行以下操作:

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

也就是说,确保语法突出显示的代码与反引号在同一列中开始。您还必须稍微帮助编号,因为在代码块之后它似乎会丢失计数。

另请参阅:https://gist.github.com/therealbstern/9cb0dfc7f0f4b76a062247676aed341b

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文