docs-in-folder.ts 966 B

12345678910111213141516171819202122232425262728293031
  1. import { Folder } from '../../../../../types/folder'
  2. import { DocId, MainDocument } from '../../../../../types/project-settings'
  3. function findAllDocsInFolder(folder: Folder, path = '') {
  4. const docs = folder.docs.map<MainDocument>(doc => ({
  5. doc: { id: doc._id as DocId, name: doc.name },
  6. path: path + doc.name,
  7. }))
  8. for (const subFolder of folder.folders) {
  9. docs.push(...findAllDocsInFolder(subFolder, `${path}${subFolder.name}/`))
  10. }
  11. return docs
  12. }
  13. export function docsInFolder(folder: Folder) {
  14. const docsInTree = findAllDocsInFolder(folder)
  15. docsInTree.sort(function (a, b) {
  16. const aDepth = (a.path.match(/\//g) || []).length
  17. const bDepth = (b.path.match(/\//g) || []).length
  18. if (aDepth - bDepth !== 0) {
  19. return -(aDepth - bDepth) // Deeper path == folder first
  20. } else if (a.path < b.path) {
  21. return -1
  22. } else if (a.path > b.path) {
  23. return 1
  24. } else {
  25. return 0
  26. }
  27. })
  28. return docsInTree
  29. }