@frontkom/block-react-parser 1.2.0-beta.1 → 1.3.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/.nvmrc CHANGED
@@ -1 +1 @@
1
- v20.10.0
1
+ 16.16.0
@@ -4,15 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = Block;
7
- var _react = _interopRequireDefault(require("react"));
8
7
  var _Tree = _interopRequireDefault(require("./Tree"));
9
8
  var _innerNode = _interopRequireDefault(require("../utils/innerNode"));
10
9
  var _Context = require("./Context");
11
- var _htmlAttribs = _interopRequireDefault(require("../utils/htmlAttribs"));
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
14
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
15
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
11
  /**
17
12
  * Block element.
18
13
  *
@@ -20,19 +15,18 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
20
15
  * @returns {JSX.Element | null | undefined}
21
16
  */
22
17
  function Block(_ref) {
23
- var block = _ref.block;
24
- var _block$blockName = block.blockName,
25
- blockName = _block$blockName === void 0 ? null : _block$blockName,
26
- innerContent = block.innerContent,
27
- innerBlocks = block.innerBlocks;
28
- var CustomBlock = (0, _Context.useBlockComponent)(blockName);
29
- var htmlAttrs = (0, _htmlAttribs.default)(innerContent[0]);
30
- var blockExtended = _objectSpread(_objectSpread({}, block), {}, {
31
- htmlAttrs: htmlAttrs
32
- });
18
+ let {
19
+ block
20
+ } = _ref;
21
+ const {
22
+ blockName = null,
23
+ innerContent,
24
+ innerBlocks
25
+ } = block;
26
+ const CustomBlock = (0, _Context.useBlockComponent)(blockName);
33
27
  if (CustomBlock) {
34
- return /*#__PURE__*/_react.default.createElement(CustomBlock, {
35
- block: blockExtended
28
+ return /*#__PURE__*/React.createElement(CustomBlock, {
29
+ block: block
36
30
  });
37
31
  }
38
32
 
@@ -41,14 +35,14 @@ function Block(_ref) {
41
35
  return null;
42
36
  }
43
37
  // Filter out empty lines and orphaned closing tags.
44
- if (innerContent.length === 1 && (innerContent[0] === '\n' || innerContent[0].substring(0, 2) === '</')) {
38
+ if (innerContent.length === 1 && (innerContent[0] === "\n" || innerContent[0].substring(0, 2) === "</")) {
45
39
  return null;
46
40
  }
47
- var node = (0, _innerNode.default)(innerBlocks, innerContent);
41
+ const node = (0, _innerNode.default)(innerBlocks, innerContent);
48
42
  if (node) {
49
- return /*#__PURE__*/_react.default.createElement(_Tree.default, {
43
+ return /*#__PURE__*/React.createElement(_Tree.default, {
50
44
  node: node,
51
- block: blockExtended
45
+ block: block
52
46
  });
53
47
  }
54
48
  return null;
@@ -9,21 +9,23 @@ exports.useComponentContext = useComponentContext;
9
9
  exports.useTagComponent = useTagComponent;
10
10
  var _react = require("react");
11
11
  var _elements = require("../elements");
12
- var TxContext = /*#__PURE__*/(0, _react.createContext)();
13
- var Provider = TxContext.Provider;
12
+ const TxContext = /*#__PURE__*/(0, _react.createContext)();
13
+ const {
14
+ Provider
15
+ } = TxContext;
14
16
  exports.Provider = Provider;
15
17
  function useComponentContext() {
16
18
  return (0, _react.useContext)(TxContext);
17
19
  }
18
20
  function useBlockComponent(name) {
19
- var _useContext = (0, _react.useContext)(TxContext),
20
- _useContext$CustomBlo = _useContext.CustomBlocks,
21
- CustomBlocks = _useContext$CustomBlo === void 0 ? _elements.coreBlocks : _useContext$CustomBlo;
21
+ const {
22
+ CustomBlocks = _elements.coreBlocks
23
+ } = (0, _react.useContext)(TxContext);
22
24
  return name && CustomBlocks[name];
23
25
  }
24
26
  function useTagComponent(tag) {
25
- var _useContext2 = (0, _react.useContext)(TxContext),
26
- _useContext2$CustomTa = _useContext2.CustomTags,
27
- CustomTags = _useContext2$CustomTa === void 0 ? _elements.coreTags : _useContext2$CustomTa;
27
+ const {
28
+ CustomTags = _elements.coreTags
29
+ } = (0, _react.useContext)(TxContext);
28
30
  return tag && CustomTags[tag];
29
31
  }
@@ -4,61 +4,42 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = Tree;
7
- var _react = _interopRequireDefault(require("react"));
8
7
  var _Context = require("./Context");
9
8
  var _Block = _interopRequireDefault(require("./Block"));
10
9
  var _attribsProps = _interopRequireDefault(require("../utils/attribsProps"));
11
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
11
  function Tree(_ref) {
13
- var _node$children2;
14
- var node = _ref.node,
15
- block = _ref.block;
16
- var CustomTag = (0, _Context.useTagComponent)(node.name);
12
+ let {
13
+ node,
14
+ block
15
+ } = _ref;
16
+ const CustomTag = (0, _Context.useTagComponent)(node.name);
17
17
  (0, _attribsProps.default)(node.attribs);
18
- if (node.type === 'text') {
19
- if (node.data === '[innerBlocks]') {
20
- var _block$innerBlocks;
18
+ if (node.type === "text") {
19
+ if (node.data === "[innerBlocks]") {
21
20
  // eslint-disable-next-line react/no-array-index-key
22
- return (_block$innerBlocks = block.innerBlocks) === null || _block$innerBlocks === void 0 ? void 0 : _block$innerBlocks.map(function (inner, index) {
23
- return /*#__PURE__*/_react.default.createElement(_Block.default, {
24
- block: inner,
25
- key: index
26
- });
27
- });
21
+ return block.innerBlocks?.map((inner, index) => /*#__PURE__*/React.createElement(_Block.default, {
22
+ block: inner,
23
+ key: index
24
+ }));
28
25
  }
29
26
  return node.data;
30
27
  }
31
28
 
32
29
  // Handle selfclosed elements (???)
33
30
  if (CustomTag) {
34
- var _node$children;
35
- var tagChildren = (_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.map(function (child, index) {
36
- return (
37
- /*#__PURE__*/
38
- // eslint-disable-next-line react/no-array-index-key
39
- _react.default.createElement(Tree, {
40
- node: child,
41
- block: block,
42
- key: index
43
- })
44
- );
45
- });
46
- return /*#__PURE__*/_react.default.createElement(CustomTag, {
47
- node: tagChildren,
31
+ return /*#__PURE__*/React.createElement(CustomTag, {
48
32
  attribs: node.attribs
49
33
  });
50
34
  }
51
- var Component = node.name;
52
- var attrs = (0, _attribsProps.default)(node.attribs);
53
- return /*#__PURE__*/_react.default.createElement(Component, attrs, (_node$children2 = node.children) === null || _node$children2 === void 0 ? void 0 : _node$children2.map(function (child, index) {
54
- return (
55
- /*#__PURE__*/
56
- // eslint-disable-next-line react/no-array-index-key
57
- _react.default.createElement(Tree, {
58
- node: child,
59
- block: block,
60
- key: index
61
- })
62
- );
63
- }));
35
+ const Component = node.name;
36
+ const attrs = (0, _attribsProps.default)(node.attribs);
37
+ return /*#__PURE__*/React.createElement(Component, attrs, node.children?.map((child, index) =>
38
+ /*#__PURE__*/
39
+ // eslint-disable-next-line react/no-array-index-key
40
+ React.createElement(Tree, {
41
+ node: child,
42
+ block: block,
43
+ key: index
44
+ })));
64
45
  }
@@ -4,36 +4,40 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.customTags = exports.customBlocks = exports.coreTags = exports.coreBlocks = void 0;
7
- var _react = _interopRequireDefault(require("react"));
8
7
  var _img = _interopRequireDefault(require("./tags/img"));
9
8
  var _selfClosing = _interopRequireDefault(require("./tags/selfClosing"));
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
12
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
13
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
- var coreTags = {
15
- img: function img(_ref) {
16
- var attribs = _ref.attribs;
17
- return /*#__PURE__*/_react.default.createElement(_img.default, {
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ const coreTags = exports.coreTags = {
11
+ img: _ref => {
12
+ let {
13
+ attribs
14
+ } = _ref;
15
+ return /*#__PURE__*/React.createElement(_img.default, {
18
16
  attribs: attribs
19
17
  });
20
18
  },
21
- br: function br(_ref2) {
22
- var attribs = _ref2.attribs;
23
- return /*#__PURE__*/_react.default.createElement(_selfClosing.default, {
19
+ br: _ref2 => {
20
+ let {
21
+ attribs
22
+ } = _ref2;
23
+ return /*#__PURE__*/React.createElement(_selfClosing.default, {
24
24
  attribs: attribs,
25
25
  tag: "br"
26
26
  });
27
27
  },
28
- hr: function hr(_ref3) {
29
- var attribs = _ref3.attribs;
30
- return /*#__PURE__*/_react.default.createElement(_selfClosing.default, {
28
+ hr: _ref3 => {
29
+ let {
30
+ attribs
31
+ } = _ref3;
32
+ return /*#__PURE__*/React.createElement(_selfClosing.default, {
31
33
  attribs: attribs,
32
34
  tag: "hr"
33
35
  });
34
36
  },
35
- meta: function meta(_ref4) {
36
- var attribs = _ref4.attribs;
37
+ meta: _ref4 => {
38
+ let {
39
+ attribs
40
+ } = _ref4;
37
41
  return null;
38
42
  }
39
43
  // area: ({attribs}) => <SelfClosing attribs={attribs} tag="area" />,
@@ -48,8 +52,7 @@ var coreTags = {
48
52
  // track: ({attribs}) => <SelfClosing attribs={attribs} tag="track" />,
49
53
  // wbr: ({attribs}) => <SelfClosing attribs={attribs} tag="wbr" />,
50
54
  };
51
- exports.coreTags = coreTags;
52
- var coreBlocks = {
55
+ const coreBlocks = exports.coreBlocks = {
53
56
  // 'core/archives': ({ block }) => doSomething(),
54
57
  // 'core/audio': ({ block }) => doSomething(),
55
58
  // 'core/avatar': ({ block }) => doSomething(),
@@ -151,10 +154,12 @@ var coreBlocks = {
151
154
  * @param {object} tags - Optional object with custom blocks definitions. Empty by default.
152
155
  * @returns {object} Object with blocks definitions
153
156
  */
154
- exports.coreBlocks = coreBlocks;
155
- var customTags = function customTags() {
156
- var tags = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
157
- return _objectSpread(_objectSpread({}, coreTags), tags);
157
+ const customTags = function () {
158
+ let tags = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
159
+ return {
160
+ ...coreTags,
161
+ ...tags
162
+ };
158
163
  };
159
164
 
160
165
  /**
@@ -165,8 +170,11 @@ var customTags = function customTags() {
165
170
  * @returns {object} Object with blocks definitions
166
171
  */
167
172
  exports.customTags = customTags;
168
- var customBlocks = function customBlocks() {
169
- var blocks = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
170
- return _objectSpread(_objectSpread({}, coreBlocks), blocks);
173
+ const customBlocks = function () {
174
+ let blocks = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
175
+ return {
176
+ ...coreBlocks,
177
+ ...blocks
178
+ };
171
179
  };
172
180
  exports.customBlocks = customBlocks;
@@ -5,14 +5,16 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = Image;
7
7
  function Image(_ref) {
8
- var attribs = _ref.attribs;
9
- var _attribs$alt = attribs.alt,
10
- alt = _attribs$alt === void 0 ? '' : _attribs$alt,
11
- _attribs$class = attribs.class,
12
- className = _attribs$class === void 0 ? '' : _attribs$class,
13
- src = attribs.src,
14
- height = attribs.height,
15
- width = attribs.width;
8
+ let {
9
+ attribs
10
+ } = _ref;
11
+ const {
12
+ alt = '',
13
+ class: className = '',
14
+ src,
15
+ height,
16
+ width
17
+ } = attribs;
16
18
  return /*#__PURE__*/React.createElement("img", {
17
19
  alt: alt,
18
20
  className: className,
@@ -5,11 +5,13 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = SelfClosing;
7
7
  var _attribsProps = _interopRequireDefault(require("../../utils/attribsProps"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
9
  function SelfClosing(_ref) {
10
- var attribs = _ref.attribs,
11
- tag = _ref.tag;
12
- var Component = tag;
13
- var attributes = (0, _attribsProps.default)(attribs);
10
+ let {
11
+ attribs,
12
+ tag
13
+ } = _ref;
14
+ const Component = tag;
15
+ const attributes = (0, _attribsProps.default)(attribs);
14
16
  return /*#__PURE__*/React.createElement(Component, attributes);
15
17
  }
package/dist/index.js CHANGED
@@ -5,49 +5,49 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  Object.defineProperty(exports, "Block", {
7
7
  enumerable: true,
8
- get: function get() {
8
+ get: function () {
9
9
  return _Block.default;
10
10
  }
11
11
  });
12
12
  Object.defineProperty(exports, "Provider", {
13
13
  enumerable: true,
14
- get: function get() {
14
+ get: function () {
15
15
  return _Context.Provider;
16
16
  }
17
17
  });
18
18
  Object.defineProperty(exports, "Tree", {
19
19
  enumerable: true,
20
- get: function get() {
20
+ get: function () {
21
21
  return _Tree.default;
22
22
  }
23
23
  });
24
24
  Object.defineProperty(exports, "attribsProps", {
25
25
  enumerable: true,
26
- get: function get() {
26
+ get: function () {
27
27
  return _attribsProps.default;
28
28
  }
29
29
  });
30
30
  Object.defineProperty(exports, "customBlocks", {
31
31
  enumerable: true,
32
- get: function get() {
32
+ get: function () {
33
33
  return _elements.customBlocks;
34
34
  }
35
35
  });
36
36
  Object.defineProperty(exports, "customTags", {
37
37
  enumerable: true,
38
- get: function get() {
38
+ get: function () {
39
39
  return _elements.customTags;
40
40
  }
41
41
  });
42
42
  Object.defineProperty(exports, "innerNode", {
43
43
  enumerable: true,
44
- get: function get() {
44
+ get: function () {
45
45
  return _innerNode.default;
46
46
  }
47
47
  });
48
48
  Object.defineProperty(exports, "parseBlocks", {
49
49
  enumerable: true,
50
- get: function get() {
50
+ get: function () {
51
51
  return _parseBlocks.default;
52
52
  }
53
53
  });
@@ -58,4 +58,4 @@ var _elements = require("./elements");
58
58
  var _attribsProps = _interopRequireDefault(require("./utils/attribsProps"));
59
59
  var _innerNode = _interopRequireDefault(require("./utils/innerNode"));
60
60
  var _parseBlocks = _interopRequireDefault(require("./utils/parseBlocks"));
61
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
61
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -6,12 +6,12 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _reactAttrConverter = _interopRequireDefault(require("react-attr-converter"));
8
8
  var _styleToJs = _interopRequireDefault(require("style-to-js"));
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- var attribsProps = function attribsProps(attribs) {
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ const attribsProps = attribs => {
11
11
  if (attribs === undefined) {
12
12
  return {};
13
13
  }
14
- var props = Object.fromEntries(Object.entries(attribs).map(function (attribute) {
14
+ const props = Object.fromEntries(Object.entries(attribs).map(attribute => {
15
15
  if (attribute[0] === 'style') {
16
16
  return [(0, _reactAttrConverter.default)(attribute[0]), (0, _styleToJs.default)(attribute[1])];
17
17
  }
@@ -19,5 +19,4 @@ var attribsProps = function attribsProps(attribs) {
19
19
  }));
20
20
  return props;
21
21
  };
22
- var _default = attribsProps;
23
- exports.default = _default;
22
+ var _default = exports.default = attribsProps;
@@ -5,17 +5,15 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _htmlparser = require("htmlparser2");
8
- var innerNode = function innerNode(innerBlocks, innerContent) {
9
- var _tree$children$;
8
+ const innerNode = (innerBlocks, innerContent) => {
10
9
  // If no inner blocks, return the block markup.
11
10
  // If inner blocks, return the wrapping markup.
12
- var innerHtml = !innerBlocks.length ? innerContent[0] : "".concat(innerContent[0], "[innerBlocks]").concat(innerContent[innerContent.length - 1]);
13
- var html = innerHtml ? innerHtml.trim() : '';
14
- var tree = (0, _htmlparser.parseDocument)(html, {
11
+ const innerHtml = !innerBlocks.length ? innerContent[0] : `${innerContent[0]}[innerBlocks]${innerContent[innerContent.length - 1]}`;
12
+ const html = innerHtml ? innerHtml.trim() : "";
13
+ const tree = (0, _htmlparser.parseDocument)(html, {
15
14
  lowerCaseTags: true,
16
15
  recognizeSelfClosing: true
17
16
  });
18
- return (_tree$children$ = tree.children[0]) !== null && _tree$children$ !== void 0 ? _tree$children$ : null;
17
+ return tree.children[0] ?? null;
19
18
  };
20
- var _default = innerNode;
21
- exports.default = _default;
19
+ var _default = exports.default = innerNode;
@@ -6,20 +6,15 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _blockSerializationDefaultParser = require("@wordpress/block-serialization-default-parser");
8
8
  var _Block = _interopRequireDefault(require("../components/Block"));
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
10
  /**
11
11
  * Parse Gutenberg blocks from HTML markup.
12
12
  *
13
13
  * @param {string} html - markup rendered by Gutenberg editor.
14
14
  * @returns {JSX.Element[]}
15
15
  */
16
- var parseBlocks = function parseBlocks(html) {
17
- return (0, _blockSerializationDefaultParser.parse)(html.trim()).map(function (block, key) {
18
- return /*#__PURE__*/React.createElement(_Block.default, {
19
- block: block,
20
- key: key
21
- });
22
- });
23
- };
24
- var _default = parseBlocks;
25
- exports.default = _default;
16
+ const parseBlocks = html => (0, _blockSerializationDefaultParser.parse)(html.trim()).map((block, key) => /*#__PURE__*/React.createElement(_Block.default, {
17
+ block: block,
18
+ key: key
19
+ }));
20
+ var _default = exports.default = parseBlocks;
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@frontkom/block-react-parser",
3
- "version": "1.2.0-beta.1",
3
+ "version": "1.3.0",
4
4
  "description": "Gutenberg-generated HTML to React parser.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
7
  "scripts": {
8
8
  "build": "rm -rf dist && NODE_ENV=production babel src --out-dir dist --copy-files",
9
- "format": "prettier --write \"**/*.{js,jsx,md}\"",
10
- "lint": "eslint --quiet src/"
9
+ "test": "node --test"
11
10
  },
12
11
  "keywords": [
13
12
  "html",
@@ -18,7 +17,7 @@
18
17
  "author": "Roberto Ornelas <rob@frontkom.com>",
19
18
  "license": "ISC",
20
19
  "dependencies": {
21
- "@wordpress/block-serialization-default-parser": "^4.21.0",
20
+ "@wordpress/block-serialization-default-parser": "^5.37.0",
22
21
  "htmlparser2": "^8.0.1",
23
22
  "react-attr-converter": "^0.3.1",
24
23
  "style-to-js": "^1.1.1"
@@ -28,21 +27,12 @@
28
27
  "@babel/core": "^7.20.2",
29
28
  "@babel/preset-env": "^7.20.2",
30
29
  "@babel/preset-react": "^7.18.6",
31
- "eslint": "^8.31.0",
32
- "eslint-config-airbnb": "^19.0.4",
33
- "eslint-config-prettier": "^8.6.0",
34
- "eslint-plugin-import": "^2.27.4",
35
- "eslint-plugin-jsx-a11y": "^6.7.1",
36
- "eslint-plugin-prettier": "^4.2.1",
37
- "eslint-plugin-react": "^7.32.0",
38
- "eslint-plugin-react-hooks": "^4.6.0",
39
- "prettier": "^2.8.2",
40
- "react": "^17.0.2",
41
- "react-dom": "^17.0.2"
30
+ "react": "^19.0.0",
31
+ "react-dom": "^19.0.0"
42
32
  },
43
33
  "peerDependencies": {
44
- "react": "^17.0.2 || ^18.0.0",
45
- "react-dom": "^17.0.2 || ^18.0.0"
34
+ "react": "^17.0.2 || ^18.0.0 || ^19.0.0",
35
+ "react-dom": "^17.0.2 || ^18.0.0 || ^19.0.0"
46
36
  },
47
37
  "babel": {
48
38
  "presets": [
@@ -1,8 +1,6 @@
1
- import React from 'react';
2
- import Tree from './Tree';
3
- import innerNode from '../utils/innerNode';
4
- import { useBlockComponent } from './Context';
5
- import htmlAttribs from '../utils/htmlAttribs';
1
+ import Tree from "./Tree";
2
+ import innerNode from "../utils/innerNode";
3
+ import { useBlockComponent } from "./Context";
6
4
 
7
5
  /**
8
6
  * Block element.
@@ -14,11 +12,8 @@ export default function Block({ block }) {
14
12
  const { blockName = null, innerContent, innerBlocks } = block;
15
13
  const CustomBlock = useBlockComponent(blockName);
16
14
 
17
- const htmlAttrs = htmlAttribs(innerContent[0]);
18
- const blockExtended = { ...block, htmlAttrs };
19
-
20
15
  if (CustomBlock) {
21
- return <CustomBlock block={blockExtended} />;
16
+ return <CustomBlock block={block} />;
22
17
  }
23
18
 
24
19
  // Filter out empty blocks.
@@ -28,14 +23,14 @@ export default function Block({ block }) {
28
23
  // Filter out empty lines and orphaned closing tags.
29
24
  if (
30
25
  innerContent.length === 1 &&
31
- (innerContent[0] === '\n' || innerContent[0].substring(0, 2) === '</')
26
+ (innerContent[0] === "\n" || innerContent[0].substring(0, 2) === "</")
32
27
  ) {
33
28
  return null;
34
29
  }
35
30
 
36
31
  const node = innerNode(innerBlocks, innerContent);
37
32
  if (node) {
38
- return <Tree node={node} block={blockExtended} />;
33
+ return <Tree node={node} block={block} />;
39
34
  }
40
35
 
41
36
  return null;
@@ -1,17 +1,18 @@
1
- import React from 'react';
2
- import { useTagComponent } from './Context';
3
- import Block from './Block';
4
- import attribsProps from '../utils/attribsProps';
1
+ import { useTagComponent } from "./Context";
2
+ import Block from "./Block";
3
+ import attribsProps from "../utils/attribsProps";
5
4
 
6
5
  export default function Tree({ node, block }) {
7
6
  const CustomTag = useTagComponent(node.name);
8
7
 
9
8
  attribsProps(node.attribs);
10
9
 
11
- if (node.type === 'text') {
12
- if (node.data === '[innerBlocks]') {
10
+ if (node.type === "text") {
11
+ if (node.data === "[innerBlocks]") {
13
12
  // eslint-disable-next-line react/no-array-index-key
14
- return block.innerBlocks?.map((inner, index) => <Block block={inner} key={index} />);
13
+ return block.innerBlocks?.map((inner, index) => (
14
+ <Block block={inner} key={index} />
15
+ ));
15
16
  }
16
17
 
17
18
  return node.data;
@@ -19,12 +20,7 @@ export default function Tree({ node, block }) {
19
20
 
20
21
  // Handle selfclosed elements (???)
21
22
  if (CustomTag) {
22
- const tagChildren = node.children?.map((child, index) => (
23
- // eslint-disable-next-line react/no-array-index-key
24
- <Tree node={child} block={block} key={index} />
25
- ));
26
-
27
- return <CustomTag node={tagChildren} attribs={node.attribs} />;
23
+ return <CustomTag attribs={node.attribs} />;
28
24
  }
29
25
 
30
26
  const Component = node.name;
@@ -1,5 +1,3 @@
1
- import React from 'react';
2
-
3
1
  import Image from './tags/img';
4
2
  import SelfClosing from './tags/selfClosing';
5
3
 
@@ -7,7 +5,9 @@ export const coreTags = {
7
5
  img: ({ attribs }) => <Image attribs={attribs} />,
8
6
  br: ({ attribs }) => <SelfClosing attribs={attribs} tag="br" />,
9
7
  hr: ({ attribs }) => <SelfClosing attribs={attribs} tag="hr" />,
10
- meta: ({ attribs }) => null,
8
+ meta: ({ attribs }) => {
9
+ return null;
10
+ },
11
11
  // area: ({attribs}) => <SelfClosing attribs={attribs} tag="area" />,
12
12
  // base: ({attribs}) => <SelfClosing attribs={attribs} tag="base" />,
13
13
  // col: ({attribs}) => <SelfClosing attribs={attribs} tag="col" />,
@@ -0,0 +1,13 @@
1
+ export default function Image({ attribs }) {
2
+ const { alt = '', class: className = '', src, height, width } = attribs;
3
+
4
+ return (
5
+ <img
6
+ alt={alt}
7
+ className={className}
8
+ src={src}
9
+ width={width}
10
+ height={height}
11
+ />
12
+ );
13
+ }
@@ -1,18 +1,18 @@
1
1
  import convert from 'react-attr-converter';
2
2
  import parseStyle from 'style-to-js';
3
3
 
4
- const attribsProps = attribs => {
4
+ const attribsProps = (attribs) => {
5
5
  if (attribs === undefined) {
6
6
  return {};
7
7
  }
8
8
 
9
9
  const props = Object.fromEntries(
10
- Object.entries(attribs).map(attribute => {
10
+ Object.entries(attribs).map((attribute) => {
11
11
  if (attribute[0] === 'style') {
12
12
  return [convert(attribute[0]), parseStyle(attribute[1])];
13
13
  }
14
14
  return [convert(attribute[0]), attribute[1]];
15
- }),
15
+ })
16
16
  );
17
17
 
18
18
  return props;
@@ -1,4 +1,4 @@
1
- import { parseDocument } from 'htmlparser2';
1
+ import { parseDocument } from "htmlparser2";
2
2
 
3
3
  const innerNode = (innerBlocks, innerContent) => {
4
4
  // If no inner blocks, return the block markup.
@@ -6,7 +6,7 @@ const innerNode = (innerBlocks, innerContent) => {
6
6
  const innerHtml = !innerBlocks.length
7
7
  ? innerContent[0]
8
8
  : `${innerContent[0]}[innerBlocks]${innerContent[innerContent.length - 1]}`;
9
- const html = innerHtml ? innerHtml.trim() : '';
9
+ const html = innerHtml ? innerHtml.trim() : "";
10
10
 
11
11
  const tree = parseDocument(html, {
12
12
  lowerCaseTags: true,
@@ -1,5 +1,5 @@
1
- import { parse } from '@wordpress/block-serialization-default-parser';
2
- import Block from '../components/Block';
1
+ import { parse } from "@wordpress/block-serialization-default-parser";
2
+ import Block from "../components/Block";
3
3
 
4
4
  /**
5
5
  * Parse Gutenberg blocks from HTML markup.
@@ -7,7 +7,7 @@ import Block from '../components/Block';
7
7
  * @param {string} html - markup rendered by Gutenberg editor.
8
8
  * @returns {JSX.Element[]}
9
9
  */
10
- const parseBlocks = html =>
10
+ const parseBlocks = (html) =>
11
11
  parse(html.trim()).map((block, key) => <Block block={block} key={key} />);
12
12
 
13
13
  export default parseBlocks;
@@ -0,0 +1,61 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { parse } from '@wordpress/block-serialization-default-parser';
4
+
5
+ const html = `
6
+ <!-- wp:columns {"columns":3} -->
7
+ <div class="wp-block-columns has-3-columns">
8
+ <!-- wp:column -->
9
+ <div class="wp-block-column">
10
+ <!-- wp:paragraph -->
11
+ <p>Left</p>
12
+ <!-- /wp:paragraph -->
13
+ </div>
14
+ <!-- /wp:column -->
15
+
16
+ <!-- wp:column -->
17
+ <div class="wp-block-column">
18
+ <!-- wp:paragraph -->
19
+ <p><strong>Middle</strong></p>
20
+ <!-- /wp:paragraph -->
21
+ </div>
22
+ <!-- /wp:column -->
23
+
24
+ <!-- wp:column -->
25
+ <div class="wp-block-column"></div>
26
+ <!-- /wp:column -->
27
+ </div>
28
+ <!-- /wp:columns -->
29
+ `;
30
+
31
+ test('parses columns block structure', () => {
32
+ const blocks = parse(html.trim());
33
+ assert.equal(blocks.length, 1);
34
+
35
+ const [columns] = blocks;
36
+ assert.equal(columns.blockName, 'core/columns');
37
+ assert.deepStrictEqual(columns.attrs, { columns: 3 });
38
+ assert.equal(columns.innerBlocks.length, 3);
39
+
40
+ const [first, second, third] = columns.innerBlocks;
41
+
42
+ assert.equal(first.blockName, 'core/column');
43
+ assert.deepStrictEqual(first.attrs ?? {}, {});
44
+ assert.equal(first.innerBlocks.length, 1);
45
+ assert.equal(first.innerBlocks[0].blockName, 'core/paragraph');
46
+ assert.deepStrictEqual(first.innerBlocks[0].attrs ?? {}, {});
47
+ assert.equal(first.innerBlocks[0].innerBlocks.length, 0);
48
+ assert.equal(first.innerBlocks[0].innerHTML.trim(), '<p>Left</p>');
49
+
50
+ assert.equal(second.blockName, 'core/column');
51
+ assert.deepStrictEqual(second.attrs ?? {}, {});
52
+ assert.equal(second.innerBlocks.length, 1);
53
+ assert.equal(second.innerBlocks[0].blockName, 'core/paragraph');
54
+ assert.deepStrictEqual(second.innerBlocks[0].attrs ?? {}, {});
55
+ assert.equal(second.innerBlocks[0].innerBlocks.length, 0);
56
+ assert.equal(second.innerBlocks[0].innerHTML.trim(), '<p><strong>Middle</strong></p>');
57
+
58
+ assert.equal(third.blockName, 'core/column');
59
+ assert.deepStrictEqual(third.attrs ?? {}, {});
60
+ assert.equal(third.innerBlocks.length, 0);
61
+ });
package/.eslintrc.js DELETED
@@ -1,15 +0,0 @@
1
- module.exports = {
2
- env: {
3
- browser: true,
4
- es2021: true,
5
- node: true,
6
- },
7
- extends: ['airbnb', 'plugin:prettier/recommended', 'plugin:react/recommended'],
8
- overrides: [],
9
- parserOptions: {
10
- ecmaVersion: 'latest',
11
- sourceType: 'module',
12
- },
13
- plugins: ['react'],
14
- rules: {},
15
- };
package/.prettierignore DELETED
@@ -1,4 +0,0 @@
1
- .cache
2
- package.json
3
- package-lock.json
4
- dist
package/.prettierrc DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "arrowParens": "avoid",
3
- "printWidth": 100,
4
- "singleQuote": true,
5
- "tabWidth": 2,
6
- "trailingComma": "all"
7
- }
8
-
@@ -1,41 +0,0 @@
1
- ## [1.0.0](https://github.com/frontkom/block-react-parser/compare/v0.1.0...v1.0.0) (2024-03-06)
2
-
3
-
4
- ### ⚠ BREAKING CHANGES
5
-
6
- * cleanup changelog
7
-
8
- ### Features
9
-
10
- * cleanup changelog ([31e66b9](https://github.com/frontkom/block-react-parser/commit/31e66b900b5994959569784ce04c85150c0f88e6))
11
-
12
-
13
- ### Documentation
14
-
15
- * cleanup changelog ([834ab23](https://github.com/frontkom/block-react-parser/commit/834ab23d057090ccaf8f179b386f6aee684ced90))
16
-
17
-
18
- ### Miscellaneous Chores
19
-
20
- * **DEVOPS-506:** add $schema and contributors to package.json ([8dc9883](https://github.com/frontkom/block-react-parser/commit/8dc98834c25dfdb0321c2ae41e3c877733466d5a))
21
- * **DEVOPS-506:** add id-token to permissions in GitHub actions ([8901401](https://github.com/frontkom/block-react-parser/commit/8901401a3d092c6ea18d8f5129c826a78f10225b))
22
- * **DEVOPS-506:** change build package action ([42c2035](https://github.com/frontkom/block-react-parser/commit/42c20356a96a6f664f46b4946bddf70a2838d8e0))
23
- * **DEVOPS-506:** change place of contributors in package.json (for test github actions ) ([ccb2b38](https://github.com/frontkom/block-react-parser/commit/ccb2b3818c10f6a7a861fcc93923dac699ffc1ee))
24
- * **DEVOPS-506:** cleanup semantic-release.yaml ([8775be9](https://github.com/frontkom/block-react-parser/commit/8775be980af665d4db3403ab80af9c642464a01f))
25
- * **DEVOPS-506:** fix build package action config ([0a99ae0](https://github.com/frontkom/block-react-parser/commit/0a99ae08358445e3e6f48e6621ed58f2f8d2e6a5))
26
- * **DEVOPS-506:** fix build package action config ([d9559e0](https://github.com/frontkom/block-react-parser/commit/d9559e015dfaffaee3590a5002cd6f540c6c850a))
27
- * **DEVOPS-506:** merge develop to beta ([637317e](https://github.com/frontkom/block-react-parser/commit/637317e40ba319374f162eb2d2b1b4f330649d7a))
28
- * **DEVOPS-506:** remove dist folder ([390ece7](https://github.com/frontkom/block-react-parser/commit/390ece75eb8bb66e789fa841e71ad6a309b7f5d7))
29
- * **DEVOPS-506:** remove dist folder from assets in @semantic-release/git ([ab67ce1](https://github.com/frontkom/block-react-parser/commit/ab67ce113828e7b4f471ef399740b4e64dd171c9))
30
- * **release:** 0.1.1 [skip ci] ([011f395](https://github.com/frontkom/block-react-parser/commit/011f39599ca5b94a5c3448aada36bd6d76d3f7ab))
31
- * **release:** 0.1.2 [skip ci] ([05be598](https://github.com/frontkom/block-react-parser/commit/05be59829e35a321b4bddbc4e0fd7789bcd36522))
32
- * **release:** 1.0.0 [skip ci] ([d6bea1a](https://github.com/frontkom/block-react-parser/commit/d6bea1a940a70b737c4dc9bd99d739bb8ce4fd63))
33
- * **release:** 1.0.0-beta.10 [skip ci] ([d69454f](https://github.com/frontkom/block-react-parser/commit/d69454f6b8c5e81ce32b6824fbc10747e8fe3476))
34
- * **release:** 1.0.0-beta.2 [skip ci] ([7e54ad1](https://github.com/frontkom/block-react-parser/commit/7e54ad166b46ada37a3865361ae7aa3d5bbd6164))
35
- * **release:** 1.0.0-beta.3 [skip ci] ([00ca556](https://github.com/frontkom/block-react-parser/commit/00ca556807d918ece4fbca575f402de3dd268d42))
36
- * **release:** 1.0.0-beta.4 [skip ci] ([89bfee0](https://github.com/frontkom/block-react-parser/commit/89bfee00d3841df58e59e2e86731d9d50fb72d57))
37
- * **release:** 1.0.0-beta.5 [skip ci] ([e475522](https://github.com/frontkom/block-react-parser/commit/e4755228758e91590431add3406743eff619a2bf))
38
- * **release:** 1.0.0-beta.6 [skip ci] ([be22f32](https://github.com/frontkom/block-react-parser/commit/be22f32c5082071d91a919f1e2149857c7a27c67))
39
- * **release:** 1.0.0-beta.7 [skip ci] ([dcd4a9b](https://github.com/frontkom/block-react-parser/commit/dcd4a9b9722a45174e2abd2e10df58860305819a))
40
- * **release:** 1.0.0-beta.8 [skip ci] ([8f5d9e1](https://github.com/frontkom/block-react-parser/commit/8f5d9e187b11e8fc90ed58ac38ebd12282f3c952))
41
- * **release:** 1.0.0-beta.9 [skip ci] ([63b7c61](https://github.com/frontkom/block-react-parser/commit/63b7c616db3145a15c4d80d871df3ed8dff7b5ba))
@@ -1,6 +0,0 @@
1
- ## [1.1.0-beta.1](https://github.com/frontkom/block-react-parser/compare/v1.0.0...v1.1.0-beta.1) (2024-03-06)
2
-
3
-
4
- ### Features
5
-
6
- * add dist folder to .gitignore ([15422a5](https://github.com/frontkom/block-react-parser/commit/15422a58a7a9c39bc450607b3fd8c46eb10e6d0e))
@@ -1,6 +0,0 @@
1
- ## [1.1.0-beta.2](https://github.com/frontkom/block-react-parser/compare/v1.1.0-beta.1...v1.1.0-beta.2) (2024-03-13)
2
-
3
-
4
- ### Features
5
-
6
- * merge beta to develop ([1144e9e](https://github.com/frontkom/block-react-parser/commit/1144e9e79a7171c4524e4fdbaa0fa1cc90d77378))
@@ -1,11 +0,0 @@
1
- ## [1.1.0](https://github.com/frontkom/block-react-parser/compare/v1.0.0...v1.1.0) (2024-03-06)
2
-
3
-
4
- ### Features
5
-
6
- * add dist folder to .gitignore ([15422a5](https://github.com/frontkom/block-react-parser/commit/15422a58a7a9c39bc450607b3fd8c46eb10e6d0e))
7
-
8
-
9
- ### Miscellaneous Chores
10
-
11
- * **release:** 1.1.0-beta.1 [skip ci] ([44a44a7](https://github.com/frontkom/block-react-parser/commit/44a44a7d90f2253118239681916ae26ba604113b))
@@ -1,11 +0,0 @@
1
- ## [1.2.0-beta.1](https://github.com/frontkom/block-react-parser/compare/v1.1.0...v1.2.0-beta.1) (2024-03-13)
2
-
3
-
4
- ### Features
5
-
6
- * merge beta to develop ([1144e9e](https://github.com/frontkom/block-react-parser/commit/1144e9e79a7171c4524e4fdbaa0fa1cc90d77378))
7
-
8
-
9
- ### Miscellaneous Chores
10
-
11
- * **release:** 1.1.0-beta.2 [skip ci] ([f1df280](https://github.com/frontkom/block-react-parser/commit/f1df280d56c1d7c5ceb91c67b3dfbd15d109d575))
@@ -1,27 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _htmlparser = require("htmlparser2");
8
- var _attribsProps2 = _interopRequireDefault(require("./attribsProps"));
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- var htmlAttribs = function htmlAttribs(html) {
11
- var htmlFragment = html === null || html === void 0 ? void 0 : html.trim();
12
- if (htmlFragment && htmlFragment.length > 1) {
13
- var _parsedFragment$child;
14
- var parsedFragment = (0, _htmlparser.parseDocument)(htmlFragment, {
15
- lowerCaseTags: true,
16
- recognizeSelfClosing: true
17
- });
18
- var attribs = parsedFragment === null || parsedFragment === void 0 ? void 0 : (_parsedFragment$child = parsedFragment.children[0]) === null || _parsedFragment$child === void 0 ? void 0 : _parsedFragment$child.attribs;
19
- if (Object.keys(attribs).length !== 0) {
20
- var _attribsProps;
21
- return (_attribsProps = (0, _attribsProps2.default)(attribs)) !== null && _attribsProps !== void 0 ? _attribsProps : {};
22
- }
23
- }
24
- return {};
25
- };
26
- var _default = htmlAttribs;
27
- exports.default = _default;
@@ -1,5 +0,0 @@
1
- export default function Image({ attribs }) {
2
- const { alt = '', class: className = '', src, height, width } = attribs;
3
-
4
- return <img alt={alt} className={className} src={src} width={width} height={height} />;
5
- }
@@ -1,22 +0,0 @@
1
- import { parseDocument } from 'htmlparser2';
2
- import attribsProps from './attribsProps';
3
-
4
- const htmlAttribs = html => {
5
- const htmlFragment = html?.trim();
6
-
7
- if (htmlFragment && htmlFragment.length > 1) {
8
- const parsedFragment = parseDocument(htmlFragment, {
9
- lowerCaseTags: true,
10
- recognizeSelfClosing: true,
11
- });
12
- const attribs = parsedFragment?.children[0]?.attribs;
13
-
14
- if (Object.keys(attribs).length !== 0) {
15
- return attribsProps(attribs) ?? {};
16
- }
17
- }
18
-
19
- return {};
20
- };
21
-
22
- export default htmlAttribs;