在 Markdown 中突出显示 bash/shell 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20303826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:29:03  来源:igfitidea点击:

Highlight bash/shell code in markdown

bashshellmarkdowngithub-flavored-markdown

提问by Ionic? Biz?u

How to highlight the bash/shell commands in markdown files?

如何在 Markdown 文件中突出显示 bash/shell 命令?



For example to highlight jsI write:

例如为了突出js我写:

```js
function () { return "This code is highlighted as Javascript!"}
```

To highlight HTML code I use ```html.

为了突出显示我使用的 HTML 代码```html

How can we highlight bash/shell commands?

我们如何突出显示 bash/shell 命令?

回答by hek2mgl

Depends on the markdown rendering engine and the markdown flavour. There is no standard for this. If you mean github flavoured markdown for example, shellshould work fine. Aliases are sh, bashor zsh. You can find the list of available syntax lexers here

取决于降价渲染引擎和降价风格。这没有标准。例如,如果您的意思是 github 风味的降价,则shell应该可以正常工作。别名是sh,bashzsh。您可以在此处找到可用语法词法分析器的列表

回答by Anton Strogonoff

If you are looking to highlight a shell session command sequence as it looks to the user (with prompts, not just as contents of a hypothetical script file), then the right identifier to use at the moment is console:

如果您希望突出显示用户看到的 shell 会话命令序列(带有提示,而不仅仅是假设脚本文件的内容),那么此时使用的正确标识符是console

```console
foo@bar:~$ whoami
foo
```

GitHub Markdown preview tab screenshot

GitHub Markdown 预览标签截图

回答by Alireza Fattahi

I find good description at https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code

我在https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code找到了很好的描述

Code blocks are part of the Markdown spec, but syntax highlighting isn't.

However, many renderers -- like Github's and Markdown Here -- support syntax highlighting. Which languages are supported and how those language names should be written will vary from renderer to renderer. Markdown Here supports highlighting for dozens of languages (and not-really-languages, like diffs and HTTP headers); to see the complete list, and how to write the language names, see the highlight.jsdemo page.

代码块是 Markdown 规范的一部分,但语法高亮不是.

然而,许多渲染器——比如 Github 和 Markdown Here——支持语法高亮。支持哪些语言以及这些语言名称的书写方式因渲染器而异。Markdown Here 支持对数十种语言(和非真正语言,如差异和 HTTP 标头)进行高亮显示;要查看完整列表以及如何编写语言名称,请参阅highlight.js演示页面。

Although I could not find any official git hub doc about using highlight.js, I've tested lots of languages and seemed to be working

虽然我找不到任何关于 using 的官方 git hub 文档highlight.js,但我已经测试了很多语言并且似乎正在工作

To see list of languages I used https://highlightjs.readthedocs.io/en/latest/css-classes-reference.html#language-names-and-aliases

要查看我使用的语言列表https://highlightjs.readthedocs.io/en/latest/css-classes-reference.html#language-names-and-aliases

Some shell samples:

一些外壳示例:

Shell:      console, shell
Bash:       bash, sh, zsh
Powershell: powershell, ps
Dos:        dos, bat, cmd

Example:

例子:

```bat
cd \
copy a b
ping 192.168.0.1
```

回答by irJERAD

using the knitrpackage:

使用knitr包:

```{r, engine='bash', code_block_name} ...

e.g.:

例如:

```{r, engine='bash', count_lines}
wc -l en_US.twitter.txt 
```

you can also use:

您还可以使用:

  • engine='sh'for shell
  • engine='python'for python
  • engine='perl', engine='haskell'and a bunch of other C-like languages and even gawk, awketc.
  • engine='sh'用于外壳
  • engine='python'蟒蛇
  • engine='perl'engine='haskell'和一堆其他类似C语言的,甚至gawkawk等等。

回答by Mr. Kennedy

Per the documentation from GitHubregarding GFM syntax highlighted code blocks

根据GitHub 上关于 GFM 语法高亮代码块的文档

We use Linguistto perform language detection and syntax highlighting. You can find out which keywords are valid in the languages YAML file.

我们使用Linguist来执行语言检测和语法突出显示。您可以在语言 YAML 文件中找出哪些关键字是有效

Rendered on GitHub, consolemakes the lines after the console blue. bash, sh, or shelldon't seem to "highlight" much ...and you can use poshfor PowerShell or CMD.

在 GitHub 上呈现,console使控制台后面的线条变为蓝色。bash, sh, 或shell似乎没有“突出显示”太多……您可以posh用于 PowerShell 或 CMD。

回答by g.annunziata

If need only to highlight first word as command, so I often use properties:

如果只需要突出显示第一个单词作为命令,那么我经常使用properties

```properties
npm run build
```  

I obtain something like:

我得到类似的东西:

npmrun build

npm运行构建

回答by AlvvaysPaul

Bitbucket uses CodeMirror for syntax highlighting. For bash or shell you can use sh, bash, or zsh. More information can be found at Configuring syntax highlighting for file extensionsand Code mirror language modes.

Bitbucket 使用 CodeMirror 进行语法高亮显示。对于bash或外壳,您可以使用shbashzsh。可以在配置文件扩展名代码镜像语言模式的语法高亮显示中找到更多信息。