|
1 | 1 | module.exports.register = function ({ config }) { |
2 | | - const { addToNavigation, unlistedPagesHeading = 'Unlisted Pages' } = config |
3 | | - const logger = this.getLogger('unlisted-pages-extension') |
4 | | - this |
5 | | - .on('navigationBuilt', ({ contentCatalog }) => { |
6 | | - contentCatalog.getComponents().forEach(({ versions }) => { |
7 | | - versions.forEach(({ name: component, version, navigation: nav, url: defaultUrl }) => { |
8 | | - const navEntriesByUrl = getNavEntriesByUrl(nav) |
9 | | - const unlistedPages = contentCatalog |
10 | | - .findBy({ component, version, family: 'page' }) |
11 | | - .filter((page) => page.out) |
12 | | - .reduce((collector, page) => { |
13 | | - // Check if the 'unlisted-page' attribute is set to true |
14 | | - if (page.asciidoc.attributes['unlisted-page'] === 'true') { |
15 | | - return collector; // Skip this page |
16 | | - } |
17 | | - if ((page.pub.url in navEntriesByUrl) || page.pub.url === defaultUrl) return collector |
18 | | - logger.warn({ file: page.src, source: page.src.origin }, 'detected unlisted page') |
19 | | - return collector.concat(page) |
20 | | - }, []) |
21 | | - if (unlistedPages.length && addToNavigation) { |
22 | | - nav.push({ |
23 | | - content: unlistedPagesHeading, |
24 | | - items: unlistedPages.map((page) => { |
25 | | - return { content: page.asciidoc.navtitle, url: page.pub.url, urlType: 'internal' } |
26 | | - }), |
27 | | - root: true, |
28 | | - }) |
29 | | - } |
| 2 | + const { addToNavigation, unlistedPagesHeading = 'Unlisted Pages' } = config |
| 3 | + const logger = this.getLogger('unlisted-pages-extension') |
| 4 | + this |
| 5 | + .on('navigationBuilt', ({ contentCatalog }) => { |
| 6 | + contentCatalog.getComponents().forEach(({ versions }) => { |
| 7 | + versions.forEach(({ name: component, version, navigation: nav, url: defaultUrl }) => { |
| 8 | + const navEntriesByUrl = getNavEntriesByUrl(nav) |
| 9 | + const unlistedPages = contentCatalog |
| 10 | + .findBy({ component, version, family: 'page' }) |
| 11 | + .filter((page) => page.out) |
| 12 | + .reduce((collector, page) => { |
| 13 | + // Check if the 'unlisted-page' attribute is set to true |
| 14 | + if (page.asciidoc.attributes['unlisted-page'] === 'true') { |
| 15 | + return collector; // Skip this page |
| 16 | + } |
| 17 | + if ((page.pub.url in navEntriesByUrl) || page.pub.url === defaultUrl) return collector |
| 18 | + logger.warn({ file: page.src, source: page.src.origin }, 'detected unlisted page') |
| 19 | + return collector.concat(page) |
| 20 | + }, []) |
| 21 | + if (unlistedPages.length && addToNavigation) { |
| 22 | + nav.push({ |
| 23 | + content: unlistedPagesHeading, |
| 24 | + items: unlistedPages.map((page) => { |
| 25 | + return { content: page.asciidoc.navtitle, url: page.pub.url, urlType: 'internal' } |
| 26 | + }), |
| 27 | + root: true, |
| 28 | + }) |
| 29 | + } |
| 30 | + }) |
30 | 31 | }) |
31 | 32 | }) |
32 | | - }) |
33 | | -} |
| 33 | + } |
34 | 34 |
|
35 | | -function getNavEntriesByUrl (items = [], accum = {}) { |
36 | | - items.forEach((item) => { |
37 | | - if (item.urlType === 'internal') accum[item.url.split('#')[0]] = item |
38 | | - getNavEntriesByUrl(item.items, accum) |
39 | | - }) |
40 | | - return accum |
41 | | -} |
| 35 | + function getNavEntriesByUrl (items = [], accum = {}) { |
| 36 | + items.forEach((item) => { |
| 37 | + if (item.urlType === 'internal') accum[item.url.split('#')[0]] = item |
| 38 | + getNavEntriesByUrl(item.items, accum) |
| 39 | + }) |
| 40 | + return accum |
| 41 | + } |
0 commit comments