markdown格式化代码_如何在Markdown中格式化代码

markdown格式化代码

There are two ways to format code in Markdown. You can either use inline code, by putting backticks (`) around parts of a line, or you can use a code block, which some renderers will apply syntax highlighting to.

在Markdown中有两种格式化代码的方式。 您可以通过将反引号(`)放在行的各个部分周围来使用内联代码,也可以使用代码块,某些渲染器将对其应用语法突出显示。

内联代码 (Inline Code)

You can use inline code formatting to emphasize a small command or piece of syntax within a line you’re writing.

您可以使用内联代码格式来强调您正在编写的一行中的一条小命令或一段语法。

For example, you may wish to mention JavaScript’s Array.protoype.map() method. By using inline code formatting, it is clear that this is a piece of code. You might also use it to illustrate a terminal command, like yarn install.

例如,您可能希望提及JavaScript的Array.protoype.map()方法。 通过使用内联代码格式,很明显这是一段代码。 您也可以使用它来说明终端命令,例如yarn install

To use inline code formatting, simply wrap the code you wish to format in backticks. On a standard US layout QWERTY keyboard, this can be found to the left of ‘1’, and above the Tab key. More information on the location of the backtick on international keyboards is provided below.

要使用内联代码格式,只需将要格式化的代码包装在反引号中即可。 在标准的美国布局QWERTY键盘上,可以在“ 1”的左侧和Tab键的上方找到它。 下面提供了有关国际键盘上反引号位置的更多信息。

For instance, writing `Array.prototype.map()` in markdown will render as Array.prototype.map().

例如,在markdown中写入Array.prototype.map()将呈现为Array.prototype.map()

代码块 (Code Blocks)

To write longer or more detailed snippets of code, it is often better to place them inside a code block. Code blocks allow you to use multiple lines, and markdown will render it inside its own box and with code type font.

要编写更长或更详细的代码片段,通常最好将它们放在代码块中。 代码块允许您使用多行,并且markdown会将其呈现在其自己的框内,并使用代码类型的字体。

To achieve this, start your block with a line of three backticks. This signals to markdown that you are creating a code block. You will need to finish with another line of three backticks. For example:

为此,请以三个反引号开始行。 这向标记降价表示您正在创建代码块。 您将需要完成另外三个反引号行。 例如:

```var add2 = function(number) {   return number + 2;}```

``var add2 = function(number){返回数字+ 2;}`''

will render in markdown as:

将在markdown中呈现为:

var add2 = function(number) {
  return number + 2;
}

语法高亮 (Syntax highlighting)

While not supported natively by markdown, many markdown engines, including the one used by GitHub, will support syntax highlighting. This means that by telling markdown what language you're using inside the code block, it will add colors like an IDE would.

虽然markdown本身不支持Markdown引擎,但许多markdown引擎(包括GitHub所使用的引擎)将支持语法突出显示。 这意味着通过告诉markdown您在代码块内使用的是哪种语言,它将像IDE一样添加颜色。

You can do this by adding the name of the language on the same line as your opening three back ticks. In the example above, if instead of the first line being ``` you could write ```js, then JavaScript highlighting will be applied to the block.

为此,您可以在打开三个倒钩的同时,在同一行添加语言名称。 在上面的示例中,如果您可以编写```js而不是第一行,那么JavaScript高亮将应用于该块。

var add2 = function(number) {
	return number + 2;
}

Syntax highlighting can be applied to more than just JavaScript, though. You can use ```html:

但是,语法高亮不仅仅可以应用于JavaScript。 您可以使用```html:

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <h1>Hello World</h1>
  </div>
</div>

```ruby:

Ruby:

"Hello World".split('').each do |letter|
  puts letter
end

or ```python:

或```python:

a, b = 0, 1
while b < 10:
    print(b)
    a, b = a, a + b

Just remember, not all markdown engines will apply syntax highlighting.

请记住,并非所有降价引擎都会应用语法突出显示。

国际键盘上的反引号 (Backticks on international keyboards)

The location of the backtick key can be different on different keyboards, and if you’re not using a US layout QWERTY keyboard, it may be tricky to find. This helpful guide lists some of the ways to find your backtick key, which we’ve collected here below:

在不同的键盘上,反引号键的位置可能会有所不同,如果您使用的不是美国布局的QWERTY键盘,可能很难找到。 实用指南列出了一些查找反引号的方法,我们在下面收集了这些信息:

QWERTY和QWERTZ: (QWERTY and QWERTZ:)

AZERTY: (AZERTY:)

翻译自: https://www.freecodecamp.org/news/how-to-format-code-in-markdown/

markdown格式化代码

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值