@unifast/react 0.0.4
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/dist/index.cjs +156 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.mjs +131 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kenzwada
|
|
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let style_to_object = require("style-to-object");
|
|
25
|
+
style_to_object = __toESM(style_to_object);
|
|
26
|
+
let _unifast_node = require("@unifast/node");
|
|
27
|
+
//#region src/hast-to-react.ts
|
|
28
|
+
const PROP_RENAMES = {
|
|
29
|
+
class: "className",
|
|
30
|
+
for: "htmlFor",
|
|
31
|
+
accesskey: "accessKey",
|
|
32
|
+
autocapitalize: "autoCapitalize",
|
|
33
|
+
autocomplete: "autoComplete",
|
|
34
|
+
autofocus: "autoFocus",
|
|
35
|
+
autoplay: "autoPlay",
|
|
36
|
+
charset: "charSet",
|
|
37
|
+
colspan: "colSpan",
|
|
38
|
+
contenteditable: "contentEditable",
|
|
39
|
+
crossorigin: "crossOrigin",
|
|
40
|
+
datetime: "dateTime",
|
|
41
|
+
enctype: "encType",
|
|
42
|
+
formaction: "formAction",
|
|
43
|
+
formenctype: "formEncType",
|
|
44
|
+
formmethod: "formMethod",
|
|
45
|
+
formnovalidate: "formNoValidate",
|
|
46
|
+
formtarget: "formTarget",
|
|
47
|
+
hreflang: "hrefLang",
|
|
48
|
+
htmlfor: "htmlFor",
|
|
49
|
+
httpequiv: "httpEquiv",
|
|
50
|
+
inputmode: "inputMode",
|
|
51
|
+
maxlength: "maxLength",
|
|
52
|
+
mediagroup: "mediaGroup",
|
|
53
|
+
minlength: "minLength",
|
|
54
|
+
nomodule: "noModule",
|
|
55
|
+
novalidate: "noValidate",
|
|
56
|
+
readonly: "readOnly",
|
|
57
|
+
referrerpolicy: "referrerPolicy",
|
|
58
|
+
rowspan: "rowSpan",
|
|
59
|
+
spellcheck: "spellCheck",
|
|
60
|
+
srcdoc: "srcDoc",
|
|
61
|
+
srclang: "srcLang",
|
|
62
|
+
srcset: "srcSet",
|
|
63
|
+
tabindex: "tabIndex",
|
|
64
|
+
usemap: "useMap"
|
|
65
|
+
};
|
|
66
|
+
function parseStyleString(style) {
|
|
67
|
+
const result = {};
|
|
68
|
+
(0, style_to_object.default)(style, (name, value) => {
|
|
69
|
+
if (name && value) {
|
|
70
|
+
const camelName = name.startsWith("--") ? name : name.replaceAll(/-([a-z])/g, (_, c) => String(c).toUpperCase());
|
|
71
|
+
result[camelName] = value;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
function convertProperties(properties) {
|
|
77
|
+
const props = {};
|
|
78
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
79
|
+
if (value === false || value == null) continue;
|
|
80
|
+
if (key === "style" && typeof value === "string") {
|
|
81
|
+
props.style = parseStyleString(value);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (key === "className" && Array.isArray(value)) {
|
|
85
|
+
props.className = value.join(" ");
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const renamed = PROP_RENAMES[key];
|
|
89
|
+
if (renamed) props[renamed] = value;
|
|
90
|
+
else props[key] = value;
|
|
91
|
+
}
|
|
92
|
+
return props;
|
|
93
|
+
}
|
|
94
|
+
function convertNode(node, options, key) {
|
|
95
|
+
const { createElement, Fragment, components } = options;
|
|
96
|
+
switch (node.type) {
|
|
97
|
+
case "root": return createElement(Fragment, null, ...convertChildren(node.children, options));
|
|
98
|
+
case "element": {
|
|
99
|
+
const component = components?.[node.tagName] ?? node.tagName;
|
|
100
|
+
const props = convertProperties(node.properties);
|
|
101
|
+
props.key = key;
|
|
102
|
+
const children = convertChildren(node.children, options);
|
|
103
|
+
if (children.length > 0) return createElement(component, props, ...children);
|
|
104
|
+
return createElement(component, props);
|
|
105
|
+
}
|
|
106
|
+
case "text": return node.value;
|
|
107
|
+
case "raw": return createElement("div", {
|
|
108
|
+
key,
|
|
109
|
+
dangerouslySetInnerHTML: { __html: node.value }
|
|
110
|
+
});
|
|
111
|
+
case "comment":
|
|
112
|
+
case "doctype": return null;
|
|
113
|
+
default: return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function convertChildren(children, options) {
|
|
117
|
+
const result = [];
|
|
118
|
+
for (let i = 0; i < children.length; i++) {
|
|
119
|
+
const converted = convertNode(children[i], options, i);
|
|
120
|
+
if (converted != null) result.push(converted);
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
function hastToReact(hast, options) {
|
|
125
|
+
return convertNode(hast, options, 0);
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/compile.ts
|
|
129
|
+
function compileToReact(input, options) {
|
|
130
|
+
const { components, createElement, Fragment, ...compileOpts } = options;
|
|
131
|
+
const result = (0, _unifast_node.compile)(input, {
|
|
132
|
+
...compileOpts,
|
|
133
|
+
outputKind: "hast"
|
|
134
|
+
});
|
|
135
|
+
let hast;
|
|
136
|
+
if (typeof result.output === "string") try {
|
|
137
|
+
hast = JSON.parse(result.output);
|
|
138
|
+
} catch {
|
|
139
|
+
throw new Error("Failed to parse HAST output from compiler");
|
|
140
|
+
}
|
|
141
|
+
else hast = result.output;
|
|
142
|
+
return {
|
|
143
|
+
element: hastToReact(hast, {
|
|
144
|
+
createElement,
|
|
145
|
+
Fragment,
|
|
146
|
+
components
|
|
147
|
+
}),
|
|
148
|
+
frontmatter: result.frontmatter,
|
|
149
|
+
diagnostics: result.diagnostics,
|
|
150
|
+
stats: result.stats,
|
|
151
|
+
toc: result.toc
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
exports.compileToReact = compileToReact;
|
|
156
|
+
exports.hastToReact = hastToReact;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CompileOptions, CompileResult, HastRoot, TocEntry } from "@unifast/core";
|
|
2
|
+
|
|
3
|
+
//#region src/hast-to-react.d.ts
|
|
4
|
+
type CreateElement = (type: any, props: any, ...children: any[]) => any;
|
|
5
|
+
type ComponentMap = Record<string, unknown>;
|
|
6
|
+
type HastToReactOptions = {
|
|
7
|
+
createElement: CreateElement;
|
|
8
|
+
Fragment: unknown;
|
|
9
|
+
components?: ComponentMap;
|
|
10
|
+
};
|
|
11
|
+
declare function hastToReact(hast: HastRoot, options: HastToReactOptions): unknown;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/compile.d.ts
|
|
14
|
+
type CompileToReactOptions = CompileOptions & {
|
|
15
|
+
components?: ComponentMap;
|
|
16
|
+
createElement: CreateElement;
|
|
17
|
+
Fragment: unknown;
|
|
18
|
+
};
|
|
19
|
+
type CompileToReactResult = {
|
|
20
|
+
element: unknown;
|
|
21
|
+
frontmatter: Record<string, unknown>;
|
|
22
|
+
diagnostics: CompileResult["diagnostics"];
|
|
23
|
+
stats: CompileResult["stats"];
|
|
24
|
+
toc: TocEntry[];
|
|
25
|
+
};
|
|
26
|
+
declare function compileToReact(input: string, options: CompileToReactOptions): CompileToReactResult;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { type CompileToReactOptions, type CompileToReactResult, type ComponentMap, type CreateElement, type HastToReactOptions, compileToReact, hastToReact };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CompileOptions, CompileResult, HastRoot, TocEntry } from "@unifast/core";
|
|
2
|
+
|
|
3
|
+
//#region src/hast-to-react.d.ts
|
|
4
|
+
type CreateElement = (type: any, props: any, ...children: any[]) => any;
|
|
5
|
+
type ComponentMap = Record<string, unknown>;
|
|
6
|
+
type HastToReactOptions = {
|
|
7
|
+
createElement: CreateElement;
|
|
8
|
+
Fragment: unknown;
|
|
9
|
+
components?: ComponentMap;
|
|
10
|
+
};
|
|
11
|
+
declare function hastToReact(hast: HastRoot, options: HastToReactOptions): unknown;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/compile.d.ts
|
|
14
|
+
type CompileToReactOptions = CompileOptions & {
|
|
15
|
+
components?: ComponentMap;
|
|
16
|
+
createElement: CreateElement;
|
|
17
|
+
Fragment: unknown;
|
|
18
|
+
};
|
|
19
|
+
type CompileToReactResult = {
|
|
20
|
+
element: unknown;
|
|
21
|
+
frontmatter: Record<string, unknown>;
|
|
22
|
+
diagnostics: CompileResult["diagnostics"];
|
|
23
|
+
stats: CompileResult["stats"];
|
|
24
|
+
toc: TocEntry[];
|
|
25
|
+
};
|
|
26
|
+
declare function compileToReact(input: string, options: CompileToReactOptions): CompileToReactResult;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { type CompileToReactOptions, type CompileToReactResult, type ComponentMap, type CreateElement, type HastToReactOptions, compileToReact, hastToReact };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import StyleToObject from "style-to-object";
|
|
2
|
+
import { compile } from "@unifast/node";
|
|
3
|
+
//#region src/hast-to-react.ts
|
|
4
|
+
const PROP_RENAMES = {
|
|
5
|
+
class: "className",
|
|
6
|
+
for: "htmlFor",
|
|
7
|
+
accesskey: "accessKey",
|
|
8
|
+
autocapitalize: "autoCapitalize",
|
|
9
|
+
autocomplete: "autoComplete",
|
|
10
|
+
autofocus: "autoFocus",
|
|
11
|
+
autoplay: "autoPlay",
|
|
12
|
+
charset: "charSet",
|
|
13
|
+
colspan: "colSpan",
|
|
14
|
+
contenteditable: "contentEditable",
|
|
15
|
+
crossorigin: "crossOrigin",
|
|
16
|
+
datetime: "dateTime",
|
|
17
|
+
enctype: "encType",
|
|
18
|
+
formaction: "formAction",
|
|
19
|
+
formenctype: "formEncType",
|
|
20
|
+
formmethod: "formMethod",
|
|
21
|
+
formnovalidate: "formNoValidate",
|
|
22
|
+
formtarget: "formTarget",
|
|
23
|
+
hreflang: "hrefLang",
|
|
24
|
+
htmlfor: "htmlFor",
|
|
25
|
+
httpequiv: "httpEquiv",
|
|
26
|
+
inputmode: "inputMode",
|
|
27
|
+
maxlength: "maxLength",
|
|
28
|
+
mediagroup: "mediaGroup",
|
|
29
|
+
minlength: "minLength",
|
|
30
|
+
nomodule: "noModule",
|
|
31
|
+
novalidate: "noValidate",
|
|
32
|
+
readonly: "readOnly",
|
|
33
|
+
referrerpolicy: "referrerPolicy",
|
|
34
|
+
rowspan: "rowSpan",
|
|
35
|
+
spellcheck: "spellCheck",
|
|
36
|
+
srcdoc: "srcDoc",
|
|
37
|
+
srclang: "srcLang",
|
|
38
|
+
srcset: "srcSet",
|
|
39
|
+
tabindex: "tabIndex",
|
|
40
|
+
usemap: "useMap"
|
|
41
|
+
};
|
|
42
|
+
function parseStyleString(style) {
|
|
43
|
+
const result = {};
|
|
44
|
+
StyleToObject(style, (name, value) => {
|
|
45
|
+
if (name && value) {
|
|
46
|
+
const camelName = name.startsWith("--") ? name : name.replaceAll(/-([a-z])/g, (_, c) => String(c).toUpperCase());
|
|
47
|
+
result[camelName] = value;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
function convertProperties(properties) {
|
|
53
|
+
const props = {};
|
|
54
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
55
|
+
if (value === false || value == null) continue;
|
|
56
|
+
if (key === "style" && typeof value === "string") {
|
|
57
|
+
props.style = parseStyleString(value);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (key === "className" && Array.isArray(value)) {
|
|
61
|
+
props.className = value.join(" ");
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const renamed = PROP_RENAMES[key];
|
|
65
|
+
if (renamed) props[renamed] = value;
|
|
66
|
+
else props[key] = value;
|
|
67
|
+
}
|
|
68
|
+
return props;
|
|
69
|
+
}
|
|
70
|
+
function convertNode(node, options, key) {
|
|
71
|
+
const { createElement, Fragment, components } = options;
|
|
72
|
+
switch (node.type) {
|
|
73
|
+
case "root": return createElement(Fragment, null, ...convertChildren(node.children, options));
|
|
74
|
+
case "element": {
|
|
75
|
+
const component = components?.[node.tagName] ?? node.tagName;
|
|
76
|
+
const props = convertProperties(node.properties);
|
|
77
|
+
props.key = key;
|
|
78
|
+
const children = convertChildren(node.children, options);
|
|
79
|
+
if (children.length > 0) return createElement(component, props, ...children);
|
|
80
|
+
return createElement(component, props);
|
|
81
|
+
}
|
|
82
|
+
case "text": return node.value;
|
|
83
|
+
case "raw": return createElement("div", {
|
|
84
|
+
key,
|
|
85
|
+
dangerouslySetInnerHTML: { __html: node.value }
|
|
86
|
+
});
|
|
87
|
+
case "comment":
|
|
88
|
+
case "doctype": return null;
|
|
89
|
+
default: return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function convertChildren(children, options) {
|
|
93
|
+
const result = [];
|
|
94
|
+
for (let i = 0; i < children.length; i++) {
|
|
95
|
+
const converted = convertNode(children[i], options, i);
|
|
96
|
+
if (converted != null) result.push(converted);
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
function hastToReact(hast, options) {
|
|
101
|
+
return convertNode(hast, options, 0);
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/compile.ts
|
|
105
|
+
function compileToReact(input, options) {
|
|
106
|
+
const { components, createElement, Fragment, ...compileOpts } = options;
|
|
107
|
+
const result = compile(input, {
|
|
108
|
+
...compileOpts,
|
|
109
|
+
outputKind: "hast"
|
|
110
|
+
});
|
|
111
|
+
let hast;
|
|
112
|
+
if (typeof result.output === "string") try {
|
|
113
|
+
hast = JSON.parse(result.output);
|
|
114
|
+
} catch {
|
|
115
|
+
throw new Error("Failed to parse HAST output from compiler");
|
|
116
|
+
}
|
|
117
|
+
else hast = result.output;
|
|
118
|
+
return {
|
|
119
|
+
element: hastToReact(hast, {
|
|
120
|
+
createElement,
|
|
121
|
+
Fragment,
|
|
122
|
+
components
|
|
123
|
+
}),
|
|
124
|
+
frontmatter: result.frontmatter,
|
|
125
|
+
diagnostics: result.diagnostics,
|
|
126
|
+
stats: result.stats,
|
|
127
|
+
toc: result.toc
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
export { compileToReact, hastToReact };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unifast/react",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Convert HAST to React elements for unifast",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.cts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"style-to-object": "^1.0.14",
|
|
23
|
+
"@unifast/core": "0.0.4",
|
|
24
|
+
"@unifast/node": "0.0.4"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/react": "^19.2.14",
|
|
28
|
+
"react": "^19.2.4",
|
|
29
|
+
"typescript": "^5.9.3",
|
|
30
|
+
"vitest": "^4.1.0"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsdown",
|
|
34
|
+
"lint:oxc": "oxlint -c ../../.oxlintrc.json .",
|
|
35
|
+
"lint:oxc:fix": "oxlint -c ../../.oxlintrc.json --fix .",
|
|
36
|
+
"typecheck": "tsc --noEmit",
|
|
37
|
+
"fmt:oxc": "oxfmt --write .",
|
|
38
|
+
"fmt:oxc:check": "oxfmt --check .",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text --coverage.reporter=lcov"
|
|
41
|
+
}
|
|
42
|
+
}
|