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中有两种格式化代码的方式。 您可以通过将反引号(`)放在行的各个部分周围来使用内联代码,也可以使用代码块,某些渲染器将对其应用语法突出显示。
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()
。
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;
}
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.
请记住,并非所有降价引擎都会应用语法突出显示。
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键盘,可能很难找到。 该实用指南列出了一些查找反引号的方法,我们在下面收集了这些信息:
翻译自: https://www.freecodecamp.org/news/how-to-format-code-in-markdown/
markdown格式化代码