Skip to content

Commit 0424811

Browse files
committed
Add URL-based tab state sync for WASI P2/P3 tabs
Supports shareable deep links with a ?wasi=p2 or ?wasi=p3 query parameter. On page load, the parameter selects the matching tab (and persists via the existing localStorage mechanism). Clicking a tab updates the URL via history.replaceState. - Add theme/tabs-url-sync.js alongside the mdbook-tabs plugin's tabs.js. The companion script runs after the plugin's handler, reads the URL on load, and listens for tab clicks via delegation. - Register the new file in book.toml under additional-js - Backward compatible: pages without the parameter retain the existing localStorage-based behavior
1 parent 1a4c0ff commit 0424811

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

component-model/book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ title = "The WebAssembly Component Model"
99
git-repository-url = "https://github.com/bytecodealliance/component-docs/tree/main/component-model"
1010
edit-url-template = "https://github.com/bytecodealliance/component-docs/tree/main/component-model/{path}"
1111
additional-css = ["theme/head.hbs", "theme/tabs.css", "theme/version-notice.css"]
12-
additional-js = ["theme/tabs.js"]
12+
additional-js = ["theme/tabs.js", "theme/tabs-url-sync.js"]
1313

1414
[output.html.fold]
1515
enable = true
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(function () {
2+
'use strict';
3+
4+
const GLOBAL_NAME = 'wasi-version';
5+
const PARAM_NAME = 'wasi';
6+
const SLUG_TO_NAME = { p2: 'WASI P2', p3: 'WASI P3' };
7+
const NAME_TO_SLUG = { 'WASI P2': 'p2', 'WASI P3': 'p3' };
8+
9+
function readSlugFromUrl() {
10+
const value = new URLSearchParams(window.location.search).get(PARAM_NAME);
11+
return value && SLUG_TO_NAME[value] ? value : null;
12+
}
13+
14+
function writeSlugToUrl(slug) {
15+
const url = new URL(window.location.href);
16+
url.searchParams.set(PARAM_NAME, slug);
17+
history.replaceState(null, '', url.toString());
18+
}
19+
20+
function activateByName(name) {
21+
const tab = document.querySelector(
22+
'.mdbook-tabs-container[data-tabglobal="' + GLOBAL_NAME + '"] ' +
23+
'.mdbook-tab[data-tabname="' + name + '"]'
24+
);
25+
if (tab) {
26+
tab.click();
27+
}
28+
}
29+
30+
document.addEventListener('DOMContentLoaded', function () {
31+
const slug = readSlugFromUrl();
32+
if (slug) {
33+
activateByName(SLUG_TO_NAME[slug]);
34+
}
35+
36+
document.addEventListener('click', function (event) {
37+
const tab = event.target.closest && event.target.closest('.mdbook-tab');
38+
if (!tab) return;
39+
const container = tab.closest('.mdbook-tabs-container');
40+
if (!container || container.dataset.tabglobal !== GLOBAL_NAME) return;
41+
const newSlug = NAME_TO_SLUG[tab.dataset.tabname];
42+
if (newSlug) {
43+
writeSlugToUrl(newSlug);
44+
}
45+
});
46+
});
47+
})();

0 commit comments

Comments
 (0)