Is there a way to represent a directory tree in a Github README.md?

后端 未结 14 1842
庸人自扰
庸人自扰 2020-12-22 17:03

In my Githubs repos documentation I want to represent a directory tree structure like this:

\"enter

14条回答
  •  无人及你
    2020-12-22 17:38

    I wrote a small script that does the trick:

    #!/bin/bash
    
    #File: tree-md
    
    tree=$(tree -tf --noreport -I '*~' --charset ascii $1 |
           sed -e 's/| \+/  /g' -e 's/[|`]-\+/ */g' -e 's:\(* \)\(\(.*/\)\([^/]\+\)\):\1[\4](\2):g')
    
    printf "# Project tree\n\n${tree}"
    

    Example:

    Original tree command:

    $ tree
    .
    ├── dir1
    │   ├── file11.ext
    │   └── file12.ext
    ├── dir2
    │   ├── file21.ext
    │   ├── file22.ext
    │   └── file23.ext
    ├── dir3
    ├── file_in_root.ext
    └── README.md
    
    3 directories, 7 files
    

    Markdown tree command:

    $ ./tree-md .
    # Project tree
    
    .
     * [tree-md](./tree-md)
     * [dir2](./dir2)
       * [file21.ext](./dir2/file21.ext)
       * [file22.ext](./dir2/file22.ext)
       * [file23.ext](./dir2/file23.ext)
     * [dir1](./dir1)
       * [file11.ext](./dir1/file11.ext)
       * [file12.ext](./dir1/file12.ext)
     * [file_in_root.ext](./file_in_root.ext)
     * [README.md](./README.md)
     * [dir3](./dir3)
    

    Rendered result:

    (Links are not visible in Stackoverflow...)

    Project tree
    • tree-md
    • dir2
      • file21.ext
      • file22.ext
      • file23.ext
    • dir1
      • file11.ext
      • file12.ext
    • file_in_root.ext
    • README.md
    • dir3

提交回复
热议问题