Highlight bash/shell code in markdown

这一生的挚爱 提交于 2019-12-04 07:20:52

问题


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


For example to highlight js I write:

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

To highlight HTML code I use ```html.

How can we highlight bash/shell commands?


回答1:


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




回答2:


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




回答3:


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.



回答4:


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



回答5:


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.




回答6:


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.




回答7:


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

```properties
npm run build
```  

I obtain something like:

npm run build



来源:https://stackoverflow.com/questions/20303826/highlight-bash-shell-code-in-markdown

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