Skip to content

Commit f166bb6

Browse files
authored
Merge pull request #10824 from marmelab/fix-documentation-storybook-detection
[Doc] Fix storybook detection and handling on client-side navigation
2 parents 98e30e0 + 67c78e5 commit f166bb6

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

docs/js/ra-navigation.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ function buildPageToC() {
6060
hasInnerContainers: true,
6161
});
6262

63-
const storybookPathMetaContent = document.querySelector(
63+
const storybookPathMetaElement = document.querySelector(
6464
STORYBOOK_PATH_META_SELECTOR
65-
).content;
65+
);
66+
let storybookPathMetaContent;
67+
if (storybookPathMetaElement) {
68+
storybookPathMetaContent = document.querySelector(
69+
STORYBOOK_PATH_META_SELECTOR
70+
).content;
71+
}
6672
const tocList = document.querySelector('.toc-list');
6773
if (!tocList || !storybookPathMetaContent) {
6874
return;
@@ -113,12 +119,24 @@ function replaceContent(text) {
113119
);
114120

115121
const newStorybookPathContent = newStorybookPathMeta?.content ?? '';
116-
document
117-
.querySelector(STORYBOOK_PATH_META_SELECTOR)
118-
.setAttribute('content', newStorybookPathContent);
122+
const storybookPathMetaElement = document.querySelector(
123+
STORYBOOK_PATH_META_SELECTOR
124+
);
125+
if (storybookPathMetaElement && newStorybookPathContent) {
126+
document
127+
.querySelector(STORYBOOK_PATH_META_SELECTOR)
128+
.setAttribute('content', newStorybookPathContent);
129+
} else if (newStorybookPathContent) {
130+
const metaElement = document.createElement('meta');
131+
metaElement.setAttribute('name', 'storybook_path');
132+
metaElement.setAttribute('content', newStorybookPathContent);
133+
document.head.appendChild(metaElement);
134+
} else {
135+
// Remove the meta element if it doesn't exist in the new content
136+
storybookPathMetaElement?.remove();
137+
}
119138

120139
window.scrollTo(0, 0);
121-
122140
buildPageToC();
123141

124142
navigationFitScroll();

0 commit comments

Comments
 (0)