Highlight bash/shell code in markdown

喜欢而已 提交于 2019-12-02 13:49:12

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

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:

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

irJERAD

using the knitr package:

```{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, awk etc.

I find good description at 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.js demo page.

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

To see list of languages I used 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
```

Per the documentation from GitHub regarding GFM syntax highlighted code blocks

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

Rendered on GitHub, console makes the lines after the console blue. bash, sh, or shell don't seem to "highlight" much ...and you can use posh for PowerShell or CMD.

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 extensions and Code mirror language modes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!