From 75617de644fbdad1daef855d16032a69fa4518b5 Mon Sep 17 00:00:00 2001 From: Jason Karlavige Date: Tue, 16 Apr 2024 08:23:06 -0400 Subject: [PATCH] remove trailing slashes from toc links --- website/src/theme/DocItem/Layout/index.js | 11 ++++++++--- website/src/utils/remove-trailing-slashes.js | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 website/src/utils/remove-trailing-slashes.js diff --git a/website/src/theme/DocItem/Layout/index.js b/website/src/theme/DocItem/Layout/index.js index 2ee1f8bf156..d1995bd0839 100644 --- a/website/src/theme/DocItem/Layout/index.js +++ b/website/src/theme/DocItem/Layout/index.js @@ -27,6 +27,7 @@ import {ThemeClassNames} from '@docusaurus/theme-common'; import VersionContext from '../../../stores/VersionContext' import getElements from '../../../utils/get-html-elements'; import useHashLink from '../../../utils/use-hash-link'; +import removeTrailingDashes from '../../../utils/remove-trailing-slashes'; /** * Decide if the toc should be rendered, on mobile or desktop viewports @@ -50,7 +51,7 @@ function useDocTOC() { ); // Headings to remove from TOC - const headingsToFilter = await getElements( + const headingsToFilter = document.querySelectorAll( ".tabs-container h2, .tabs-container h3, .expandable-anchor h2, .expandable-anchor h3" ); @@ -84,9 +85,13 @@ function useDocTOC() { level = null; } + // Remove trailing slashes from TOC item + // Trailing slashes come from the LifeCycle pill use on headers + const updatedHeading = removeTrailingDashes(heading) + return { - value: heading.innerHTML, - id: heading.id, + value: updatedHeading.innerHTML, + id: updatedHeading.id, level: level && level, }; }; diff --git a/website/src/utils/remove-trailing-slashes.js b/website/src/utils/remove-trailing-slashes.js new file mode 100644 index 00000000000..549e5193429 --- /dev/null +++ b/website/src/utils/remove-trailing-slashes.js @@ -0,0 +1,19 @@ +// Util function to remove trailing dashes from ID attribute on headers +export default function removeTrailingDashes(header) { + // Create copy of header element + const updatedHeader = header; + + // Get id attribute + const thisId = updatedHeader?.getAttribute("id"); + + // If header's id ends with trailing dash, remove dash + if (thisId?.endsWith("-")) { + // Remove `-` from end of ID string + updatedHeader.id = thisId?.substring(0, thisId?.length - 1); + + // Recursively run function to check for another trailing slash + removeTrailingDashes(updatedHeader); + } + + return updatedHeader; +}