html-react-parser 4.0.0 → 4.1.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 +16 -0
- package/dist/html-react-parser.js +11 -4
- package/dist/html-react-parser.js.map +1 -1
- package/dist/html-react-parser.min.js +1 -1
- package/dist/html-react-parser.min.js.map +1 -1
- package/index.d.ts +6 -0
- package/lib/dom-to-react.js +5 -3
- package/lib/utilities.js +6 -1
- package/package.json +17 -17
package/index.d.ts
CHANGED
|
@@ -38,6 +38,12 @@ export interface HTMLReactParserOptions {
|
|
|
38
38
|
domNode: DOMNode
|
|
39
39
|
) => JSX.Element | object | void | undefined | null | false;
|
|
40
40
|
|
|
41
|
+
transform?: (
|
|
42
|
+
reactNode: JSX.Element | string,
|
|
43
|
+
domNode: DOMNode,
|
|
44
|
+
index: number
|
|
45
|
+
) => JSX.Element | string | null;
|
|
46
|
+
|
|
41
47
|
trim?: boolean;
|
|
42
48
|
}
|
|
43
49
|
|
package/lib/dom-to-react.js
CHANGED
|
@@ -11,6 +11,7 @@ var canTextBeChildOfNode = utilities.canTextBeChildOfNode;
|
|
|
11
11
|
* @param {DomElement[]} nodes - DOM nodes.
|
|
12
12
|
* @param {object} [options={}] - Options.
|
|
13
13
|
* @param {Function} [options.replace] - Replacer.
|
|
14
|
+
* @param {Function} [options.transform] - Transform.
|
|
14
15
|
* @param {object} [options.library] - Library (React, Preact, etc.).
|
|
15
16
|
* @returns - String or JSX element(s).
|
|
16
17
|
*/
|
|
@@ -26,6 +27,7 @@ function domToReact(nodes, options) {
|
|
|
26
27
|
var node;
|
|
27
28
|
var isWhitespace;
|
|
28
29
|
var hasReplace = typeof options.replace === 'function';
|
|
30
|
+
var transform = options.transform || utilities.returnFirstArg;
|
|
29
31
|
var replaceElement;
|
|
30
32
|
var props;
|
|
31
33
|
var children;
|
|
@@ -46,7 +48,7 @@ function domToReact(nodes, options) {
|
|
|
46
48
|
key: replaceElement.key || i
|
|
47
49
|
});
|
|
48
50
|
}
|
|
49
|
-
result.push(replaceElement);
|
|
51
|
+
result.push(transform(replaceElement, node, i));
|
|
50
52
|
continue;
|
|
51
53
|
}
|
|
52
54
|
}
|
|
@@ -68,7 +70,7 @@ function domToReact(nodes, options) {
|
|
|
68
70
|
|
|
69
71
|
// We have a text node that's not whitespace and it can be nested
|
|
70
72
|
// in its parent so add it to the results
|
|
71
|
-
result.push(node.data);
|
|
73
|
+
result.push(transform(node.data, node, i));
|
|
72
74
|
continue;
|
|
73
75
|
}
|
|
74
76
|
|
|
@@ -115,7 +117,7 @@ function domToReact(nodes, options) {
|
|
|
115
117
|
props.key = i;
|
|
116
118
|
}
|
|
117
119
|
|
|
118
|
-
result.push(createElement(node.name, props, children));
|
|
120
|
+
result.push(transform(createElement(node.name, props, children), node, i));
|
|
119
121
|
}
|
|
120
122
|
|
|
121
123
|
return result.length === 1 ? result[0] : result;
|
package/lib/utilities.js
CHANGED
|
@@ -120,11 +120,16 @@ function canTextBeChildOfNode(node) {
|
|
|
120
120
|
return !elementsWithNoTextChildren.has(node.name);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
function returnFirstArg(arg) {
|
|
124
|
+
return arg;
|
|
125
|
+
}
|
|
126
|
+
|
|
123
127
|
module.exports = {
|
|
124
128
|
PRESERVE_CUSTOM_ATTRIBUTES: PRESERVE_CUSTOM_ATTRIBUTES,
|
|
125
129
|
invertObject: invertObject,
|
|
126
130
|
isCustomComponent: isCustomComponent,
|
|
127
131
|
setStyleProp: setStyleProp,
|
|
128
132
|
canTextBeChildOfNode: canTextBeChildOfNode,
|
|
129
|
-
elementsWithNoTextChildren: elementsWithNoTextChildren
|
|
133
|
+
elementsWithNoTextChildren: elementsWithNoTextChildren,
|
|
134
|
+
returnFirstArg: returnFirstArg
|
|
130
135
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-react-parser",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "HTML to React parser.",
|
|
5
5
|
"author": "Mark <mark@remarkablemark.org>",
|
|
6
6
|
"main": "index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"size-limit": "size-limit",
|
|
27
27
|
"test": "jest --coverage --testPathIgnorePatterns test/integration/",
|
|
28
28
|
"test:ci": "npm test -- --ci --colors",
|
|
29
|
-
"test:module": "node --
|
|
29
|
+
"test:module": "node --test test/module/",
|
|
30
30
|
"test:integration": "npm run build && jest --env=jsdom test/integration/",
|
|
31
31
|
"test:watch": "npm test -- --watch"
|
|
32
32
|
},
|
|
@@ -51,31 +51,31 @@
|
|
|
51
51
|
"style-to-js": "1.1.3"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@commitlint/cli": "17.6.
|
|
55
|
-
"@commitlint/config-conventional": "17.6.
|
|
56
|
-
"@rollup/plugin-commonjs": "25.0.
|
|
54
|
+
"@commitlint/cli": "17.6.7",
|
|
55
|
+
"@commitlint/config-conventional": "17.6.7",
|
|
56
|
+
"@rollup/plugin-commonjs": "25.0.3",
|
|
57
57
|
"@rollup/plugin-node-resolve": "15.1.0",
|
|
58
|
-
"@size-limit/preset-big-lib": "8.2.
|
|
59
|
-
"@types/react": "18.2.
|
|
60
|
-
"@typescript-eslint/parser": "
|
|
58
|
+
"@size-limit/preset-big-lib": "8.2.6",
|
|
59
|
+
"@types/react": "18.2.15",
|
|
60
|
+
"@typescript-eslint/parser": "6.1.0",
|
|
61
61
|
"benchmark": "2.1.4",
|
|
62
62
|
"dtslint": "4.2.1",
|
|
63
|
-
"eslint": "8.
|
|
64
|
-
"eslint-plugin-prettier": "
|
|
63
|
+
"eslint": "8.45.0",
|
|
64
|
+
"eslint-plugin-prettier": "5.0.0",
|
|
65
65
|
"husky": "8.0.3",
|
|
66
|
-
"jest": "29.
|
|
67
|
-
"jest-environment-jsdom": "29.
|
|
68
|
-
"lint-staged": "13.2.
|
|
66
|
+
"jest": "29.6.1",
|
|
67
|
+
"jest-environment-jsdom": "29.6.1",
|
|
68
|
+
"lint-staged": "13.2.3",
|
|
69
69
|
"pinst": "3.0.0",
|
|
70
|
-
"preact": "10.
|
|
71
|
-
"prettier": "
|
|
70
|
+
"preact": "10.16.0",
|
|
71
|
+
"prettier": "3.0.0",
|
|
72
72
|
"react": "18.2.0",
|
|
73
73
|
"react-dom": "18.2.0",
|
|
74
74
|
"rimraf": "5.0.1",
|
|
75
75
|
"rollup": "2.79.1",
|
|
76
76
|
"rollup-plugin-terser": "7.0.2",
|
|
77
|
-
"size-limit": "8.2.
|
|
78
|
-
"typescript": "5.
|
|
77
|
+
"size-limit": "8.2.6",
|
|
78
|
+
"typescript": "5.1.6"
|
|
79
79
|
},
|
|
80
80
|
"peerDependencies": {
|
|
81
81
|
"react": "0.14 || 15 || 16 || 17 || 18"
|