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 +21 -0
- package/README.md +170 -2
- package/dist/astro/WordHug.astro +20 -0
- package/dist/index.cjs +118 -0
- package/dist/index.d.cts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +90 -0
- package/dist/react.cjs +106 -0
- package/dist/react.d.cts +25 -0
- package/dist/react.d.ts +25 -0
- package/dist/react.js +79 -0
- package/dist/solid.cjs +114 -0
- package/dist/solid.d.cts +24 -0
- package/dist/solid.d.ts +24 -0
- package/dist/solid.js +89 -0
- package/dist/svelte/WordHug.svelte +17 -0
- package/dist/svelte/index.d.ts +17 -0
- package/dist/svelte/index.js +1 -0
- package/dist/vue.cjs +124 -0
- package/dist/vue.d.cts +26 -0
- package/dist/vue.d.ts +26 -0
- package/dist/vue.js +103 -0
- package/package.json +138 -4
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
|
+
});
|
package/dist/solid.d.cts
ADDED
|
@@ -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.d.ts
ADDED
|
@@ -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
|
+
};
|