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

后端 未结 14 1761
庸人自扰
庸人自扰 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:36

    None of the above solution worked for me completely on my mac.

    The best solution I found this is to use the utility created here.

    https://github.com/michalbe/md-file-tree

    Once you have installed the utility npm install md-file-tree -g then you can simply run to get all files tree

    md-file-tree . > README.md
    
    0 讨论(0)
  • 2020-12-22 17:37

    I made a node module to automate this task: mddir

    Usage

    node mddir "../relative/path/"

    To install: npm install mddir -g

    To generate markdown for current directory: mddir

    To generate for any absolute path: mddir /absolute/path

    To generate for a relative path: mddir ~/Documents/whatever.

    The md file gets generated in your working directory.

    Currently ignores node_modules, and .git folders.

    Troubleshooting

    If you receive the error 'node\r: No such file or directory', the issue is that your operating system uses different line endings and mddir can't parse them without you explicitly setting the line ending style to Unix. This usually affects Windows, but also some versions of Linux. Setting line endings to Unix style has to be performed within the mddir npm global bin folder.

    Line endings fix

    Get npm bin folder path with:

    npm config get prefix

    Cd into that folder

    brew install dos2unix

    dos2unix lib/node_modules/mddir/src/mddir.js

    This converts line endings to Unix instead of Dos

    Then run as normal with: node mddir "../relative/path/".

    Example generated markdown file structure 'directoryList.md'

        |-- .bowerrc
        |-- .jshintrc
        |-- .jshintrc2
        |-- Gruntfile.js
        |-- README.md
        |-- bower.json
        |-- karma.conf.js
        |-- package.json
        |-- app
            |-- app.js
            |-- db.js
            |-- directoryList.md
            |-- index.html
            |-- mddir.js
            |-- routing.js
            |-- server.js
            |-- _api
                |-- api.groups.js
                |-- api.posts.js
                |-- api.users.js
                |-- api.widgets.js
            |-- _components
                |-- directives
                    |-- directives.module.js
                    |-- vendor
                        |-- directive.draganddrop.js
                |-- helpers
                    |-- helpers.module.js
                    |-- proprietary
                        |-- factory.actionDispatcher.js
                |-- services
                    |-- services.cardTemplates.js
                    |-- services.cards.js
                    |-- services.groups.js
                    |-- services.posts.js
                    |-- services.users.js
                    |-- services.widgets.js
            |-- _mocks
                |-- mocks.groups.js
                |-- mocks.posts.js
                |-- mocks.users.js
                |-- mocks.widgets.js
    
    0 讨论(0)
  • 2020-12-22 17:37

    Simple tree command will do the job. For example: tree -o readme.md will print the tree structure of your current working directory and write it to readme.md. Then open readme.md file in one of text editor like Sublime and wrap its content within a pair of triple backticks (```).

    FYI: you might have to brew install tree in OSX if it's not already installed. In Linux and Windows it should just work fine. Also in windows you might have to replace hyphen with forward slash.

    I hope this helps.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-22 17:38

    If you are working on windows write tree /f inside the directory you want to achieve that in command prompt. This should do your job. you can copy and paste the output on markdown surrounded my triple back ticks i.e. '''{tree structure here}'''

    0 讨论(0)
  • 2020-12-22 17:41

    You can use <pre> tags as I did in one of my projects.

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