-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(documentation): Autolink with base url disrupt other anchor links #2529
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@swisspost/design-system-documentation': patch | ||
--- | ||
|
||
Fixed conflict between autolink anchor links and normal anchor links. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<script src="/assets/scripts/storybook-preview-events.js"></script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could have been part of the manager, but it would have required more work on the observer and I think this event could be beneficial as well for e2e tests, for example, where we only load the iframe not the whole storybook. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Change base location of iframe to get relative parent anchor link and not relative to iframe url. | ||
window.addEventListener('storybook:contentReady', function () { | ||
const previewIframe = document.getElementById('storybook-preview-iframe'); | ||
let links = previewIframe.contentDocument.querySelectorAll('.docs-autolink a'); | ||
links.forEach(link => { | ||
const anchor = link.getAttribute('href'); | ||
if (anchor.startsWith('#')) { | ||
link.setAttribute('href', `${window.parent.location.href}${anchor}`); | ||
} | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const previewIframe = document.body; | ||
let footerEl = document.querySelector('.docs-footer'); | ||
let currentPage = null; | ||
|
||
if (footerEl) { | ||
// if footer already exists, just emit ready event and listen for route-changes | ||
contentReady(); | ||
} else { | ||
// if footer does not exist yet, wait until its rendered, then emit contentReady event and listen for route-changes | ||
new MutationObserver(function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, we have to make all this code to wait until the footer is available. Maybe after storybookjs/storybook#24429, we could simply put the script at the end of the body and simplify it? |
||
footerEl = document.querySelector('.docs-footer'); | ||
|
||
if (footerEl && isNewPage()) { | ||
contentReady(); | ||
currentPage = window.parent.location.href; | ||
} | ||
}).observe(previewIframe, { | ||
childList: true, | ||
subtree: true, | ||
}); | ||
} | ||
|
||
function contentReady() { | ||
window.dispatchEvent(new Event('storybook:contentReady')); | ||
window.parent.dispatchEvent(new Event('storybook:contentReady')); | ||
} | ||
|
||
function isNewPage() { | ||
return currentPage !== window.parent.location.href; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had another try and I managed to make it work with this plugin which replace the legacy
remark-autolink-headings
(see https://github.com/remarkjs/remark-autolink-headings)