Skip to content

Commit

Permalink
docs(nxdev): add tree lookup by path on documents (#11531)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcabanes authored Aug 10, 2022
1 parent ff7a25a commit 608f57d
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions nx-dev/data-access-documents/src/lib/documents.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export class DocumentsApi {
} else {
paths.push({
params: {
segments: [...acc, curr.id],
segments: curr.path
? curr.path.split('/').filter(Boolean).flat()
: [...acc, curr.id],
},
});
}
Expand All @@ -98,22 +100,48 @@ export class DocumentsApi {
return paths;
}

/**
* Getting the document's filePath is done in 2 steps
* - traversing the tree by path segments
* - if not found, try searching for it via the complete path string
* @param path
* @private
*/
private getFilePath(path: string[]): string {
let items = this.documents?.itemList;

if (!items) {
throw new Error(`Document not found`);
}

let found;
let found: DocumentMetadata | null = null;
// Traversing the tree by matching item's ids with path's segments
for (const part of path) {
found = items?.find((item) => item.id === part);
found = items?.find((item) => item.id === part) || null;
if (found) {
items = found.itemList;
} else {
throw new Error(`Document not found`);
}
}

// If still not found, then attempt to match any item's id with the current path as a string
if (!found) {
function recur(curr, acc) {
if (curr.itemList) {
curr.itemList.forEach((ii) => {
recur(ii, [...acc, curr.id]);
});
} else {
if (curr.path === '/' + path.join('/')) {
found = curr;
}
}
}
this.documents.itemList!.forEach((item) => {
recur(item, []);
});
}

if (!found) throw new Error(`Document not found`);
const file = found.file ?? ['generated', ...path].join('/');
return join(this.options.publicDocsRoot, `${file}.md`);
}
Expand Down

1 comment on commit 608f57d

@vercel
Copy link

@vercel vercel bot commented on 608f57d Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.