From 67c78e54fd1f704bea14048f131a1e3f1ec13044 Mon Sep 17 00:00:00 2001 From: Gildas <1122076+djhi@users.noreply.github.com> Date: Thu, 3 Jul 2025 17:27:14 +0200 Subject: [PATCH] [Doc] Fix storybook detection and handling on client-side navigation --- docs/js/ra-navigation.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/docs/js/ra-navigation.js b/docs/js/ra-navigation.js index edd36cf86c2..feb50b68b2d 100644 --- a/docs/js/ra-navigation.js +++ b/docs/js/ra-navigation.js @@ -60,9 +60,15 @@ function buildPageToC() { hasInnerContainers: true, }); - const storybookPathMetaContent = document.querySelector( + const storybookPathMetaElement = document.querySelector( STORYBOOK_PATH_META_SELECTOR - ).content; + ); + let storybookPathMetaContent; + if (storybookPathMetaElement) { + storybookPathMetaContent = document.querySelector( + STORYBOOK_PATH_META_SELECTOR + ).content; + } const tocList = document.querySelector('.toc-list'); if (!tocList || !storybookPathMetaContent) { return; @@ -113,12 +119,24 @@ function replaceContent(text) { ); const newStorybookPathContent = newStorybookPathMeta?.content ?? ''; - document - .querySelector(STORYBOOK_PATH_META_SELECTOR) - .setAttribute('content', newStorybookPathContent); + const storybookPathMetaElement = document.querySelector( + STORYBOOK_PATH_META_SELECTOR + ); + if (storybookPathMetaElement && newStorybookPathContent) { + document + .querySelector(STORYBOOK_PATH_META_SELECTOR) + .setAttribute('content', newStorybookPathContent); + } else if (newStorybookPathContent) { + const metaElement = document.createElement('meta'); + metaElement.setAttribute('name', 'storybook_path'); + metaElement.setAttribute('content', newStorybookPathContent); + document.head.appendChild(metaElement); + } else { + // Remove the meta element if it doesn't exist in the new content + storybookPathMetaElement?.remove(); + } window.scrollTo(0, 0); - buildPageToC(); navigationFitScroll();