I have an array of file path components like this:
[ ['some', 'dir', 'file.txt'],
['other', 'folder', 'here.txt'],
['this', 'one', 'is', 'deeper', 'file.txt'],
['some', 'dir', 'second.txt'
]
So the array contains arrays each consisting of path components to a file. The last element in an inner array is always the file itself, with the preceding elements being directories leading to the file.
What I'm trying to figure out is how to transform the above data so that I can easily generate a file tree with it using <ul>
and <li>
tags such that folders are nested within each other and files within the same folder show up together. All sorted alphabetically.
From the above I would like to generate the following. The file <li>
themselves have to be links to the path to that file:
<ul>
<li>some/
<ul>
<li>dir/
<ul>
<li><a href="some/dir/file.txt">file.txt</a></li>
<li><a href="some/dir/second.txt">second.txt</a></li>
</ul>
</li>
</ul>
</li>
<li>other/
<ul>
<li>folder/
<ul>
<li><a href="other/folder/here.txt">here.txt<a/></li>
</ul>
</li>
</ul>
</li>
<li>this/
<ul>
<li>one/
<ul>
<li>is/
<ul>
<li>deeper/
<ul>
<li><a href="this/one/is/deeper/file.txt">file.txt</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Thanks, I'd appreciate any ideas.