获得 markdown 无序列表格式的文件目录树

发布时间 2023-06-26 00:35:03作者: BuckyI

tree 命令可以获得文件目录结构,但是放在文档中时,我想用 markdown 无序列表的形式,在编辑器内还可以折叠。

完整解决方案:在 ~/.oh-my-zsh/custom 下添加下述自定义函数(这里只是为了方便创建了函数,如果一次性使用的话直接调用命令即可)

tree-markdown() {
    echo "? generate tree of $@ in markdown format"
    local trees=$(tree -v --dirsfirst --noreport  --charset ascii $@ |
        sed -e 's/| \+/  /g' \
            -e 's/[|`]-\+/ -/g')
    echo ${trees}
}

参考资料:
markdown - Is there a way to represent a directory tree in a Github README.md? - Stack Overflow