wordhug 0.0.0-stage → 0.2.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cody Ta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,171 @@
1
- # Temporary Holding Version
1
+ # wordhug
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Keep phrases together on one line. Put square brackets around a phrase and it is wrapped in an inline-block element, so it moves to the next line as one unit instead of breaking in the middle. Works with React, Vue, Solid, Svelte and Astro, or as a plain HTML string.
4
+
5
+ ```tsx
6
+ <WordHug>{"Hello [big world] today"}</WordHug>
7
+ // → Hello <span style="display: inline-block">big world</span> today
8
+ ```
9
+
10
+ Useful for headings and short copy where a line break would split a name, a price, or a phrase that should read as one unit.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install wordhug
16
+ ```
17
+
18
+ Each framework is an optional peer dependency; install only the one you use.
19
+
20
+ | Framework | Import from | Peer dependency | Text goes in |
21
+ | --------- | ---------------- | ----------------------------- | -------------- |
22
+ | React | `wordhug/react` | `react` 18+ | `children` |
23
+ | Vue | `wordhug/vue` | `vue` 3.3+ | default slot |
24
+ | Solid | `wordhug/solid` | `solid-js` and `@solidjs/web` 2+ | `children` |
25
+ | Svelte | `wordhug/svelte` | `svelte` 5+ | `text` prop |
26
+ | Astro | `wordhug/astro` | – | `text` prop |
27
+ | None | `wordhug` | – | `renderToHtml` |
28
+
29
+ ### Upgrading from 0.1
30
+
31
+ The React component moved to its own subpath. Change the import:
32
+
33
+ ```diff
34
+ - import { WordHug } from "wordhug";
35
+ + import { WordHug } from "wordhug/react";
36
+ ```
37
+
38
+ `parseBrackets` is still imported from `wordhug`.
39
+
40
+ ## Usage
41
+
42
+ ### React
43
+
44
+ ```tsx
45
+ import { WordHug } from "wordhug/react";
46
+
47
+ export function Hero() {
48
+ return (
49
+ <h1>
50
+ <WordHug>{"Fresh bread [delivered daily] to [your door]"}</WordHug>
51
+ </h1>
52
+ );
53
+ }
54
+ ```
55
+
56
+ `WordHug` has no hooks and no `"use client"` directive, so it renders in React Server Components as well as on the client.
57
+
58
+ | Prop | Type | Default | Description |
59
+ | ----------- | ----------------------------- | ----------------------------- | ------------------------------------ |
60
+ | `children` | `string \| null \| undefined` | – | Text to render. Empty input renders nothing. |
61
+ | `as` | `React.ElementType` | `"span"` | Element that wraps each group. |
62
+ | `className` | `string` | – | Class name on each group wrapper. |
63
+ | `style` | `CSSProperties \| null` | `{ display: "inline-block" }` | Inline style on each group wrapper. Pass `null` to style groups only through `className`. |
64
+
65
+ ### Vue
66
+
67
+ ```vue
68
+ <script setup>
69
+ import { WordHug } from "wordhug/vue";
70
+ </script>
71
+
72
+ <template>
73
+ <h1><WordHug class="nowrap">Fresh bread [delivered daily] to [your door]</WordHug></h1>
74
+ </template>
75
+ ```
76
+
77
+ The text comes from the default slot (plain text and `{{ }}` interpolations; elements in the slot are ignored). `class` and `style` go on each group wrapper, not on a root element. `style` defaults to `display: inline-block`; `:style="null"` removes it. `as` takes a tag name or a component and defaults to `"span"`.
78
+
79
+ ### Solid
80
+
81
+ ```tsx
82
+ import { WordHug } from "wordhug/solid";
83
+
84
+ <h1>
85
+ <WordHug class="nowrap">{"Fresh bread [delivered daily] to [your door]"}</WordHug>
86
+ </h1>;
87
+ ```
88
+
89
+ Props: `children` (string), `as` (tag or component, default `"span"`), `class`, and `style` (object or string, default `{ display: "inline-block" }`, `null` removes it). Re-renders when `children` changes. Requires Solid 2.
90
+
91
+ ### Svelte
92
+
93
+ ```svelte
94
+ <script>
95
+ import { WordHug } from "wordhug/svelte";
96
+ </script>
97
+
98
+ <h1><WordHug text="Fresh bread [delivered daily] to [your door]" /></h1>
99
+ ```
100
+
101
+ Props: `text` (string), `as` (tag name, default `"span"`), `class`, and `style` (string, default `"display: inline-block"`, `null` removes it). Requires Svelte 5; the component ships as source and is compiled by your build.
102
+
103
+ ### Astro
104
+
105
+ ```astro
106
+ ---
107
+ import WordHug from "wordhug/astro";
108
+ ---
109
+
110
+ <h1><WordHug text="Fresh bread [delivered daily] to [your door]" /></h1>
111
+ ```
112
+
113
+ Same props as Svelte: `text`, `as`, `class`, `style`. Renders static HTML with no client JavaScript.
114
+
115
+ ### Plain HTML: `renderToHtml`
116
+
117
+ ```ts
118
+ import { renderToHtml } from "wordhug";
119
+
120
+ renderToHtml("Fish & [chips]", { class: "nowrap" });
121
+ // → 'Fish &amp; <span class="nowrap" style="display: inline-block">chips</span>'
122
+ ```
123
+
124
+ Options are `as` (tag name), `class` and `style`, as for Svelte. Text and attributes are HTML-escaped, so the result is safe to insert with `innerHTML`.
125
+
126
+ ### Styling
127
+
128
+ Groups work without any stylesheet: each wrapper gets `display: inline-block` inline. To use a class instead (for example Tailwind's `inline-block` or `whitespace-nowrap`), pass a class and a `null` style.
129
+
130
+ An inline-block group moves to the next line as a whole. If a group is wider than its container, it still wraps inside its own box rather than overflowing. Use `white-space: nowrap` if you would rather it never break and overflow instead. Note that a parent's `text-decoration` (such as a link underline) does not carry into inline-block groups.
131
+
132
+ ### Parsing rules
133
+
134
+ - `[...]` marks a group. Groups can nest: `[a [b c] d]`.
135
+ - A bracket without a partner is kept as literal text: `a ] b` and `a [ b` render unchanged.
136
+ - Runs of whitespace collapse to a single space, and whitespace at the start and end of the text and of each group is removed.
137
+ - Text touching a bracket stays attached: `(see [page 3])` renders `(see <span>page 3</span>)`.
138
+ - Empty groups (`[]`) are dropped.
139
+ - To show a literal bracket, put a backslash before it: `\[sic\]` renders `[sic]` and never forms a group. A backslash before any other character is kept as is.
140
+
141
+ In JSX text, Vue and Svelte templates and Astro attributes a backslash is literal, so `<WordHug>Quote \[sic\]</WordHug>` and `text="Quote \[sic\]"` work as written. Inside a JavaScript string the backslash itself needs escaping: `{"Quote \\[sic\\]"}`.
142
+
143
+ ### `parseBrackets`
144
+
145
+ The parser is exported from the package root, which imports no framework, so you can render the tree any way you like.
146
+
147
+ ```ts
148
+ import { parseBrackets } from "wordhug";
149
+
150
+ parseBrackets("Hello [big world] today");
151
+ // [
152
+ // { type: "text", value: "Hello " },
153
+ // { type: "group", children: [{ type: "text", value: "big world" }] },
154
+ // { type: "text", value: " today" },
155
+ // ]
156
+ ```
157
+
158
+ Types `Node`, `TextNode`, `GroupNode` and `RenderToHtmlOptions` are exported from `wordhug`; `WordHugProps` is exported from `wordhug/react`, `wordhug/solid` and `wordhug/svelte`.
159
+
160
+ ## Development
161
+
162
+ ```bash
163
+ npm install
164
+ npm test
165
+ npm run typecheck
166
+ npm run build
167
+ ```
168
+
169
+ ## License
170
+
171
+ MIT
@@ -0,0 +1,20 @@
1
+ ---
2
+ // Renders `text` so that every [bracketed phrase] is wrapped in an element
3
+ // (an inline-block span by default) that moves to the next line as one unit.
4
+ import { renderToHtml } from "wordhug";
5
+
6
+ export interface Props {
7
+ /** Text to render. `[...]` groups are kept together on one line. */
8
+ text: string | null | undefined;
9
+ /** Tag name used to wrap each group. Defaults to `"span"`. */
10
+ as?: string;
11
+ /** Class for each group wrapper. */
12
+ class?: string;
13
+ /** Style attribute for each group wrapper. Defaults to `"display: inline-block"`; `null` removes it. */
14
+ style?: string | null;
15
+ }
16
+
17
+ const { text, as, class: className, style } = Astro.props;
18
+ ---
19
+
20
+ <Fragment set:html={renderToHtml(text, { as, class: className, style })} />
package/dist/index.cjs ADDED
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ parseBrackets: () => parseBrackets,
24
+ renderToHtml: () => renderToHtml
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/parse.ts
29
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
30
+ function parseBrackets(input) {
31
+ const pairs = matchBrackets(input);
32
+ const stack = [{ nodes: [], end: -1 }];
33
+ let start = 0;
34
+ for (let i = 0; i < input.length; i++) {
35
+ const top = stack[stack.length - 1];
36
+ if (i === top.end) {
37
+ pushText(top.nodes, input.slice(start, i));
38
+ stack.pop();
39
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
40
+ start = i + 1;
41
+ continue;
42
+ }
43
+ const close = pairs.get(i);
44
+ if (close !== void 0) {
45
+ pushText(top.nodes, input.slice(start, i));
46
+ stack.push({ nodes: [], end: close });
47
+ start = i + 1;
48
+ }
49
+ }
50
+ const root = stack[0].nodes;
51
+ pushText(root, input.slice(start));
52
+ return normalize(root);
53
+ }
54
+ var ESCAPED_BRACKET = /\\([[\]])/g;
55
+ function pushText(nodes, value) {
56
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
57
+ }
58
+ function matchBrackets(input) {
59
+ const pairs = /* @__PURE__ */ new Map();
60
+ const open = [];
61
+ for (let i = 0; i < input.length; i++) {
62
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
63
+ else if (input[i] === "[") open.push(i);
64
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
65
+ }
66
+ return pairs;
67
+ }
68
+ function normalize(nodes) {
69
+ const out = [];
70
+ for (const node of nodes) {
71
+ if (node.type === "group") {
72
+ if (node.children.length > 0) out.push(node);
73
+ continue;
74
+ }
75
+ const last2 = out[out.length - 1];
76
+ if (last2?.type === "text") last2.value += node.value;
77
+ else out.push({ type: "text", value: node.value });
78
+ }
79
+ for (const node of out) {
80
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
81
+ }
82
+ const first = out[0];
83
+ if (first?.type === "text") first.value = first.value.trimStart();
84
+ const last = out[out.length - 1];
85
+ if (last?.type === "text") last.value = last.value.trimEnd();
86
+ return out.filter((node) => node.type === "group" || node.value !== "");
87
+ }
88
+
89
+ // src/html.ts
90
+ var TAG_NAME = /^[a-zA-Z][a-zA-Z0-9-]*$/;
91
+ function renderToHtml(text, { as = "span", class: className, style = "display: inline-block" } = {}) {
92
+ if (!text) return "";
93
+ if (!TAG_NAME.test(as)) throw new TypeError(`wordhug: invalid tag name ${JSON.stringify(as)}`);
94
+ let attrs = "";
95
+ if (className) attrs += ` class="${escapeHtml(className)}"`;
96
+ if (style) attrs += ` style="${escapeHtml(style)}"`;
97
+ const open = `<${as}${attrs}>`;
98
+ const close = `</${as}>`;
99
+ const render = (nodes) => nodes.map(
100
+ (node) => node.type === "text" ? escapeHtml(node.value) : open + render(node.children) + close
101
+ ).join("");
102
+ return render(parseBrackets(text));
103
+ }
104
+ var HTML_ESCAPES = {
105
+ "&": "&amp;",
106
+ "<": "&lt;",
107
+ ">": "&gt;",
108
+ '"': "&quot;",
109
+ "'": "&#39;"
110
+ };
111
+ function escapeHtml(value) {
112
+ return value.replace(/[&<>"']/g, (ch) => HTML_ESCAPES[ch]);
113
+ }
114
+ // Annotate the CommonJS export names for ESM import in node:
115
+ 0 && (module.exports = {
116
+ parseBrackets,
117
+ renderToHtml
118
+ });
@@ -0,0 +1,44 @@
1
+ interface TextNode {
2
+ type: "text";
3
+ value: string;
4
+ }
5
+ interface GroupNode {
6
+ type: "group";
7
+ children: Node[];
8
+ }
9
+ type Node = TextNode | GroupNode;
10
+ /**
11
+ * Parse text with square-bracket groups into a tree.
12
+ *
13
+ * - `[...]` becomes a group; groups can nest.
14
+ * - Brackets without a partner (`a ] b`, `a [ b`) are kept as literal text.
15
+ * - Runs of whitespace collapse to a single space, and whitespace at the start
16
+ * and end of the input and of each group is dropped.
17
+ * - Text directly touching a bracket stays attached: `(see [p. 3])` keeps the
18
+ * `(see ` and `)` around the group without extra spaces.
19
+ * - Empty groups (`[]`, `[ ]`) are removed.
20
+ * - `\[` and `\]` are literal brackets: they never open or close a group, and
21
+ * the backslash is removed. A backslash before anything else is kept as is.
22
+ */
23
+ declare function parseBrackets(input: string): Node[];
24
+
25
+ interface RenderToHtmlOptions {
26
+ /** Tag name used to wrap each group. Defaults to `"span"`. */
27
+ as?: string;
28
+ /** Class attribute for each group wrapper. */
29
+ class?: string;
30
+ /**
31
+ * Style attribute for each group wrapper. Defaults to `"display: inline-block"`.
32
+ * Pass `null` to leave styling entirely to `class`.
33
+ */
34
+ style?: string | null;
35
+ }
36
+ /**
37
+ * Render text to an HTML string in which every `[bracketed phrase]` is wrapped
38
+ * in an element (an inline-block span by default). Text and attribute values
39
+ * are escaped, so the result is safe to insert as HTML.
40
+ */
41
+ declare function renderToHtml(text: string | null | undefined, { as, class: className, style }?: RenderToHtmlOptions): string;
42
+
43
+ export { parseBrackets, renderToHtml };
44
+ export type { GroupNode, Node, RenderToHtmlOptions, TextNode };
@@ -0,0 +1,44 @@
1
+ interface TextNode {
2
+ type: "text";
3
+ value: string;
4
+ }
5
+ interface GroupNode {
6
+ type: "group";
7
+ children: Node[];
8
+ }
9
+ type Node = TextNode | GroupNode;
10
+ /**
11
+ * Parse text with square-bracket groups into a tree.
12
+ *
13
+ * - `[...]` becomes a group; groups can nest.
14
+ * - Brackets without a partner (`a ] b`, `a [ b`) are kept as literal text.
15
+ * - Runs of whitespace collapse to a single space, and whitespace at the start
16
+ * and end of the input and of each group is dropped.
17
+ * - Text directly touching a bracket stays attached: `(see [p. 3])` keeps the
18
+ * `(see ` and `)` around the group without extra spaces.
19
+ * - Empty groups (`[]`, `[ ]`) are removed.
20
+ * - `\[` and `\]` are literal brackets: they never open or close a group, and
21
+ * the backslash is removed. A backslash before anything else is kept as is.
22
+ */
23
+ declare function parseBrackets(input: string): Node[];
24
+
25
+ interface RenderToHtmlOptions {
26
+ /** Tag name used to wrap each group. Defaults to `"span"`. */
27
+ as?: string;
28
+ /** Class attribute for each group wrapper. */
29
+ class?: string;
30
+ /**
31
+ * Style attribute for each group wrapper. Defaults to `"display: inline-block"`.
32
+ * Pass `null` to leave styling entirely to `class`.
33
+ */
34
+ style?: string | null;
35
+ }
36
+ /**
37
+ * Render text to an HTML string in which every `[bracketed phrase]` is wrapped
38
+ * in an element (an inline-block span by default). Text and attribute values
39
+ * are escaped, so the result is safe to insert as HTML.
40
+ */
41
+ declare function renderToHtml(text: string | null | undefined, { as, class: className, style }?: RenderToHtmlOptions): string;
42
+
43
+ export { parseBrackets, renderToHtml };
44
+ export type { GroupNode, Node, RenderToHtmlOptions, TextNode };
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ // src/parse.ts
2
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
3
+ function parseBrackets(input) {
4
+ const pairs = matchBrackets(input);
5
+ const stack = [{ nodes: [], end: -1 }];
6
+ let start = 0;
7
+ for (let i = 0; i < input.length; i++) {
8
+ const top = stack[stack.length - 1];
9
+ if (i === top.end) {
10
+ pushText(top.nodes, input.slice(start, i));
11
+ stack.pop();
12
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
13
+ start = i + 1;
14
+ continue;
15
+ }
16
+ const close = pairs.get(i);
17
+ if (close !== void 0) {
18
+ pushText(top.nodes, input.slice(start, i));
19
+ stack.push({ nodes: [], end: close });
20
+ start = i + 1;
21
+ }
22
+ }
23
+ const root = stack[0].nodes;
24
+ pushText(root, input.slice(start));
25
+ return normalize(root);
26
+ }
27
+ var ESCAPED_BRACKET = /\\([[\]])/g;
28
+ function pushText(nodes, value) {
29
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
30
+ }
31
+ function matchBrackets(input) {
32
+ const pairs = /* @__PURE__ */ new Map();
33
+ const open = [];
34
+ for (let i = 0; i < input.length; i++) {
35
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
36
+ else if (input[i] === "[") open.push(i);
37
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
38
+ }
39
+ return pairs;
40
+ }
41
+ function normalize(nodes) {
42
+ const out = [];
43
+ for (const node of nodes) {
44
+ if (node.type === "group") {
45
+ if (node.children.length > 0) out.push(node);
46
+ continue;
47
+ }
48
+ const last2 = out[out.length - 1];
49
+ if (last2?.type === "text") last2.value += node.value;
50
+ else out.push({ type: "text", value: node.value });
51
+ }
52
+ for (const node of out) {
53
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
54
+ }
55
+ const first = out[0];
56
+ if (first?.type === "text") first.value = first.value.trimStart();
57
+ const last = out[out.length - 1];
58
+ if (last?.type === "text") last.value = last.value.trimEnd();
59
+ return out.filter((node) => node.type === "group" || node.value !== "");
60
+ }
61
+
62
+ // src/html.ts
63
+ var TAG_NAME = /^[a-zA-Z][a-zA-Z0-9-]*$/;
64
+ function renderToHtml(text, { as = "span", class: className, style = "display: inline-block" } = {}) {
65
+ if (!text) return "";
66
+ if (!TAG_NAME.test(as)) throw new TypeError(`wordhug: invalid tag name ${JSON.stringify(as)}`);
67
+ let attrs = "";
68
+ if (className) attrs += ` class="${escapeHtml(className)}"`;
69
+ if (style) attrs += ` style="${escapeHtml(style)}"`;
70
+ const open = `<${as}${attrs}>`;
71
+ const close = `</${as}>`;
72
+ const render = (nodes) => nodes.map(
73
+ (node) => node.type === "text" ? escapeHtml(node.value) : open + render(node.children) + close
74
+ ).join("");
75
+ return render(parseBrackets(text));
76
+ }
77
+ var HTML_ESCAPES = {
78
+ "&": "&amp;",
79
+ "<": "&lt;",
80
+ ">": "&gt;",
81
+ '"': "&quot;",
82
+ "'": "&#39;"
83
+ };
84
+ function escapeHtml(value) {
85
+ return value.replace(/[&<>"']/g, (ch) => HTML_ESCAPES[ch]);
86
+ }
87
+ export {
88
+ parseBrackets,
89
+ renderToHtml
90
+ };
package/dist/react.cjs ADDED
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/react.tsx
21
+ var react_exports = {};
22
+ __export(react_exports, {
23
+ WordHug: () => WordHug
24
+ });
25
+ module.exports = __toCommonJS(react_exports);
26
+
27
+ // src/parse.ts
28
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
29
+ function parseBrackets(input) {
30
+ const pairs = matchBrackets(input);
31
+ const stack = [{ nodes: [], end: -1 }];
32
+ let start = 0;
33
+ for (let i = 0; i < input.length; i++) {
34
+ const top = stack[stack.length - 1];
35
+ if (i === top.end) {
36
+ pushText(top.nodes, input.slice(start, i));
37
+ stack.pop();
38
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
39
+ start = i + 1;
40
+ continue;
41
+ }
42
+ const close = pairs.get(i);
43
+ if (close !== void 0) {
44
+ pushText(top.nodes, input.slice(start, i));
45
+ stack.push({ nodes: [], end: close });
46
+ start = i + 1;
47
+ }
48
+ }
49
+ const root = stack[0].nodes;
50
+ pushText(root, input.slice(start));
51
+ return normalize(root);
52
+ }
53
+ var ESCAPED_BRACKET = /\\([[\]])/g;
54
+ function pushText(nodes, value) {
55
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
56
+ }
57
+ function matchBrackets(input) {
58
+ const pairs = /* @__PURE__ */ new Map();
59
+ const open = [];
60
+ for (let i = 0; i < input.length; i++) {
61
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
62
+ else if (input[i] === "[") open.push(i);
63
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
64
+ }
65
+ return pairs;
66
+ }
67
+ function normalize(nodes) {
68
+ const out = [];
69
+ for (const node of nodes) {
70
+ if (node.type === "group") {
71
+ if (node.children.length > 0) out.push(node);
72
+ continue;
73
+ }
74
+ const last2 = out[out.length - 1];
75
+ if (last2?.type === "text") last2.value += node.value;
76
+ else out.push({ type: "text", value: node.value });
77
+ }
78
+ for (const node of out) {
79
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
80
+ }
81
+ const first = out[0];
82
+ if (first?.type === "text") first.value = first.value.trimStart();
83
+ const last = out[out.length - 1];
84
+ if (last?.type === "text") last.value = last.value.trimEnd();
85
+ return out.filter((node) => node.type === "group" || node.value !== "");
86
+ }
87
+
88
+ // src/react.tsx
89
+ var import_jsx_runtime = require("react/jsx-runtime");
90
+ var DEFAULT_STYLE = { display: "inline-block" };
91
+ function WordHug({
92
+ children,
93
+ as: Tag = "span",
94
+ className,
95
+ style = DEFAULT_STYLE
96
+ }) {
97
+ if (!children) return null;
98
+ const renderNodes = (nodes) => nodes.map(
99
+ (node, i) => node.type === "text" ? node.value : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Tag, { className, style: style ?? void 0, children: renderNodes(node.children) }, i)
100
+ );
101
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: renderNodes(parseBrackets(children)) });
102
+ }
103
+ // Annotate the CommonJS export names for ESM import in node:
104
+ 0 && (module.exports = {
105
+ WordHug
106
+ });
@@ -0,0 +1,25 @@
1
+ import { ElementType, CSSProperties, ReactNode } from 'react';
2
+
3
+ interface WordHugProps {
4
+ /** Text to render. `[...]` groups are kept together on one line. */
5
+ children: string | null | undefined;
6
+ /** Element used to wrap each group. Defaults to `"span"`. */
7
+ as?: ElementType;
8
+ /** Class name for each group wrapper. */
9
+ className?: string;
10
+ /**
11
+ * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`,
12
+ * so groups stay together without any stylesheet. Pass `null` to leave styling
13
+ * entirely to `className`.
14
+ */
15
+ style?: CSSProperties | null;
16
+ }
17
+ /**
18
+ * Render text so that every `[bracketed phrase]` is wrapped in an element
19
+ * (an inline-block span by default) that moves to the next line as one unit.
20
+ * Has no hooks or client-only code, so it works in React Server Components.
21
+ */
22
+ declare function WordHug({ children, as: Tag, className, style, }: WordHugProps): ReactNode;
23
+
24
+ export { WordHug };
25
+ export type { WordHugProps };
@@ -0,0 +1,25 @@
1
+ import { ElementType, CSSProperties, ReactNode } from 'react';
2
+
3
+ interface WordHugProps {
4
+ /** Text to render. `[...]` groups are kept together on one line. */
5
+ children: string | null | undefined;
6
+ /** Element used to wrap each group. Defaults to `"span"`. */
7
+ as?: ElementType;
8
+ /** Class name for each group wrapper. */
9
+ className?: string;
10
+ /**
11
+ * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`,
12
+ * so groups stay together without any stylesheet. Pass `null` to leave styling
13
+ * entirely to `className`.
14
+ */
15
+ style?: CSSProperties | null;
16
+ }
17
+ /**
18
+ * Render text so that every `[bracketed phrase]` is wrapped in an element
19
+ * (an inline-block span by default) that moves to the next line as one unit.
20
+ * Has no hooks or client-only code, so it works in React Server Components.
21
+ */
22
+ declare function WordHug({ children, as: Tag, className, style, }: WordHugProps): ReactNode;
23
+
24
+ export { WordHug };
25
+ export type { WordHugProps };