关闭。此问题不符合 Stack Overflow 准则。它目前不接受答案。我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为 Stack Overflow 的主题。 2年前关闭。改进这个问题
我想用 Markdown 语法描述目录和文件结构。有没有办法巧妙地做到这一点?
.
├── _config.yml
├── _drafts
│ ├── begin-with-the-crazy-ideas.textile
│ └── on-simplicity-in-technology.markdown
├── _includes
│ ├── footer.html
│ └── header.html
├── _layouts
│ ├── default.html
│ └── post.html
├── _posts
│ ├── 2007-10-29-why-every-programmer-should-play-nethack.textile
│ └── 2009-04-26-barcamp-boston-4-roundup.textile
├── _data
│ └── members.yml
├── _site
└── index.html
编辑:Unicode 具有用于此目的的绘图字符,可以按原样复制粘贴:https://en.wikipedia.org/wiki/Box-drawing_character
我按照 another repository 中的示例将目录结构包装在一对三重反引号 (```
) 中:
```
project
│ README.md
│ file001.txt
│
└───folder1
│ │ file011.txt
│ │ file012.txt
│ │
│ └───subfolder1
│ │ file111.txt
│ │ file112.txt
│ │ ...
│
└───folder2
│ file021.txt
│ file022.txt
```
如果您担心 Unicode 字符,您可以使用 ASCII 来构建结构,因此您的示例结构变为
.
+-- _config.yml
+-- _drafts
| +-- begin-with-the-crazy-ideas.textile
| +-- on-simplicity-in-technology.markdown
+-- _includes
| +-- footer.html
| +-- header.html
+-- _layouts
| +-- default.html
| +-- post.html
+-- _posts
| +-- 2007-10-29-why-every-programmer-should-play-nethack.textile
| +-- 2009-04-26-barcamp-boston-4-roundup.textile
+-- _data
| +-- members.yml
+-- _site
+-- index.html
如果您选择 ANSI
输出,这类似于 tree
使用的格式。
tree
的输出)使用不同符号的其他选择相比,此选择的另一个优点是差异更小。
如果您使用 VS Code,这是用于生成文件树的 awesome extension。
直接添加到markdown...
📦quakehunter
┣ 📂client
┣ 📂node_modules
┣ 📂server
┃ ┗ 📜index.js
┣ 📜.gitignore
┣ 📜package-lock.json
┗ 📜package.json
您可以使用 tree 生成与您的示例非常相似的内容。获得输出后,您可以将其包装在 <pre>
标记中以保留纯文本格式。
正如已经推荐的那样,您可以使用 tree
。但是为了将它与重组文本一起使用,需要一些额外的参数。
如果您使用 pandoc
生成 pdf,则不会打印标准的 tree
输出。
tree --dirsfirst --charset=ascii /path/to/directory
将生成一个很好的 ASCII
树,可以像这样集成到您的文档中:
.. code::
.
|-- ContentStore
| |-- de-DE
| | |-- art.mshc
| | |-- artnoloc.mshc
| | |-- clientserver.mshc
| | |-- noarm.mshc
| | |-- resources.mshc
| | `-- windowsclient.mshc
| `-- en-US
| |-- art.mshc
| |-- artnoloc.mshc
| |-- clientserver.mshc
| |-- noarm.mshc
| |-- resources.mshc
| `-- windowsclient.mshc
`-- IndexStore
|-- de-DE
| |-- art.mshi
| |-- artnoloc.mshi
| |-- clientserver.mshi
| |-- noarm.mshi
| |-- resources.mshi
| `-- windowsclient.mshi
`-- en-US
|-- art.mshi
|-- artnoloc.mshi
|-- clientserver.mshi
|-- noarm.mshi
|-- resources.mshi
`-- windowsclient.mshi
我制作了一个节点模块来自动执行此任务:mddir
用法
节点 mddir "../relative/path/"
安装: npm install mddir -g
为当前目录生成markdown:mddir
为任何绝对路径生成: mddir /absolute/path
为相对路径生成:mddir ~/Documents/whatever。
md 文件在您的工作目录中生成。
当前忽略 node_modules 和 .git 文件夹。
故障排除
如果您收到错误“节点\r:没有此类文件或目录”,则问题是您的操作系统使用不同的行尾,并且 mddir 无法解析它们,除非您将行尾样式显式设置为 Unix。这通常会影响 Windows,但也会影响某些版本的 Linux。必须在 mddir npm 全局 bin 文件夹中将行尾设置为 Unix 样式。
行尾修复
获取 npm bin 文件夹路径:
npm config get prefix
cd到那个文件夹
冲泡安装dos2unix
dos2unix lib/node_modules/mddir/src/mddir.js
这会将行尾转换为 Unix 而不是 Dos
然后正常运行:node mddir "../relative/path/"。
示例生成的降价文件结构 '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
如果您使用的是 Atom 编辑器,则可以通过 ascii-tree 包完成此操作。
您可以编写以下树:
root
+-- dir1
+--file1
+-- dir2
+-- file2
并通过选择它并按 ctrl-alt-t
将其转换为以下内容:
root
├── dir1
│ └── file1
└── dir2
└── file2
我为我的 Dropbox 文件列表编写了这个脚本。
sed
用于删除 ->
之后的符号链接文件/文件夹路径的完整路径
很遗憾,标签丢失了。 使用 zsh
我可以保留标签。
!/usr/bin/env bash
#!/usr/bin/env zsh
F1='index-2.md' #With hyperlinks
F2='index.md'
if [ -e $F1 ];then
rm $F1
fi
if [ -e $F2 ];then
rm $F2
fi
DATA=`tree --dirsfirst -t -Rl --noreport | \
sed 's/->.*$//g'` # Remove symlink adress and ->
echo -e '```\n' ${DATA} '\n```' > $F1 # Markdown needs triple back ticks for <pre>
# With the power of piping, creating HTML tree than pipe it
# to html2markdown program, creates cool markdown file with hyperlinks.
DATA=`tree --dirsfirst -t -Rl --noreport -H http://guneysu.pancakeapps.com`
echo $DATA | \
sed 's/\r\r/\n/g' | \
html2markdown | \
sed '/^\s*$/d' | \
sed 's/\# Directory Tree//g' | \
> $F2
输出如下:
```
.
├── 2013
│ └── index.markdown
├── 2014
│ └── index.markdown
├── 2015
│ └── index.markdown
├── _posts
│ └── 2014-12-27-2014-yili-degerlendirmesi.markdown
├── _stash
└── update.sh
```
[BASE_URL/](BASE_URL/)
├── [2013](BASE_URL/2013/)
│ └── [index.markdown](BASE_URL/2013/index.markdown)
├── [2014](BASE_URL/2014/)
│ └── [index.markdown](BASE_URL/2014/index.markdown)
├── [2015](BASE_URL/2015/)
│ └── [index.markdown](BASE_URL/2015/index.markdown)
├── [_posts](BASE_URL/_posts/)
│ └── [2014-12-27-2014-yili-degerlendirmesi.markdown](_posts/2014-12-27-2014-yili-degerlendirmesi.markdown)
├── [_stash](BASE_URL/_stash/)
├── [index-2.md](BASE_URL/index-2.md)
└── [update.sh](BASE_URL/update.sh)
* * *
tree v1.6.0 © 1996 - 2011 by Steve Baker and Thomas Moore
HTML output hacked and copyleft © 1998 by Francesc Rocher
Charsets / OS/2 support © 2001 by Kyosuke Tokoro
我建议使用 wasabi 然后你可以使用 markdown-ish 感觉像这样
root/ # entry comments can be inline after a '#'
# or on their own line, also after a '#'
readme.md # a child of, 'root/', it's indented
# under its parent.
usage.md # indented syntax is nice for small projects
# and short comments.
src/ # directories MUST be identified with a '/'
fileOne.txt # files don't need any notation
fileTwo* # '*' can identify executables
fileThree@ # '@' can identify symlinks
并在 this 的 js 库中抛出确切的语法
在 OSX 下,使用 reveal.js
,如果我只是用户 tree
,然后复制/粘贴输出,则会出现渲染问题:出现奇怪的符号。
我找到了两种可能的解决方案。
1) 使用 charset ascii 并简单地将输出复制/粘贴到降价文件中
tree -L 1 --charset=ascii
2)markdown文件中直接使用html和unicode
<pre>
.
⊢ README.md
⊢ docs
⊢ e2e
⊢ karma.conf.js
⊢ node_modules
⊢ package.json
⊢ protractor.conf.js
⊢ src
⊢ tsconfig.json
⌙ tslint.json
</pre>
希望能帮助到你。
为此有一个 NPM 模块:
它允许您将目录树表示为字符串或对象。将它与命令行一起使用将允许您将表示保存在 txt 文件中。
例子:
$ npm dree parse myDirectory --dest ./generated --name tree
不定期副业成功案例分享
└
符号,还是有办法用 ascii 写它们?