From 8caceb0457e65cee12286e713817dba3a6954ea6 Mon Sep 17 00:00:00 2001 From: Max Edell Date: Wed, 4 Oct 2023 16:43:05 -0700 Subject: [PATCH] fix: disallow repeated segments at end of path --- blocks/sidenav/sidenav.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/blocks/sidenav/sidenav.js b/blocks/sidenav/sidenav.js index 60f84ec..9c7d797 100644 --- a/blocks/sidenav/sidenav.js +++ b/blocks/sidenav/sidenav.js @@ -591,11 +591,11 @@ function bookToList(book) { // add the chapter const chapterUl = addSubList( chapter.name, - `${book.path}/${chapter.key}/${chapter.key}`, + `${book.path}/${chapter.key}`, chapter.key, ); - const makeHref = (topic, parentKey) => `${book.path}/${chapter.key}/${parentKey ? `${parentKey}/` : ''}${topic.key}`; + const makeHref = (topic, parentKey) => `${book.path}/${chapter.key}/${parentKey ? (`${parentKey}${parentKey.endsWith(`/${topic.key}`) ? '' : `/${topic.key}`}`) : topic.key}`; // then the topics recursively const processTopic = (topic, parentKey) => { @@ -621,18 +621,19 @@ function bookToList(book) { addSubList( topic.name, - `${book.path}/${chapter.key}/${parentKey ? `${parentKey}/` : ''}${topic.key}/${ - topic.key - }`, + makeHref(topic, parentKey), topic.key, ); topic.children.forEach((subtopic) => { + if (subtopic.key === topic.key && !subtopic.children) { + // uncomment to omit entries in sidenav with same link as parent + // return; + } + processTopic( subtopic, - `${parentKey ? `${parentKey}/` : ''}${topic.key}${ - subtopic.parent ? `/${subtopic.parent}` : '' - }`, + `${parentKey ? `${parentKey}/` : ''}${topic.key}`, ); });