Skip to content
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

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-ligers-cheat.md
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.
7 changes: 4 additions & 3 deletions packages/documentation/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StorybookConfig } from '@storybook/web-components-vite';
import pkg from '../package.json';
import remarkAutolinkHeadings from 'remark-autolink-headings';
import { mergeConfig } from 'vite';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
Copy link
Contributor Author

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)


const config: StorybookConfig = {
logLevel: 'info',
Expand Down Expand Up @@ -29,15 +29,16 @@ const config: StorybookConfig = {
options: {
mdxPluginOptions: {
mdxCompileOptions: {
remarkPlugins: [
rehypePlugins: [
[
remarkAutolinkHeadings,
rehypeAutolinkHeadings,
{
content: {
type: 'element',
tagName: 'post-icon',
properties: { name: 2037 },
},
headingProperties: { className: 'docs-autolink' },
behavior: 'append',
},
],
Expand Down
3 changes: 2 additions & 1 deletion packages/documentation/.storybook/manager-head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script src="/assets/scripts/storybook-events.js"></script>
<script src="/assets/scripts/storybook-manager-events.js"></script>
<script src="/assets/scripts/autolink.js"></script>
<script src="/assets/scripts/analytics-helper.js"></script>
<script src="/assets/scripts/analytics-events.js"></script>

Expand Down
1 change: 1 addition & 0 deletions packages/documentation/.storybook/preview-body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="/assets/scripts/storybook-preview-events.js"></script>
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

18 changes: 0 additions & 18 deletions packages/documentation/.storybook/preview-head.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,3 @@
<script>
window.global = window;
</script>

<!-- Change base location of iframe to get relative parent anchor link and not relative to iframe url. Source: https://stackoverflow.com/a/68418536 -->
<script>
const onChangeState = () => {
const baseEl = document.querySelector('base');
baseEl.setAttribute('href', window.parent.location.href);
};

['pushState', 'replaceState'].forEach(changeState => {
window.history[changeState] = new Proxy(window.history[changeState], {
apply(target, thisArg, argList) {
onChangeState();

return target.apply(thisArg, argList);
},
});
});
</script>
1 change: 1 addition & 0 deletions packages/documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-syntax-highlighter": "15.5.0",
"rehype-autolink-headings": "^7.1.0",
"remark-autolink-headings": "7.0.1",
"rimraf": "5.0.5",
"sass": "1.69.7",
Expand Down
11 changes: 11 additions & 0 deletions packages/documentation/public/assets/scripts/autolink.js
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 () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
96 changes: 96 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading