html-react-parser 4.0.0 → 4.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/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
 
@@ -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.0.0",
3
+ "version": "4.2.0",
4
4
  "description": "HTML to React parser.",
5
5
  "author": "Mark <mark@remarkablemark.org>",
6
6
  "main": "index.js",
@@ -11,6 +11,10 @@
11
11
  "import": "./index.mjs",
12
12
  "require": "./index.js"
13
13
  },
14
+ "./lib/*": {
15
+ "types": "./lib/*.d.ts",
16
+ "default": "./lib/*.js"
17
+ },
14
18
  "./package.json": "./package.json"
15
19
  },
16
20
  "scripts": {
@@ -26,7 +30,7 @@
26
30
  "size-limit": "size-limit",
27
31
  "test": "jest --coverage --testPathIgnorePatterns test/integration/",
28
32
  "test:ci": "npm test -- --ci --colors",
29
- "test:module": "node --experimental-modules test/module/index.mjs",
33
+ "test:module": "node --test test/module/",
30
34
  "test:integration": "npm run build && jest --env=jsdom test/integration/",
31
35
  "test:watch": "npm test -- --watch"
32
36
  },
@@ -51,31 +55,31 @@
51
55
  "style-to-js": "1.1.3"
52
56
  },
53
57
  "devDependencies": {
54
- "@commitlint/cli": "17.6.5",
55
- "@commitlint/config-conventional": "17.6.5",
56
- "@rollup/plugin-commonjs": "25.0.0",
58
+ "@commitlint/cli": "17.6.7",
59
+ "@commitlint/config-conventional": "17.6.7",
60
+ "@rollup/plugin-commonjs": "25.0.3",
57
61
  "@rollup/plugin-node-resolve": "15.1.0",
58
- "@size-limit/preset-big-lib": "8.2.4",
59
- "@types/react": "18.2.7",
60
- "@typescript-eslint/parser": "5.59.8",
62
+ "@size-limit/preset-big-lib": "8.2.6",
63
+ "@types/react": "18.2.15",
64
+ "@typescript-eslint/parser": "6.1.0",
61
65
  "benchmark": "2.1.4",
62
66
  "dtslint": "4.2.1",
63
- "eslint": "8.41.0",
64
- "eslint-plugin-prettier": "4.2.1",
67
+ "eslint": "8.45.0",
68
+ "eslint-plugin-prettier": "5.0.0",
65
69
  "husky": "8.0.3",
66
- "jest": "29.5.0",
67
- "jest-environment-jsdom": "29.5.0",
68
- "lint-staged": "13.2.2",
70
+ "jest": "29.6.1",
71
+ "jest-environment-jsdom": "29.6.1",
72
+ "lint-staged": "13.2.3",
69
73
  "pinst": "3.0.0",
70
- "preact": "10.15.1",
71
- "prettier": "2.8.8",
74
+ "preact": "10.16.0",
75
+ "prettier": "3.0.0",
72
76
  "react": "18.2.0",
73
77
  "react-dom": "18.2.0",
74
78
  "rimraf": "5.0.1",
75
79
  "rollup": "2.79.1",
76
80
  "rollup-plugin-terser": "7.0.2",
77
- "size-limit": "8.2.4",
78
- "typescript": "5.0.4"
81
+ "size-limit": "8.2.6",
82
+ "typescript": "5.1.6"
79
83
  },
80
84
  "peerDependencies": {
81
85
  "react": "0.14 || 15 || 16 || 17 || 18"