wordhug 0.1.0 → 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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # wordhug
2
2
 
3
- Keep phrases together on one line in React. 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.
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
4
 
5
5
  ```tsx
6
6
  <WordHug>{"Hello [big world] today"}</WordHug>
@@ -15,12 +15,34 @@ Useful for headings and short copy where a line break would split a name, a pric
15
15
  npm install wordhug
16
16
  ```
17
17
 
18
- React 18 or later is a peer dependency.
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`.
19
39
 
20
40
  ## Usage
21
41
 
42
+ ### React
43
+
22
44
  ```tsx
23
- import { WordHug } from "wordhug";
45
+ import { WordHug } from "wordhug/react";
24
46
 
25
47
  export function Hero() {
26
48
  return (
@@ -33,8 +55,6 @@ export function Hero() {
33
55
 
34
56
  `WordHug` has no hooks and no `"use client"` directive, so it renders in React Server Components as well as on the client.
35
57
 
36
- ### Props
37
-
38
58
  | Prop | Type | Default | Description |
39
59
  | ----------- | ----------------------------- | ----------------------------- | ------------------------------------ |
40
60
  | `children` | `string \| null \| undefined` | – | Text to render. Empty input renders nothing. |
@@ -42,9 +62,72 @@ export function Hero() {
42
62
  | `className` | `string` | – | Class name on each group wrapper. |
43
63
  | `style` | `CSSProperties \| null` | `{ display: "inline-block" }` | Inline style on each group wrapper. Pass `null` to style groups only through `className`. |
44
64
 
45
- 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 `className` and `style={null}`.
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.
46
129
 
47
- 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 `style={{ whiteSpace: "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.
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.
48
131
 
49
132
  ### Parsing rules
50
133
 
@@ -55,11 +138,11 @@ An inline-block group moves to the next line as a whole. If a group is wider tha
55
138
  - Empty groups (`[]`) are dropped.
56
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.
57
140
 
58
- In JSX text a backslash is literal, so `<WordHug>Quote \[sic\]</WordHug>` works as written. Inside a JavaScript string the backslash itself needs escaping: `{"Quote \\[sic\\]"}`.
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\\]"}`.
59
142
 
60
143
  ### `parseBrackets`
61
144
 
62
- The parser is exported on its own and does not import React, so you can use the tree with any renderer.
145
+ The parser is exported from the package root, which imports no framework, so you can render the tree any way you like.
63
146
 
64
147
  ```ts
65
148
  import { parseBrackets } from "wordhug";
@@ -72,7 +155,7 @@ parseBrackets("Hello [big world] today");
72
155
  // ]
73
156
  ```
74
157
 
75
- Types `Node`, `TextNode`, `GroupNode` and `WordHugProps` are exported too.
158
+ Types `Node`, `TextNode`, `GroupNode` and `RenderToHtmlOptions` are exported from `wordhug`; `WordHugProps` is exported from `wordhug/react`, `wordhug/solid` and `wordhug/svelte`.
76
159
 
77
160
  ## Development
78
161
 
@@ -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 CHANGED
@@ -20,8 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- WordHug: () => WordHug,
24
- parseBrackets: () => parseBrackets
23
+ parseBrackets: () => parseBrackets,
24
+ renderToHtml: () => renderToHtml
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
27
 
@@ -86,23 +86,33 @@ function normalize(nodes) {
86
86
  return out.filter((node) => node.type === "group" || node.value !== "");
87
87
  }
88
88
 
89
- // src/word-hug.tsx
90
- var import_jsx_runtime = require("react/jsx-runtime");
91
- var DEFAULT_STYLE = { display: "inline-block" };
92
- function WordHug({
93
- children,
94
- as: Tag = "span",
95
- className,
96
- style = DEFAULT_STYLE
97
- }) {
98
- if (!children) return null;
99
- const renderNodes = (nodes) => nodes.map(
100
- (node, i) => node.type === "text" ? node.value : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Tag, { className, style: style ?? void 0, children: renderNodes(node.children) }, i)
101
- );
102
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: renderNodes(parseBrackets(children)) });
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]);
103
113
  }
104
114
  // Annotate the CommonJS export names for ESM import in node:
105
115
  0 && (module.exports = {
106
- WordHug,
107
- parseBrackets
116
+ parseBrackets,
117
+ renderToHtml
108
118
  });
package/dist/index.d.cts CHANGED
@@ -1,5 +1,3 @@
1
- import { ElementType, CSSProperties, ReactNode } from 'react';
2
-
3
1
  interface TextNode {
4
2
  type: "text";
5
3
  value: string;
@@ -24,26 +22,23 @@ type Node = TextNode | GroupNode;
24
22
  */
25
23
  declare function parseBrackets(input: string): Node[];
26
24
 
27
- interface WordHugProps {
28
- /** Text to render. `[...]` groups are kept together on one line. */
29
- children: string | null | undefined;
30
- /** Element used to wrap each group. Defaults to `"span"`. */
31
- as?: ElementType;
32
- /** Class name for each group wrapper. */
33
- className?: string;
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;
34
30
  /**
35
- * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`,
36
- * so groups stay together without any stylesheet. Pass `null` to leave styling
37
- * entirely to `className`.
31
+ * Style attribute for each group wrapper. Defaults to `"display: inline-block"`.
32
+ * Pass `null` to leave styling entirely to `class`.
38
33
  */
39
- style?: CSSProperties | null;
34
+ style?: string | null;
40
35
  }
41
36
  /**
42
- * Render text so that every `[bracketed phrase]` is wrapped in an element
43
- * (an inline-block span by default) that moves to the next line as one unit.
44
- * Has no hooks or client-only code, so it works in React Server Components.
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.
45
40
  */
46
- declare function WordHug({ children, as: Tag, className, style, }: WordHugProps): ReactNode;
41
+ declare function renderToHtml(text: string | null | undefined, { as, class: className, style }?: RenderToHtmlOptions): string;
47
42
 
48
- export { WordHug, parseBrackets };
49
- export type { GroupNode, Node, TextNode, WordHugProps };
43
+ export { parseBrackets, renderToHtml };
44
+ export type { GroupNode, Node, RenderToHtmlOptions, TextNode };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { ElementType, CSSProperties, ReactNode } from 'react';
2
-
3
1
  interface TextNode {
4
2
  type: "text";
5
3
  value: string;
@@ -24,26 +22,23 @@ type Node = TextNode | GroupNode;
24
22
  */
25
23
  declare function parseBrackets(input: string): Node[];
26
24
 
27
- interface WordHugProps {
28
- /** Text to render. `[...]` groups are kept together on one line. */
29
- children: string | null | undefined;
30
- /** Element used to wrap each group. Defaults to `"span"`. */
31
- as?: ElementType;
32
- /** Class name for each group wrapper. */
33
- className?: string;
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;
34
30
  /**
35
- * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`,
36
- * so groups stay together without any stylesheet. Pass `null` to leave styling
37
- * entirely to `className`.
31
+ * Style attribute for each group wrapper. Defaults to `"display: inline-block"`.
32
+ * Pass `null` to leave styling entirely to `class`.
38
33
  */
39
- style?: CSSProperties | null;
34
+ style?: string | null;
40
35
  }
41
36
  /**
42
- * Render text so that every `[bracketed phrase]` is wrapped in an element
43
- * (an inline-block span by default) that moves to the next line as one unit.
44
- * Has no hooks or client-only code, so it works in React Server Components.
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.
45
40
  */
46
- declare function WordHug({ children, as: Tag, className, style, }: WordHugProps): ReactNode;
41
+ declare function renderToHtml(text: string | null | undefined, { as, class: className, style }?: RenderToHtmlOptions): string;
47
42
 
48
- export { WordHug, parseBrackets };
49
- export type { GroupNode, Node, TextNode, WordHugProps };
43
+ export { parseBrackets, renderToHtml };
44
+ export type { GroupNode, Node, RenderToHtmlOptions, TextNode };
package/dist/index.js CHANGED
@@ -59,22 +59,32 @@ function normalize(nodes) {
59
59
  return out.filter((node) => node.type === "group" || node.value !== "");
60
60
  }
61
61
 
62
- // src/word-hug.tsx
63
- import { Fragment, jsx } from "react/jsx-runtime";
64
- var DEFAULT_STYLE = { display: "inline-block" };
65
- function WordHug({
66
- children,
67
- as: Tag = "span",
68
- className,
69
- style = DEFAULT_STYLE
70
- }) {
71
- if (!children) return null;
72
- const renderNodes = (nodes) => nodes.map(
73
- (node, i) => node.type === "text" ? node.value : /* @__PURE__ */ jsx(Tag, { className, style: style ?? void 0, children: renderNodes(node.children) }, i)
74
- );
75
- return /* @__PURE__ */ jsx(Fragment, { children: renderNodes(parseBrackets(children)) });
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]);
76
86
  }
77
87
  export {
78
- WordHug,
79
- parseBrackets
88
+ parseBrackets,
89
+ renderToHtml
80
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 };
package/dist/react.js ADDED
@@ -0,0 +1,79 @@
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/react.tsx
63
+ import { Fragment, jsx } from "react/jsx-runtime";
64
+ var DEFAULT_STYLE = { display: "inline-block" };
65
+ function WordHug({
66
+ children,
67
+ as: Tag = "span",
68
+ className,
69
+ style = DEFAULT_STYLE
70
+ }) {
71
+ if (!children) return null;
72
+ const renderNodes = (nodes) => nodes.map(
73
+ (node, i) => node.type === "text" ? node.value : /* @__PURE__ */ jsx(Tag, { className, style: style ?? void 0, children: renderNodes(node.children) }, i)
74
+ );
75
+ return /* @__PURE__ */ jsx(Fragment, { children: renderNodes(parseBrackets(children)) });
76
+ }
77
+ export {
78
+ WordHug
79
+ };
package/dist/solid.cjs ADDED
@@ -0,0 +1,114 @@
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/solid.ts
21
+ var solid_exports = {};
22
+ __export(solid_exports, {
23
+ WordHug: () => WordHug
24
+ });
25
+ module.exports = __toCommonJS(solid_exports);
26
+ var import_solid_js = require("solid-js");
27
+ var import_web = require("@solidjs/web");
28
+
29
+ // src/parse.ts
30
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
31
+ function parseBrackets(input) {
32
+ const pairs = matchBrackets(input);
33
+ const stack = [{ nodes: [], end: -1 }];
34
+ let start = 0;
35
+ for (let i = 0; i < input.length; i++) {
36
+ const top = stack[stack.length - 1];
37
+ if (i === top.end) {
38
+ pushText(top.nodes, input.slice(start, i));
39
+ stack.pop();
40
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
41
+ start = i + 1;
42
+ continue;
43
+ }
44
+ const close = pairs.get(i);
45
+ if (close !== void 0) {
46
+ pushText(top.nodes, input.slice(start, i));
47
+ stack.push({ nodes: [], end: close });
48
+ start = i + 1;
49
+ }
50
+ }
51
+ const root = stack[0].nodes;
52
+ pushText(root, input.slice(start));
53
+ return normalize(root);
54
+ }
55
+ var ESCAPED_BRACKET = /\\([[\]])/g;
56
+ function pushText(nodes, value) {
57
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
58
+ }
59
+ function matchBrackets(input) {
60
+ const pairs = /* @__PURE__ */ new Map();
61
+ const open = [];
62
+ for (let i = 0; i < input.length; i++) {
63
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
64
+ else if (input[i] === "[") open.push(i);
65
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
66
+ }
67
+ return pairs;
68
+ }
69
+ function normalize(nodes) {
70
+ const out = [];
71
+ for (const node of nodes) {
72
+ if (node.type === "group") {
73
+ if (node.children.length > 0) out.push(node);
74
+ continue;
75
+ }
76
+ const last2 = out[out.length - 1];
77
+ if (last2?.type === "text") last2.value += node.value;
78
+ else out.push({ type: "text", value: node.value });
79
+ }
80
+ for (const node of out) {
81
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
82
+ }
83
+ const first = out[0];
84
+ if (first?.type === "text") first.value = first.value.trimStart();
85
+ const last = out[out.length - 1];
86
+ if (last?.type === "text") last.value = last.value.trimEnd();
87
+ return out.filter((node) => node.type === "group" || node.value !== "");
88
+ }
89
+
90
+ // src/solid.ts
91
+ var DEFAULT_STYLE = { display: "inline-block" };
92
+ function WordHug(props) {
93
+ const render = (nodes) => nodes.map(
94
+ (node) => node.type === "text" ? node.value : (0, import_solid_js.createComponent)(import_web.Dynamic, {
95
+ get component() {
96
+ return props.as ?? "span";
97
+ },
98
+ get class() {
99
+ return props.class;
100
+ },
101
+ get style() {
102
+ return props.style === void 0 ? DEFAULT_STYLE : props.style ?? void 0;
103
+ },
104
+ get children() {
105
+ return render(node.children);
106
+ }
107
+ })
108
+ );
109
+ return (() => props.children ? render(parseBrackets(props.children)) : null);
110
+ }
111
+ // Annotate the CommonJS export names for ESM import in node:
112
+ 0 && (module.exports = {
113
+ WordHug
114
+ });
@@ -0,0 +1,24 @@
1
+ import { ValidComponent, JSX } from '@solidjs/web';
2
+
3
+ interface WordHugProps {
4
+ /** Text to render. `[...]` groups are kept together on one line. */
5
+ children: string | null | undefined;
6
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
7
+ as?: ValidComponent;
8
+ /** Class for each group wrapper. */
9
+ class?: string;
10
+ /**
11
+ * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`.
12
+ * Pass `null` to leave styling entirely to `class`.
13
+ */
14
+ style?: JSX.CSSProperties | string | null;
15
+ }
16
+ /**
17
+ * Render text so that every `[bracketed phrase]` is wrapped in an element
18
+ * (an inline-block span by default) that moves to the next line as one unit.
19
+ * Re-renders when `children` changes.
20
+ */
21
+ declare function WordHug(props: WordHugProps): JSX.Element;
22
+
23
+ export { WordHug };
24
+ export type { WordHugProps };
@@ -0,0 +1,24 @@
1
+ import { ValidComponent, JSX } from '@solidjs/web';
2
+
3
+ interface WordHugProps {
4
+ /** Text to render. `[...]` groups are kept together on one line. */
5
+ children: string | null | undefined;
6
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
7
+ as?: ValidComponent;
8
+ /** Class for each group wrapper. */
9
+ class?: string;
10
+ /**
11
+ * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`.
12
+ * Pass `null` to leave styling entirely to `class`.
13
+ */
14
+ style?: JSX.CSSProperties | string | null;
15
+ }
16
+ /**
17
+ * Render text so that every `[bracketed phrase]` is wrapped in an element
18
+ * (an inline-block span by default) that moves to the next line as one unit.
19
+ * Re-renders when `children` changes.
20
+ */
21
+ declare function WordHug(props: WordHugProps): JSX.Element;
22
+
23
+ export { WordHug };
24
+ export type { WordHugProps };
package/dist/solid.js ADDED
@@ -0,0 +1,89 @@
1
+ // src/solid.ts
2
+ import { createComponent } from "solid-js";
3
+ import { Dynamic } from "@solidjs/web";
4
+
5
+ // src/parse.ts
6
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
7
+ function parseBrackets(input) {
8
+ const pairs = matchBrackets(input);
9
+ const stack = [{ nodes: [], end: -1 }];
10
+ let start = 0;
11
+ for (let i = 0; i < input.length; i++) {
12
+ const top = stack[stack.length - 1];
13
+ if (i === top.end) {
14
+ pushText(top.nodes, input.slice(start, i));
15
+ stack.pop();
16
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
17
+ start = i + 1;
18
+ continue;
19
+ }
20
+ const close = pairs.get(i);
21
+ if (close !== void 0) {
22
+ pushText(top.nodes, input.slice(start, i));
23
+ stack.push({ nodes: [], end: close });
24
+ start = i + 1;
25
+ }
26
+ }
27
+ const root = stack[0].nodes;
28
+ pushText(root, input.slice(start));
29
+ return normalize(root);
30
+ }
31
+ var ESCAPED_BRACKET = /\\([[\]])/g;
32
+ function pushText(nodes, value) {
33
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
34
+ }
35
+ function matchBrackets(input) {
36
+ const pairs = /* @__PURE__ */ new Map();
37
+ const open = [];
38
+ for (let i = 0; i < input.length; i++) {
39
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
40
+ else if (input[i] === "[") open.push(i);
41
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
42
+ }
43
+ return pairs;
44
+ }
45
+ function normalize(nodes) {
46
+ const out = [];
47
+ for (const node of nodes) {
48
+ if (node.type === "group") {
49
+ if (node.children.length > 0) out.push(node);
50
+ continue;
51
+ }
52
+ const last2 = out[out.length - 1];
53
+ if (last2?.type === "text") last2.value += node.value;
54
+ else out.push({ type: "text", value: node.value });
55
+ }
56
+ for (const node of out) {
57
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
58
+ }
59
+ const first = out[0];
60
+ if (first?.type === "text") first.value = first.value.trimStart();
61
+ const last = out[out.length - 1];
62
+ if (last?.type === "text") last.value = last.value.trimEnd();
63
+ return out.filter((node) => node.type === "group" || node.value !== "");
64
+ }
65
+
66
+ // src/solid.ts
67
+ var DEFAULT_STYLE = { display: "inline-block" };
68
+ function WordHug(props) {
69
+ const render = (nodes) => nodes.map(
70
+ (node) => node.type === "text" ? node.value : createComponent(Dynamic, {
71
+ get component() {
72
+ return props.as ?? "span";
73
+ },
74
+ get class() {
75
+ return props.class;
76
+ },
77
+ get style() {
78
+ return props.style === void 0 ? DEFAULT_STYLE : props.style ?? void 0;
79
+ },
80
+ get children() {
81
+ return render(node.children);
82
+ }
83
+ })
84
+ );
85
+ return (() => props.children ? render(parseBrackets(props.children)) : null);
86
+ }
87
+ export {
88
+ WordHug
89
+ };
@@ -0,0 +1,17 @@
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
+ -->
5
+ <script>
6
+ import { parseBrackets } from "wordhug";
7
+
8
+ /** @type {import("./index").WordHugProps} */
9
+ let { text, as = "span", class: className, style = "display: inline-block" } = $props();
10
+
11
+ const nodes = $derived(text ? parseBrackets(text) : []);
12
+ </script>
13
+
14
+ <!-- Kept on one line: whitespace between these tags would show up in the output. -->
15
+ {#snippet render(/** @type {import("wordhug").Node[]} */ list)}{#each list as node}{#if node.type === "text"}{node.value}{:else}<svelte:element this={as} class={className} style={style ?? undefined}>{@render render(node.children)}</svelte:element>{/if}{/each}{/snippet}
16
+
17
+ {@render render(nodes)}
@@ -0,0 +1,17 @@
1
+ import type { Component } from "svelte";
2
+
3
+ export interface WordHugProps {
4
+ /** Text to render. `[...]` groups are kept together on one line. */
5
+ text: string | null | undefined;
6
+ /** Tag name used to wrap each group. Defaults to `"span"`. */
7
+ as?: string;
8
+ /** Class for each group wrapper. */
9
+ class?: string;
10
+ /**
11
+ * Style attribute for each group wrapper. Defaults to `"display: inline-block"`.
12
+ * Pass `null` to leave styling entirely to `class`.
13
+ */
14
+ style?: string | null;
15
+ }
16
+
17
+ export declare const WordHug: Component<WordHugProps>;
@@ -0,0 +1 @@
1
+ export { default as WordHug } from "./WordHug.svelte";
package/dist/vue.cjs ADDED
@@ -0,0 +1,124 @@
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/vue.ts
21
+ var vue_exports = {};
22
+ __export(vue_exports, {
23
+ WordHug: () => WordHug
24
+ });
25
+ module.exports = __toCommonJS(vue_exports);
26
+ var import_vue = require("vue");
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/vue.ts
90
+ var DEFAULT_STYLE = { display: "inline-block" };
91
+ var WordHug = (0, import_vue.defineComponent)({
92
+ name: "WordHug",
93
+ inheritAttrs: false,
94
+ props: {
95
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
96
+ as: { type: [String, Object, Function], default: "span" }
97
+ },
98
+ setup(props, { slots, attrs }) {
99
+ return () => {
100
+ const text = slotText(slots.default?.());
101
+ if (!text) return null;
102
+ const style = "style" in attrs ? attrs.style : DEFAULT_STYLE;
103
+ const groupProps = {};
104
+ if (attrs.class != null) groupProps.class = attrs.class;
105
+ if (style != null) groupProps.style = style;
106
+ const render = (nodes) => nodes.map(
107
+ (node) => node.type === "text" ? node.value : (0, import_vue.h)(props.as, groupProps, { default: () => render(node.children) })
108
+ );
109
+ return render(parseBrackets(text));
110
+ };
111
+ }
112
+ });
113
+ function slotText(vnodes) {
114
+ let text = "";
115
+ for (const vnode of vnodes ?? []) {
116
+ if (vnode.type === import_vue.Text && typeof vnode.children === "string") text += vnode.children;
117
+ else if (Array.isArray(vnode.children)) text += slotText(vnode.children);
118
+ }
119
+ return text;
120
+ }
121
+ // Annotate the CommonJS export names for ESM import in node:
122
+ 0 && (module.exports = {
123
+ WordHug
124
+ });
package/dist/vue.d.cts ADDED
@@ -0,0 +1,26 @@
1
+ import * as vue from 'vue';
2
+ import { PropType, Component, VNodeChild } from 'vue';
3
+
4
+ /**
5
+ * Render the default slot's text so that every `[bracketed phrase]` is wrapped
6
+ * in an element (an inline-block span by default) that moves to the next line
7
+ * as one unit. `class` and `style` go on each group wrapper; `style` defaults
8
+ * to `display: inline-block`, and `:style="null"` removes it.
9
+ */
10
+ declare const WordHug: vue.DefineComponent<vue.ExtractPropTypes<{
11
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
12
+ as: {
13
+ type: PropType<string | Component>;
14
+ default: string;
15
+ };
16
+ }>, () => VNodeChild[] | null, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
17
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
18
+ as: {
19
+ type: PropType<string | Component>;
20
+ default: string;
21
+ };
22
+ }>> & Readonly<{}>, {
23
+ as: string | Component;
24
+ }, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
25
+
26
+ export { WordHug };
package/dist/vue.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import * as vue from 'vue';
2
+ import { PropType, Component, VNodeChild } from 'vue';
3
+
4
+ /**
5
+ * Render the default slot's text so that every `[bracketed phrase]` is wrapped
6
+ * in an element (an inline-block span by default) that moves to the next line
7
+ * as one unit. `class` and `style` go on each group wrapper; `style` defaults
8
+ * to `display: inline-block`, and `:style="null"` removes it.
9
+ */
10
+ declare const WordHug: vue.DefineComponent<vue.ExtractPropTypes<{
11
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
12
+ as: {
13
+ type: PropType<string | Component>;
14
+ default: string;
15
+ };
16
+ }>, () => VNodeChild[] | null, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
17
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
18
+ as: {
19
+ type: PropType<string | Component>;
20
+ default: string;
21
+ };
22
+ }>> & Readonly<{}>, {
23
+ as: string | Component;
24
+ }, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
25
+
26
+ export { WordHug };
package/dist/vue.js ADDED
@@ -0,0 +1,103 @@
1
+ // src/vue.ts
2
+ import {
3
+ defineComponent,
4
+ h,
5
+ Text
6
+ } from "vue";
7
+
8
+ // src/parse.ts
9
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
10
+ function parseBrackets(input) {
11
+ const pairs = matchBrackets(input);
12
+ const stack = [{ nodes: [], end: -1 }];
13
+ let start = 0;
14
+ for (let i = 0; i < input.length; i++) {
15
+ const top = stack[stack.length - 1];
16
+ if (i === top.end) {
17
+ pushText(top.nodes, input.slice(start, i));
18
+ stack.pop();
19
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
20
+ start = i + 1;
21
+ continue;
22
+ }
23
+ const close = pairs.get(i);
24
+ if (close !== void 0) {
25
+ pushText(top.nodes, input.slice(start, i));
26
+ stack.push({ nodes: [], end: close });
27
+ start = i + 1;
28
+ }
29
+ }
30
+ const root = stack[0].nodes;
31
+ pushText(root, input.slice(start));
32
+ return normalize(root);
33
+ }
34
+ var ESCAPED_BRACKET = /\\([[\]])/g;
35
+ function pushText(nodes, value) {
36
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
37
+ }
38
+ function matchBrackets(input) {
39
+ const pairs = /* @__PURE__ */ new Map();
40
+ const open = [];
41
+ for (let i = 0; i < input.length; i++) {
42
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
43
+ else if (input[i] === "[") open.push(i);
44
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
45
+ }
46
+ return pairs;
47
+ }
48
+ function normalize(nodes) {
49
+ const out = [];
50
+ for (const node of nodes) {
51
+ if (node.type === "group") {
52
+ if (node.children.length > 0) out.push(node);
53
+ continue;
54
+ }
55
+ const last2 = out[out.length - 1];
56
+ if (last2?.type === "text") last2.value += node.value;
57
+ else out.push({ type: "text", value: node.value });
58
+ }
59
+ for (const node of out) {
60
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
61
+ }
62
+ const first = out[0];
63
+ if (first?.type === "text") first.value = first.value.trimStart();
64
+ const last = out[out.length - 1];
65
+ if (last?.type === "text") last.value = last.value.trimEnd();
66
+ return out.filter((node) => node.type === "group" || node.value !== "");
67
+ }
68
+
69
+ // src/vue.ts
70
+ var DEFAULT_STYLE = { display: "inline-block" };
71
+ var WordHug = defineComponent({
72
+ name: "WordHug",
73
+ inheritAttrs: false,
74
+ props: {
75
+ /** Element or component used to wrap each group. Defaults to `"span"`. */
76
+ as: { type: [String, Object, Function], default: "span" }
77
+ },
78
+ setup(props, { slots, attrs }) {
79
+ return () => {
80
+ const text = slotText(slots.default?.());
81
+ if (!text) return null;
82
+ const style = "style" in attrs ? attrs.style : DEFAULT_STYLE;
83
+ const groupProps = {};
84
+ if (attrs.class != null) groupProps.class = attrs.class;
85
+ if (style != null) groupProps.style = style;
86
+ const render = (nodes) => nodes.map(
87
+ (node) => node.type === "text" ? node.value : h(props.as, groupProps, { default: () => render(node.children) })
88
+ );
89
+ return render(parseBrackets(text));
90
+ };
91
+ }
92
+ });
93
+ function slotText(vnodes) {
94
+ let text = "";
95
+ for (const vnode of vnodes ?? []) {
96
+ if (vnode.type === Text && typeof vnode.children === "string") text += vnode.children;
97
+ else if (Array.isArray(vnode.children)) text += slotText(vnode.children);
98
+ }
99
+ return text;
100
+ }
101
+ export {
102
+ WordHug
103
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wordhug",
3
- "version": "0.1.0",
4
- "description": "Keep [bracketed phrases] together on one line in React by wrapping them in inline-block spans.",
3
+ "version": "0.2.0",
4
+ "description": "Keep [bracketed phrases] together on one line in React, Vue, Solid, Svelte and Astro by wrapping them in inline-block spans.",
5
5
  "license": "MIT",
6
6
  "author": "Cody Ta",
7
7
  "repository": {
@@ -31,10 +31,50 @@
31
31
  "default": "./dist/index.cjs"
32
32
  }
33
33
  },
34
+ "./react": {
35
+ "import": {
36
+ "types": "./dist/react.d.ts",
37
+ "default": "./dist/react.js"
38
+ },
39
+ "require": {
40
+ "types": "./dist/react.d.cts",
41
+ "default": "./dist/react.cjs"
42
+ }
43
+ },
44
+ "./vue": {
45
+ "import": {
46
+ "types": "./dist/vue.d.ts",
47
+ "default": "./dist/vue.js"
48
+ },
49
+ "require": {
50
+ "types": "./dist/vue.d.cts",
51
+ "default": "./dist/vue.cjs"
52
+ }
53
+ },
54
+ "./solid": {
55
+ "import": {
56
+ "types": "./dist/solid.d.ts",
57
+ "default": "./dist/solid.js"
58
+ },
59
+ "require": {
60
+ "types": "./dist/solid.d.cts",
61
+ "default": "./dist/solid.cjs"
62
+ }
63
+ },
64
+ "./svelte": {
65
+ "types": "./dist/svelte/index.d.ts",
66
+ "svelte": "./dist/svelte/index.js",
67
+ "default": "./dist/svelte/index.js"
68
+ },
69
+ "./astro": "./dist/astro/WordHug.astro",
34
70
  "./package.json": "./package.json"
35
71
  },
36
72
  "keywords": [
37
73
  "react",
74
+ "vue",
75
+ "solid",
76
+ "svelte",
77
+ "astro",
38
78
  "text",
39
79
  "line-break",
40
80
  "no-wrap",
@@ -42,30 +82,59 @@
42
82
  "brackets"
43
83
  ],
44
84
  "scripts": {
45
- "build": "tsup",
85
+ "build": "tsup && node scripts/copy-components.mjs",
46
86
  "test": "vitest run",
47
87
  "test:watch": "vitest",
48
88
  "typecheck": "tsc --noEmit",
49
89
  "prepublishOnly": "npm run typecheck && npm test && npm run build",
50
- "changeset": "changeset"
90
+ "changeset": "changeset",
91
+ "check:package": "node scripts/check-package.mjs"
51
92
  },
52
93
  "peerDependencies": {
53
- "react": ">=18"
94
+ "@solidjs/web": ">=2",
95
+ "react": ">=18",
96
+ "solid-js": ">=2",
97
+ "svelte": ">=5",
98
+ "vue": ">=3.3"
99
+ },
100
+ "peerDependenciesMeta": {
101
+ "@solidjs/web": {
102
+ "optional": true
103
+ },
104
+ "react": {
105
+ "optional": true
106
+ },
107
+ "solid-js": {
108
+ "optional": true
109
+ },
110
+ "svelte": {
111
+ "optional": true
112
+ },
113
+ "vue": {
114
+ "optional": true
115
+ }
54
116
  },
55
117
  "engines": {
56
118
  "node": ">=18"
57
119
  },
58
120
  "devDependencies": {
59
121
  "@changesets/cli": "^3.0.3",
122
+ "@solidjs/web": "^2.0.3",
123
+ "@sveltejs/vite-plugin-svelte": "^7.3.1",
60
124
  "@testing-library/dom": "^10.4.2",
61
125
  "@testing-library/react": "^16.3.3",
126
+ "@types/node": "^24.19.2",
62
127
  "@types/react": "^19.3.0",
63
128
  "@types/react-dom": "^19.3.0",
129
+ "astro": "^7.4.13",
64
130
  "jsdom": "^31.0.1",
65
131
  "react": "^19.3.1",
66
132
  "react-dom": "^19.3.1",
133
+ "solid-js": "^2.0.3",
134
+ "svelte": "^5.58.0",
67
135
  "tsup": "^9.0.0",
68
136
  "typescript": "^6.0.3",
69
- "vitest": "^5.1.1"
137
+ "vitest": "^5.1.1",
138
+ "vue": "^3.5.45"
70
139
  }
71
140
  }