bun-plugin-stx 0.2.312 → 0.2.314

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1673,7 +1673,7 @@ else {
1673
1673
  // Remove old page scripts
1674
1674
  document.querySelectorAll('script[data-stx-page]').forEach(function(s){s.remove()});
1675
1675
  if(pushState!==false)writeHistory(pushState,url+(hash||''));
1676
- updateNav(url);
1676
+ updateNav();
1677
1677
  updateActiveLinks();
1678
1678
  if(o.scrollToTop&&!hash)window.scrollTo({top:0,behavior:'instant'});
1679
1679
  else if(hash){var el=document.querySelector(hash);if(el)el.scrollIntoView({behavior:'smooth'})}
@@ -2110,7 +2110,7 @@ else {
2110
2110
  if(pushState!==false)writeHistory(pushState,url+(hash||''));
2111
2111
 
2112
2112
  // Update active nav links
2113
- updateNav(url);
2113
+ updateNav();
2114
2114
  updateActiveLinks();
2115
2115
 
2116
2116
  // Scroll
@@ -2507,33 +2507,59 @@ else {
2507
2507
  }
2508
2508
 
2509
2509
  // \u2500\u2500 Active link management \u2500\u2500
2510
- function updateNav(url){
2510
+ // Which links point at the page being shown. Every href is resolved against
2511
+ // the current URL first, because comparing the raw attribute with
2512
+ // location.pathname got most real links wrong:
2513
+ // - a query-only href (?range=24h, the shape of every range picker and tab
2514
+ // bar) never equalled a pathname, so the active one lost its classes and
2515
+ // had the server's aria-current="page" stripped on every load;
2516
+ // - /dashboard?site=1 was never an exact match for /dashboard;
2517
+ // - http://same-origin/x and /x/ were never matches for /x;
2518
+ // - /blog was "active" on /blogging, a prefix that is not a parent.
2519
+ // Null for links the router does not own (another origin, a bare #fragment,
2520
+ // anything unparseable), which are left exactly as the server rendered them.
2521
+ function normPath(p){return p.length>1&&p.charAt(p.length-1)==='/'?p.slice(0,-1):p}
2522
+ function linkState(href){
2523
+ if(!href||href.charAt(0)==='#')return null;
2524
+ var u;
2525
+ try{u=new URL(href,location.href)}catch(e){return null}
2526
+ if(u.origin!==location.origin)return null;
2527
+ var path=normPath(u.pathname),cur=normPath(location.pathname);
2528
+ // A link with no query matches on its path, so a nav entry stays current
2529
+ // while the page adds filters. A link WITH a query is current only when
2530
+ // every param it names has that value here: of five range links that all
2531
+ // point at this path, exactly one is.
2532
+ var have=new URLSearchParams(location.search),q=true;
2533
+ u.searchParams.forEach(function(v,k){if(have.getAll(k).indexOf(v)===-1)q=false});
2534
+ return {
2535
+ exact:q&&path===cur,
2536
+ active:q&&(path==='/'?cur==='/':(cur===path||cur.indexOf(path+'/')===0))
2537
+ };
2538
+ }
2539
+
2540
+ function updateNav(){
2511
2541
  document.querySelectorAll('nav a[href], #mobileNav a[href], [data-stx-nav] a[href]').forEach(function(a){
2512
- var href=a.getAttribute('href');
2513
- if(!href||href.startsWith('#')||href.startsWith('http'))return;
2514
- var isActive=(href===url)||(href==='/'&&url==='/');
2515
- if(a.hasAttribute('data-stx-link')){
2516
- var ac=a.getAttribute('data-stx-active-class')||'active';
2517
- if(isActive)ac.split(' ').forEach(function(cls){if(cls)a.classList.add(cls)});else ac.split(' ').forEach(function(cls){if(cls)a.classList.remove(cls)});
2518
- }
2542
+ if(!a.hasAttribute('data-stx-link'))return;
2543
+ var st=linkState(a.getAttribute('href'));
2544
+ if(!st)return;
2545
+ var ac=a.getAttribute('data-stx-active-class')||'active';
2546
+ if(st.exact)ac.split(' ').forEach(function(cls){if(cls)a.classList.add(cls)});else ac.split(' ').forEach(function(cls){if(cls)a.classList.remove(cls)});
2519
2547
  });
2520
2548
  }
2521
2549
 
2522
2550
  function updateActiveLinks(){
2523
2551
  // Update active classes on <stx-link> elements (and legacy data-stx-link)
2524
2552
  var links=document.querySelectorAll('[data-stx-link]');
2525
- var cur=location.pathname;
2526
2553
  links.forEach(function(link){
2527
- var href=link.getAttribute('to')||link.getAttribute('href')||'';
2554
+ var st=linkState(link.getAttribute('to')||link.getAttribute('href')||'');
2555
+ if(!st)return;
2528
2556
  var ac=link.getAttribute('active-class')||link.getAttribute('data-stx-active-class')||'active';
2529
2557
  var eac=link.getAttribute('exact-active-class')||link.getAttribute('data-stx-exact-active-class')||'exact-active';
2530
2558
  ac.split(' ').forEach(function(cls){if(cls)link.classList.remove(cls)});
2531
2559
  eac.split(' ').forEach(function(cls){if(cls)link.classList.remove(cls)});
2532
- var isExact=cur===href;
2533
- var isActive=href!=='/'?cur.startsWith(href):cur==='/';
2534
- if(isExact)eac.split(' ').forEach(function(cls){if(cls)link.classList.add(cls)});
2535
- if(isActive)ac.split(' ').forEach(function(cls){if(cls)link.classList.add(cls)});
2536
- markCurrent(link,isExact);
2560
+ if(st.exact)eac.split(' ').forEach(function(cls){if(cls)link.classList.add(cls)});
2561
+ if(st.active)ac.split(' ').forEach(function(cls){if(cls)link.classList.add(cls)});
2562
+ markCurrent(link,st.exact);
2537
2563
  });
2538
2564
  }
2539
2565
 
@@ -2657,7 +2683,7 @@ else {
2657
2683
  injectStyles();
2658
2684
  injectViewTransitionCSS();
2659
2685
  updateActiveLinks();
2660
- updateNav(location.pathname);
2686
+ updateNav();
2661
2687
  }
2662
2688
 
2663
2689
  if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',init);else init();
package/dist/serve.js CHANGED
@@ -9266,7 +9266,6 @@ Examples:`);
9266
9266
  After installing: bun add bun-plugin-stx`);
9267
9267
  throw new Error("No file patterns provided");
9268
9268
  }
9269
- const routes = new Map;
9270
9269
  let sourceFiles = null;
9271
9270
  let discoveredPagePaths = null;
9272
9271
  let assetsInitialized = false;
@@ -9387,7 +9386,6 @@ function __stxOverlay(errs){
9387
9386
  if (isStxLike || isCode || isData) {
9388
9387
  if (isStxLike)
9389
9388
  sourceFiles = null;
9390
- routes.clear();
9391
9389
  htmlCache.clear();
9392
9390
  cssCssCache.clear();
9393
9391
  resolvedStxModule?.clearDevCaches?.();
@@ -9726,7 +9724,7 @@ function __stxOverlay(errs){
9726
9724
  pageMiddlewarePatterns.length = 0;
9727
9725
  await Promise.all(files.map((f) => detectPageMiddleware(f)));
9728
9726
  try {
9729
- const { Router } = await import("./chunk-zt4e86p1.js");
9727
+ const { Router } = await import("./chunk-3c6zbghg.js");
9730
9728
  const pagesDirs = patterns.map((p) => p.replace(/\/$/, "")).filter(Boolean);
9731
9729
  const router = new Router(process19.cwd(), { pagesDirs, stateDir: stateDirName() });
9732
9730
  if (!options.quiet)
@@ -10076,14 +10074,12 @@ function __stxOverlay(errs){
10076
10074
  const isRootRequest = requestPath === "/" || normalizedPath === "";
10077
10075
  if (`/${fileRoute}` === requestPath || fileRoute === normalizedPath || `/${fileRoute}.stx` === requestPath || `/${fileRoute}.md` === requestPath || `/${fileRoute}.html` === requestPath || isIndexFile && isRootRequest) {
10078
10076
  const output = await processTemplate(filePath, reqCtx);
10079
- routes.set(requestPath, output);
10080
10077
  return output;
10081
10078
  }
10082
10079
  if (fileRoute.startsWith("pages/")) {
10083
10080
  const prettyRoute = fileRoute.slice(6);
10084
10081
  if (`/${prettyRoute}` === requestPath || prettyRoute === normalizedPath) {
10085
10082
  const output = await processTemplate(filePath, reqCtx);
10086
- routes.set(requestPath, output);
10087
10083
  return output;
10088
10084
  }
10089
10085
  }
@@ -10115,7 +10111,6 @@ function __stxOverlay(errs){
10115
10111
  const paramNames = [...fileRouteBase.matchAll(/\[([^\]]+)\]/g)].map((m) => m[1].replace(/^\.\.\./, ""));
10116
10112
  const paramValues = match.slice(1);
10117
10113
  const output = await processTemplateDynamic(filePath, paramNames, paramValues, normalizedPath, reqCtx);
10118
- routes.set(requestPath, output);
10119
10114
  return output;
10120
10115
  }
10121
10116
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.312",
4
+ "version": "0.2.314",
5
5
  "description": "A Bun plugin that allows for using Laravel Blade-like syntax.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",