diff --git a/scripts/build-docs.js b/scripts/build-docs.js index 977ddd204ec6..ac47b6751cee 100644 --- a/scripts/build-docs.js +++ b/scripts/build-docs.js @@ -1,5 +1,4 @@ -const sortBy = require('lodash/sortBy'); - +const sortBy = require('lodash/sortBy') function buildNavTree(navItems) { try { const tree = { @@ -7,7 +6,7 @@ function buildNavTree(navItems) { item: { title: 'Welcome', weight: 0, isRootSection: true, isSection: true, rootSectionId: 'welcome', sectionWeight: 0, slug: '/docs' }, children: {} } - }; + } //first we make sure that list of items lists main section items and then sub sections, documents last const sortedItems = sortBy(navItems, ['isRootSection', 'weight', 'isSection']); @@ -15,7 +14,7 @@ function buildNavTree(navItems) { sortedItems.forEach(item => { //identify main sections if (item.isRootSection) { - tree[item.rootSectionId] = { item, children: {} }; + tree[item.rootSectionId] = { item, children: {} } } //identify subsections @@ -26,7 +25,6 @@ function buildNavTree(navItems) { tree[item.parent].children[item.sectionId] = { item, children: [] }; } - // Handle documents if (!item.isSection) { if (item.sectionId) { let section = tree[item.rootSectionId]?.children[item.sectionId]; @@ -53,7 +51,7 @@ function buildNavTree(navItems) { return obj; }, {}); - // Handling subsections + //handling subsections if (allChildrenKeys.length > 1) { for (const key of allChildrenKeys) { if (allChildren[key].children) { @@ -62,7 +60,7 @@ function buildNavTree(navItems) { }); } - // Point slug for specification subgroup to the latest specification version + // point in slug for specification subgroup to the latest specification version if (rootKey === 'reference' && key === 'specification') { allChildren[key].item.href = allChildren[key].children.find(c => c.isPrerelease === undefined).slug; } @@ -98,6 +96,7 @@ const convertDocPosts = (docObject) => { } } + function addDocButtons(docPosts, treePosts) { let structuredPosts = []; let rootSections = []; @@ -121,48 +120,47 @@ function addDocButtons(docPosts, treePosts) { // Traversing the structuredPosts in order to add `nextPage` and `prevPage` details for each page let countDocPages = structuredPosts.length; structuredPosts = structuredPosts.map((post, index) => { - // Post item specifying the root Section or sub-section in the docs are excluded as - // they don't comprise any Doc Page or content to be shown in website. + // post item specifying the root Section or sub-section in the docs are excluded as + // they doesn't comprise any Doc Page or content to be shown in website. if (post?.isRootSection || post?.isSection || index == 0) { - if (post?.isRootSection || index == 0) { - rootSections.push(post.title); - } - return post; + if (post?.isRootSection || index == 0) + rootSections.push(post.title) + return post } - let nextPage = {}, prevPage = {}; + let nextPage = {}, prevPage = {} let docPost = post; - // Checks whether the next page for the current docPost item exists or not + // checks whether the next page for the current docPost item exists or not if (index + 1 < countDocPages) { - // Checks whether the next item inside structuredPosts is a rootElement or a sectionElement - // If yes, it goes again to a next to next item in structuredPosts to link the nextPage + // checks whether the next item inside structuredPosts is a rootElement or a sectionElement + // if yes, it goes again to a next to next item in structuredPosts to link the nextPage if (!structuredPosts[index + 1].isRootElement && !structuredPosts[index + 1].isSection) { nextPage = { title: structuredPosts[index + 1].title, href: structuredPosts[index + 1].slug - }; + } } else { nextPage = { title: `${structuredPosts[index + 1].title} - ${structuredPosts[index + 2].title}`, href: structuredPosts[index + 2].slug - }; + } } - docPost = { ...docPost, nextPage }; + docPost = { ...docPost, nextPage } } - // Checks whether the previous page for the current docPost item exists or not + // checks whether the previous page for the current docPost item exists or not if (index > 0) { - // Checks whether the previous item inside structuredPosts is a rootElement or a sectionElement - // If yes, it goes again to a next previous item in structuredPosts to link the prevPage + // checks whether the previous item inside structuredPosts is a rootElement or a sectionElement + // if yes, it goes again to a next previous item in structuredPosts to link the prevPage if (!structuredPosts[index - 1]?.isRootElement && !structuredPosts[index - 1]?.isSection) { prevPage = { title: structuredPosts[index - 1].title, href: structuredPosts[index - 1].slug - }; - docPost = { ...docPost, prevPage }; + } + docPost = { ...docPost, prevPage } } else { - // Additional check for the first page of Docs so that it doesn't give any segmentation fault + // additonal check for the first page of Docs so that it doesn't give any Segementation fault if (index - 2 >= 0) { prevPage = { title: `${structuredPosts[index - 1]?.isRootSection ? rootSections[rootSections.length - 2] : rootSections[rootSections.length - 1]} - ${structuredPosts[index - 2].title}`,