handoff-app-next 2.0.0-canary.21 → 2.0.0-canary.22

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.
@@ -1,4 +1,4 @@
1
1
  /** Semver + alpha suffix stamped at build time. e.g. "2.0.0-alpha.20260609.da2f104f" */
2
- export declare const BUILD_VERSION = "2.0.0-alpha.20260922.693deaab";
2
+ export declare const BUILD_VERSION = "2.0.0-alpha.20260923.0638c55a";
3
3
  /** ISO 8601 timestamp of this build. */
4
- export declare const BUILD_DATE = "2026-09-23T02:02:04.361Z";
4
+ export declare const BUILD_DATE = "2026-09-23T18:07:20.845Z";
@@ -1,6 +1,6 @@
1
1
  // AUTO-GENERATED by scripts/write-build-meta.mjs — do not edit.
2
2
  // Regenerated on every `npm run build`.
3
3
  /** Semver + alpha suffix stamped at build time. e.g. "2.0.0-alpha.20260609.da2f104f" */
4
- export const BUILD_VERSION = "2.0.0-alpha.20260922.693deaab";
4
+ export const BUILD_VERSION = "2.0.0-alpha.20260923.0638c55a";
5
5
  /** ISO 8601 timestamp of this build. */
6
- export const BUILD_DATE = "2026-09-23T02:02:04.361Z";
6
+ export const BUILD_DATE = "2026-09-23T18:07:20.845Z";
@@ -1,7 +1,40 @@
1
1
  /**
2
- * Trims HTML preview content by extracting body content
3
- * @param preview - The HTML preview string
4
- * @returns Trimmed HTML content
2
+ * Take everything between `<body>` and `</body>`.
3
+ *
4
+ * ## Why this parses, and why it masks first
5
+ *
6
+ * The parse earns its place: `parse(…).querySelector('body').innerHTML` also *normalises* what it
7
+ * re-serializes — `<path … />` becomes `<path …></path>`, an implicitly-closed `<p>` gains its
8
+ * `</p>` — and that normalised form is what the reviewed HubSpot modules in production contain. A
9
+ * plain string slice is faithful to the template but diverges from production on every component
10
+ * with an inline SVG: 37 of one registry's 69 published modules, 76 tag tokens.
11
+ *
12
+ * What the parse must *not* do is read the template's Handlebars as markup. `preview` is very often
13
+ * not HTML: for a Handlebars component it is `template.hbs`, and this function's output becomes
14
+ * `componentData.code` — the source every downstream consumer transpiles, including the HubSpot
15
+ * module emitter. node-html-parser has no concept of `{{!-- … --}}`, so an HTML tag *named* inside a
16
+ * Handlebars comment is a real tag to it:
17
+ *
18
+ * {{!-- A navigation item with no url renders dead rather than disappearing. An <a> without
19
+ * href is not focusable or clickable. --}}
20
+ * <a {{#if this.url}}href="{{this.url}}" {{/if}}class="accordion-button">…</a>
21
+ *
22
+ * The parser opens a phantom `<a>` from the prose, spends the template's real `</a>` closing it, and
23
+ * rebalances the rest of the document around the genuine `<a>` that is now unclosed — turning
24
+ * ancestor `<div>` openers into closers and discarding the ones it can no longer match. The result
25
+ * stays balanced, so nothing downstream can tell. One registry's nav component came back with 32
26
+ * `<div>` pairs instead of 35 and 4 `<h6>` pairs instead of 5; the mobile drawer's flex wrapper
27
+ * vanished outright and its contents rendered as siblings of the drawer on the live site.
28
+ *
29
+ * Masking the Handlebars first takes away the parser's ability to see inside one, and keeps the
30
+ * normalisation. Comments are the expensive case but not the only one — a `<` inside a helper
31
+ * argument, `{{concat "<span>" x}}`, is eaten exactly the same way — so every construct is masked
32
+ * except the one in {@link maskHandlebars} that must not be.
33
+ *
34
+ * Non-HTML input (a React component's source, say) has no `<body>` and comes back untouched.
35
+ *
36
+ * @param preview - The HTML (or template) string
37
+ * @returns The body's contents, trimmed; the whole input when there is no `<body>`
5
38
  */
6
39
  export declare const trimPreview: (preview: string) => string;
7
40
  /**
@@ -1,9 +1,113 @@
1
1
  import { parse } from 'node-html-parser';
2
2
  import prettier from 'prettier';
3
+ const BODY_OPEN = /<body(?=[\s/>])[^>]*>/i;
4
+ const BODY_CLOSE = '</body>';
3
5
  /**
4
- * Trims HTML preview content by extracting body content
5
- * @param preview - The HTML preview string
6
- * @returns Trimmed HTML content
6
+ * Every Handlebars construct, longest delimiter first so `{{!-- … --}}`, `{{{{raw}}}}` and
7
+ * `{{{ … }}}` are each claimed whole before the plain `{{ … }}` alternative can bite off a prefix.
8
+ */
9
+ const HANDLEBARS = /\{\{!--[\s\S]*?--\}\}|\{\{!(?!--)[\s\S]*?\}\}|\{\{\{\{[\s\S]*?\}\}\}\}|\{\{\{[\s\S]*?\}\}\}|\{\{[\s\S]*?\}\}/g;
10
+ /**
11
+ * Replace every Handlebars construct with an opaque token, so the HTML parser cannot read markup
12
+ * that exists only inside one.
13
+ *
14
+ * Three properties make the round trip exact:
15
+ *
16
+ * 1. **Inert.** A token is `[a-z]` and digits only — no `<`, `>`, `&`, quote or whitespace. An HTML
17
+ * serializer has nothing to escape, normalise or re-order, and that holds in text, in an
18
+ * attribute value, and in attribute-*name* position alike — which is where
19
+ * `<a {{#if this.url}}href="{{this.url}}" {{/if}}class="nav-link">` puts one.
20
+ * 2. **Collision-free.** The nonce grows until it occurs nowhere in the input, so nothing the author
21
+ * wrote can be mistaken for a token on the way back.
22
+ * 3. **Unambiguous.** The nonce never ends in a digit, so `<nonce>(\d+)<nonce>` cannot straddle two
23
+ * adjacent tokens.
24
+ *
25
+ * ## The one construct that is left alone
26
+ *
27
+ * A construct in **tag-name** position — `<{{properties.header}} class="mb-0">` — must stay
28
+ * unmasked. Unmasked, `{` cannot open a tag name, so the parser treats the whole thing as text and
29
+ * passes it through untouched. Masked, it becomes a *valid* tag name, and since the matching
30
+ * `</{{properties.header}}>` masks to a different token the parser sees an unclosed element and
31
+ * rebalances around it. A measured registry's simple-title component loses a `<section>` and two
32
+ * `<div>` pairs that way — the very damage this function exists to prevent, reintroduced by
33
+ * over-masking.
34
+ */
35
+ const maskHandlebars = (source) => {
36
+ let nonce = 'hbmask';
37
+ while (source.includes(nonce)) {
38
+ nonce += 'x';
39
+ }
40
+ const spans = [];
41
+ const masked = source.replace(HANDLEBARS, (construct, offset) => {
42
+ const inTagNamePosition = source[offset - 1] === '<' || (source[offset - 1] === '/' && source[offset - 2] === '<');
43
+ if (inTagNamePosition) {
44
+ return construct;
45
+ }
46
+ spans.push(construct);
47
+ return `${nonce}${spans.length - 1}${nonce}`;
48
+ });
49
+ return { masked, nonce, spans };
50
+ };
51
+ /**
52
+ * Put the Handlebars back.
53
+ *
54
+ * Returns `null` when the parser did not hand every token back intact: a leftover nonce means a
55
+ * token was split or rewritten, and a half-restored template is worse than an unparsed one. The
56
+ * caller falls back to the slice rather than emitting it.
57
+ */
58
+ const unmaskHandlebars = (markup, { nonce, spans }) => {
59
+ const restored = markup.replace(new RegExp(`${nonce}(\\d+)${nonce}`, 'g'), (whole, index) => spans[Number(index)] ?? whole);
60
+ return restored.includes(nonce) ? null : restored;
61
+ };
62
+ /** The body's contents by string search: what the parse path falls back to. Faithful, unnormalised. */
63
+ const sliceBody = (source) => {
64
+ const open = BODY_OPEN.exec(source);
65
+ if (!open) {
66
+ return null;
67
+ }
68
+ const start = open.index + open[0].length;
69
+ // Last, not first: a document whose body contains a nested `</body>` still ends at its own. A body
70
+ // left unclosed runs to the end of the input.
71
+ const close = source.toLowerCase().lastIndexOf(BODY_CLOSE);
72
+ return source.slice(start, close === -1 || close < start ? source.length : close).trim();
73
+ };
74
+ /**
75
+ * Take everything between `<body>` and `</body>`.
76
+ *
77
+ * ## Why this parses, and why it masks first
78
+ *
79
+ * The parse earns its place: `parse(…).querySelector('body').innerHTML` also *normalises* what it
80
+ * re-serializes — `<path … />` becomes `<path …></path>`, an implicitly-closed `<p>` gains its
81
+ * `</p>` — and that normalised form is what the reviewed HubSpot modules in production contain. A
82
+ * plain string slice is faithful to the template but diverges from production on every component
83
+ * with an inline SVG: 37 of one registry's 69 published modules, 76 tag tokens.
84
+ *
85
+ * What the parse must *not* do is read the template's Handlebars as markup. `preview` is very often
86
+ * not HTML: for a Handlebars component it is `template.hbs`, and this function's output becomes
87
+ * `componentData.code` — the source every downstream consumer transpiles, including the HubSpot
88
+ * module emitter. node-html-parser has no concept of `{{!-- … --}}`, so an HTML tag *named* inside a
89
+ * Handlebars comment is a real tag to it:
90
+ *
91
+ * {{!-- A navigation item with no url renders dead rather than disappearing. An <a> without
92
+ * href is not focusable or clickable. --}}
93
+ * <a {{#if this.url}}href="{{this.url}}" {{/if}}class="accordion-button">…</a>
94
+ *
95
+ * The parser opens a phantom `<a>` from the prose, spends the template's real `</a>` closing it, and
96
+ * rebalances the rest of the document around the genuine `<a>` that is now unclosed — turning
97
+ * ancestor `<div>` openers into closers and discarding the ones it can no longer match. The result
98
+ * stays balanced, so nothing downstream can tell. One registry's nav component came back with 32
99
+ * `<div>` pairs instead of 35 and 4 `<h6>` pairs instead of 5; the mobile drawer's flex wrapper
100
+ * vanished outright and its contents rendered as siblings of the drawer on the live site.
101
+ *
102
+ * Masking the Handlebars first takes away the parser's ability to see inside one, and keeps the
103
+ * normalisation. Comments are the expensive case but not the only one — a `<` inside a helper
104
+ * argument, `{{concat "<span>" x}}`, is eaten exactly the same way — so every construct is masked
105
+ * except the one in {@link maskHandlebars} that must not be.
106
+ *
107
+ * Non-HTML input (a React component's source, say) has no `<body>` and comes back untouched.
108
+ *
109
+ * @param preview - The HTML (or template) string
110
+ * @returns The body's contents, trimmed; the whole input when there is no `<body>`
7
111
  */
8
112
  export const trimPreview = (preview) => {
9
113
  if (typeof preview !== 'string') {
@@ -13,14 +117,25 @@ export const trimPreview = (preview) => {
13
117
  if (!normalizedPreview) {
14
118
  return '';
15
119
  }
120
+ const masked = maskHandlebars(normalizedPreview);
16
121
  try {
17
- const bodyEl = parse(normalizedPreview).querySelector('body');
18
- return bodyEl ? bodyEl.innerHTML.trim() : normalizedPreview;
122
+ // `comment: true` keeps `<!-- … -->`. An authoring note is content the template meant to carry;
123
+ // dropping it is loss, not normalisation, and the default drops it.
124
+ const body = parse(masked.masked, { comment: true }).querySelector('body');
125
+ if (!body) {
126
+ return normalizedPreview;
127
+ }
128
+ const restored = unmaskHandlebars(body.innerHTML.trim(), masked);
129
+ if (restored !== null) {
130
+ return restored;
131
+ }
19
132
  }
20
133
  catch {
21
- // Keep build resilient if parser fails on malformed HTML.
22
- return normalizedPreview;
134
+ // Keep the build resilient if the parser fails on malformed HTML.
23
135
  }
136
+ // The parse either threw or did not hand every token back. Fall back to the slice: no
137
+ // normalisation, but nothing invented and nothing lost.
138
+ return sliceBody(normalizedPreview) ?? normalizedPreview;
24
139
  };
25
140
  /**
26
141
  * Formats HTML content using Prettier