From f620d0045e3ab97095d8b71b8bcf0e3c386104e7 Mon Sep 17 00:00:00 2001 From: Debbie Matthews Date: Wed, 3 Apr 2024 21:55:46 -0700 Subject: [PATCH] Skip over dividers when finding next/prev page (#1021) * Skip over dividers when finding next/prev page * Designate base 10 for parseInt --- lib/utils.js | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 8999e6efe..a29535130 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -38,7 +38,7 @@ function getPreviousNextFromMenu(menu, slugStr, parent) { for (let index = 0; index < menu.length; index++) { // If we are inside top-level category, point them to the next top-level category if (current && !next) { - next = menu[index]; + next = menu[index].url ? menu[index] : menu[index + 1]; } if (current) { continue; @@ -46,28 +46,35 @@ function getPreviousNextFromMenu(menu, slugStr, parent) { // For top-level categories if (menu[index].url === slugStr) { - // If we are NOT on the first top-level category (Streamlit Library), point the previous arrow - // to the previous parent - // Example: Streamlit Community Cloud points to Streamlit Library. - // Example: Knowledge Base points to Streamlit Community Cloud + // If we are NOT on the first top-level category (Get started), point the + // previous arrow to the parent + // Example: Develop points to Get started. + // Example: Deploy points to Develop if (index < 1) { prev = parent; + // If we are on the second item within a category and the first item is + // a divider, skip the divider and point to the parent + } else if (index === 1 && !menu[index - 1].url) { + prev = parent; + // If the current item is preceded by a divider, skip the divider and + // point to the item before the divider + } else if (!menu[index - 1].url) { + prev = menu[index - 2]; } else { prev = menu[index - 1]; } - // If we are on a top-level category, the next item should be its first child - // Example: Streamlit Library -> Get Started - // Example: Streamlit Community Cloud -> Community - if (parseInt(menu[index].depth) === 0) { - next = menu[index].children[0]; - } - - // If we are on a sub item that's expandable, then its first item should be its direct descendant - // Example: Tutorials -> Connect to data sources - // Example: Community -> Sign up - if (menu[index].children && menu[index].children.length > 0) { - next = menu[index].children[0]; + // If we are on a top-level category or expandable item, the next item + // should be its first child. Skip over a divider if it's the first child. + // Example: Get started -> Installation + // Example: API reference -> Write and magic + const isTopLevel = parseInt(menu[index].depth, 10) === 0; + const isExpandable = + menu[index].children && menu[index].children.length > 0; + if (isTopLevel || isExpandable) { + next = menu[index].children[0].url + ? menu[index].children[0] + : menu[index].children[1]; } current = menu[index]; @@ -78,7 +85,7 @@ function getPreviousNextFromMenu(menu, slugStr, parent) { ({ current, prev, next } = getPreviousNextFromMenu( menu[index].children, slugStr, - menu[index] + menu[index], )); } }