Highlight Bash/shell code in Markdown files

后端 未结 7 1973
猫巷女王i
猫巷女王i 2021-01-29 17:35

How can I highlight the Bash/shell commands in Markdown files?


For example, to highlight js, I write:



        
相关标签:
7条回答
  • 2021-01-29 17:43

    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.

    0 讨论(0)
  • 2021-01-29 17:45

    It 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.

    0 讨论(0)
  • 2021-01-29 17:45

    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.
    0 讨论(0)
  • 2021-01-29 17:55

    I found a good description at Markdown Cheatsheet:

    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 GitHub documentation about using highlight.js, I've tested lots of languages and seemed to be working

    To see list of languages I used https://github.com/highlightjs/highlight.js/blob/master/SUPPORTED_LANGUAGES.md

    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
    ```
    
    0 讨论(0)
  • 2021-01-29 17:55

    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.

    0 讨论(0)
  • 2021-01-29 17:57

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

    ```properties
    npm run build
    ```  
    

    I obtain something like:

    npm run build

    0 讨论(0)
提交回复
热议问题