Return the depth-first crawl order of pages from a site map
A docs team is building a “print the whole site” feature. Starting from one or more entry pages, the crawler follows links and prints each page the first time it reaches it. Readers expect a page to be followed by the pages it links to before moving on to the next topic, so the crawl must go depth first.
Implement crawlOrder(siteMap, entry). It returns the pages in the order a depth-first crawl visits them.
crawlOrder(siteMap, entry) // => string[]
siteMap: an object whose keys are page paths and whose values are arrays of the paths that page links to, in the order they appear on the page.entry: a page path, or an array of page paths.siteMap has no outgoing links, but it is still visited when something links to it. An entry page that is not a key is visited on its own.entry is an array, crawl from each entry in order. Entries that were already visited by an earlier crawl are skipped. An empty array returns [].siteMap.const siteMap = {
'/': ['/docs', '/blog', '/pricing'],
'/docs': ['/docs/install', '/docs/api'],
'/docs/api': ['/docs/install', '/'],
'/blog': ['/blog/launch'],
'/blog/launch': ['/pricing'],
};
crawlOrder(siteMap, '/');
// => ['/', '/docs', '/docs/install', '/docs/api', '/blog', '/blog/launch', '/pricing']
crawlOrder(siteMap, ['/blog', '/docs']);
// => ['/blog', '/blog/launch', '/pricing', '/docs', '/docs/install', '/docs/api', '/']
Note that /pricing is reached through /blog/launch before the crawl gets back to the last link on /.