htmx-router 0.0.6 → 0.0.8

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.
@@ -12,6 +12,7 @@ export declare enum MaskType {
12
12
  export declare class RenderArgs {
13
13
  req: http.IncomingMessage;
14
14
  res: http.ServerResponse;
15
+ title: string;
15
16
  params: MetaHTML;
16
17
  url: URL;
17
18
  shared: {
@@ -23,11 +24,12 @@ export declare class RenderArgs {
23
24
  _maskChain: MaskType[];
24
25
  _maxChain: number;
25
26
  constructor(req: http.IncomingMessage, res: http.ServerResponse, url: URL);
26
- addLinks(links: MetaHTML[], override?: boolean): void;
27
- addMeta(links: MetaHTML[], override?: boolean): void;
27
+ setTitle: (value: string) => void;
28
+ addLinks: (links: MetaHTML[], override?: boolean) => void;
29
+ addMeta: (links: MetaHTML[], override?: boolean) => void;
28
30
  Outlet: () => Promise<string>;
29
31
  _addOutlet(route: RouteLeaf): void;
30
32
  _applyMask(mask: boolean[], depth: number): void;
31
- renderHeadHTML(): string;
33
+ renderHeadHTML: () => string;
32
34
  }
33
35
  export {};
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RenderArgs = exports.MaskType = void 0;
4
+ const titleScript = `<script>` +
5
+ `document.addEventListener("DOMContentLoaded",function(){` +
6
+ `document.body.addEventListener("setTitle",function(evt){document.title = evt.detail.value;})` +
7
+ `});` +
8
+ `</script>`;
4
9
  const attrRegex = /^[A-z][A-z\-0-9]+$/;
5
10
  function ValidateMetaHTML(val) {
6
11
  for (const key in val) {
@@ -24,6 +29,29 @@ var MaskType;
24
29
  })(MaskType || (exports.MaskType = MaskType = {}));
25
30
  class RenderArgs {
26
31
  constructor(req, res, url) {
32
+ this.setTitle = (value) => {
33
+ this.title = value;
34
+ };
35
+ this.addLinks = (links, override = false) => {
36
+ if (!ValidateMetaHTMLs(links))
37
+ throw new Error(`Provided links have invalid attribute`);
38
+ if (override) {
39
+ this.links = links;
40
+ }
41
+ else {
42
+ this.links.push(...links);
43
+ }
44
+ };
45
+ this.addMeta = (links, override = false) => {
46
+ if (!ValidateMetaHTMLs(links))
47
+ throw new Error(`Provided links have invalid attribute`);
48
+ if (override) {
49
+ this.meta = links;
50
+ }
51
+ else {
52
+ this.meta.push(...links);
53
+ }
54
+ };
27
55
  // unpacking Outlet caused this to be undefined
28
56
  // hence the weird def
29
57
  this.Outlet = () => {
@@ -38,10 +66,29 @@ class RenderArgs {
38
66
  const routeName = `hx-route-${depth.toString(16)}`;
39
67
  return route.render(this, mask, routeName);
40
68
  };
69
+ this.renderHeadHTML = () => {
70
+ let out = "";
71
+ for (const elm of this.links) {
72
+ out += "<link";
73
+ for (const attr in elm) {
74
+ out += ` ${attr}="${elm[attr].replace(/"/g, "\\\"")}"`;
75
+ }
76
+ out += "></link>";
77
+ }
78
+ for (const elm of this.meta) {
79
+ out += "<meta";
80
+ for (const attr in elm) {
81
+ out += ` ${attr}="${elm[attr].replace(/"/g, "\\\"")}"`;
82
+ }
83
+ out += "></meta>";
84
+ }
85
+ return out + titleScript;
86
+ };
41
87
  this.req = req;
42
88
  this.res = res;
43
89
  this.url = url;
44
90
  this.params = {};
91
+ this.title = "";
45
92
  this.shared = {};
46
93
  this.links = [];
47
94
  this.meta = [];
@@ -49,32 +96,14 @@ class RenderArgs {
49
96
  this._maskChain = [];
50
97
  this._maxChain = 0;
51
98
  }
52
- addLinks(links, override = false) {
53
- if (!ValidateMetaHTMLs(links))
54
- throw new Error(`Provided links have invalid attribute`);
55
- if (override) {
56
- this.links = links;
57
- }
58
- else {
59
- this.links.push(...links);
60
- }
61
- }
62
- addMeta(links, override = false) {
63
- if (!ValidateMetaHTMLs(links))
64
- throw new Error(`Provided links have invalid attribute`);
65
- if (override) {
66
- this.meta = links;
67
- }
68
- else {
69
- this.meta.push(...links);
70
- }
71
- }
72
99
  _addOutlet(route) {
73
100
  this._outletChain.push(route);
74
101
  }
75
102
  _applyMask(mask, depth) {
76
103
  const padded = new Array(this._outletChain.length - mask.length).fill(false);
77
- padded.push(...mask);
104
+ for (let i = mask.length - 1; i >= 0; i--) {
105
+ padded.push(mask[i]);
106
+ }
78
107
  this._maskChain = padded
79
108
  .map((x, i) => x === true ?
80
109
  MaskType.hide :
@@ -83,23 +112,5 @@ class RenderArgs {
83
112
  MaskType.headless);
84
113
  this._maxChain = this._maskChain.length;
85
114
  }
86
- renderHeadHTML() {
87
- let out = "";
88
- for (const elm of this.links) {
89
- out += "<link";
90
- for (const attr in elm) {
91
- out += ` ${attr}="${elm[attr].replace(/"/g, "\\\"")}"`;
92
- }
93
- out += "></link>";
94
- }
95
- for (const elm of this.meta) {
96
- out += "<meta";
97
- for (const attr in elm) {
98
- out += ` ${attr}="${elm[attr].replace(/"/g, "\\\"")}"`;
99
- }
100
- out += "></meta>";
101
- }
102
- return out;
103
- }
104
115
  }
105
116
  exports.RenderArgs = RenderArgs;
package/bin/router.js CHANGED
@@ -95,10 +95,12 @@ class RouteTree {
95
95
  path = path.split(/[\./\\]/g);
96
96
  }
97
97
  if (path.length === 0) {
98
+ override.push(false);
98
99
  this.route = new RouteLeaf(module, override);
99
100
  return;
100
101
  }
101
102
  if (path.length === 1 && path[0] === "_index") {
103
+ override.push(false);
102
104
  this.default = new RouteLeaf(module, override);
103
105
  return;
104
106
  }
@@ -165,13 +167,27 @@ class RouteTree {
165
167
  try {
166
168
  const depth = BuildOutlet(this, args, from);
167
169
  if (from) {
168
- res.setHeader('HX-Replace-Url', req.url || "/");
170
+ res.setHeader('HX-Push-Url', req.url || "/");
169
171
  if (depth > 0) {
170
172
  res.setHeader('HX-Retarget', `#hx-route-${depth.toString(16)}`);
171
173
  }
172
174
  res.setHeader('HX-Reswap', "outerHTML");
173
175
  }
174
- return await args.Outlet();
176
+ const out = await args.Outlet();
177
+ if (args.title) {
178
+ const trigger = res.getHeader('HX-Trigger');
179
+ const entry = `{"setTitle":"${args.title.replace(/"/g, `\\"`)}"}`;
180
+ if (Array.isArray(trigger)) {
181
+ res.setHeader('HX-Trigger', [...trigger, entry]);
182
+ }
183
+ else if (trigger) {
184
+ res.setHeader('HX-Trigger', [trigger.toString(), entry]);
185
+ }
186
+ else {
187
+ res.setHeader('HX-Trigger', [entry]);
188
+ }
189
+ }
190
+ return out;
175
191
  }
176
192
  catch (e) {
177
193
  if (e instanceof shared_1.Redirect)
@@ -255,7 +271,7 @@ function BuildOutlet(start, args, fromPath) {
255
271
  }
256
272
  }
257
273
  if (matching) {
258
- depth = args._outletChain.length;
274
+ depth = args._outletChain.length - 1;
259
275
  }
260
276
  args._applyMask(mask, depth);
261
277
  return depth;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "htmx-router",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "A remix.js style file path router for htmX websites",
5
5
  "main": "./bin/index.js",
6
6
  "scripts": {