@frontkom/block-react-parser 0.1.0 → 1.1.0-beta.2
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/.eslintrc.js +15 -0
- package/.nvmrc +1 -1
- package/.prettierignore +4 -0
- package/.prettierrc +8 -0
- package/changelogs/CHANGELOG-1.0.0.md +41 -0
- package/changelogs/CHANGELOG-1.1.0-beta.1.md +6 -0
- package/changelogs/CHANGELOG-1.1.0-beta.2.md +6 -0
- package/dist/components/Block.js +21 -15
- package/dist/components/Context.js +8 -10
- package/dist/components/Tree.js +40 -23
- package/dist/elements/index.js +25 -33
- package/dist/elements/tags/img.js +8 -10
- package/dist/elements/tags/selfClosing.js +4 -6
- package/dist/index.js +8 -8
- package/dist/utils/attribsProps.js +4 -3
- package/dist/utils/htmlAttribs.js +27 -0
- package/dist/utils/innerNode.js +6 -5
- package/dist/utils/parseBlocks.js +10 -5
- package/package.json +15 -4
- package/src/components/Block.jsx +11 -6
- package/src/components/Tree.jsx +13 -9
- package/src/elements/{index.js → index.jsx} +3 -3
- package/src/elements/tags/img.jsx +5 -0
- package/src/utils/attribsProps.js +3 -3
- package/src/utils/htmlAttribs.js +22 -0
- package/src/utils/innerNode.jsx +2 -2
- package/src/utils/parseBlocks.jsx +3 -3
- package/src/elements/tags/img.js +0 -13
- /package/src/elements/tags/{selfClosing.js → selfClosing.jsx} +0 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
v20.10.0
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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))
|
|
@@ -0,0 +1,6 @@
|
|
|
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))
|
|
@@ -0,0 +1,6 @@
|
|
|
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))
|
package/dist/components/Block.js
CHANGED
|
@@ -4,10 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = Block;
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
7
8
|
var _Tree = _interopRequireDefault(require("./Tree"));
|
|
8
9
|
var _innerNode = _interopRequireDefault(require("../utils/innerNode"));
|
|
9
10
|
var _Context = require("./Context");
|
|
11
|
+
var _htmlAttribs = _interopRequireDefault(require("../utils/htmlAttribs"));
|
|
10
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; }
|
|
11
16
|
/**
|
|
12
17
|
* Block element.
|
|
13
18
|
*
|
|
@@ -15,18 +20,19 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
15
20
|
* @returns {JSX.Element | null | undefined}
|
|
16
21
|
*/
|
|
17
22
|
function Block(_ref) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
+
});
|
|
27
33
|
if (CustomBlock) {
|
|
28
|
-
return /*#__PURE__*/
|
|
29
|
-
block:
|
|
34
|
+
return /*#__PURE__*/_react.default.createElement(CustomBlock, {
|
|
35
|
+
block: blockExtended
|
|
30
36
|
});
|
|
31
37
|
}
|
|
32
38
|
|
|
@@ -35,14 +41,14 @@ function Block(_ref) {
|
|
|
35
41
|
return null;
|
|
36
42
|
}
|
|
37
43
|
// Filter out empty lines and orphaned closing tags.
|
|
38
|
-
if (innerContent.length === 1 && (innerContent[0] ===
|
|
44
|
+
if (innerContent.length === 1 && (innerContent[0] === '\n' || innerContent[0].substring(0, 2) === '</')) {
|
|
39
45
|
return null;
|
|
40
46
|
}
|
|
41
|
-
|
|
47
|
+
var node = (0, _innerNode.default)(innerBlocks, innerContent);
|
|
42
48
|
if (node) {
|
|
43
|
-
return /*#__PURE__*/
|
|
49
|
+
return /*#__PURE__*/_react.default.createElement(_Tree.default, {
|
|
44
50
|
node: node,
|
|
45
|
-
block:
|
|
51
|
+
block: blockExtended
|
|
46
52
|
});
|
|
47
53
|
}
|
|
48
54
|
return null;
|
|
@@ -9,23 +9,21 @@ exports.useComponentContext = useComponentContext;
|
|
|
9
9
|
exports.useTagComponent = useTagComponent;
|
|
10
10
|
var _react = require("react");
|
|
11
11
|
var _elements = require("../elements");
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Provider
|
|
15
|
-
} = TxContext;
|
|
12
|
+
var TxContext = /*#__PURE__*/(0, _react.createContext)();
|
|
13
|
+
var Provider = TxContext.Provider;
|
|
16
14
|
exports.Provider = Provider;
|
|
17
15
|
function useComponentContext() {
|
|
18
16
|
return (0, _react.useContext)(TxContext);
|
|
19
17
|
}
|
|
20
18
|
function useBlockComponent(name) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
var _useContext = (0, _react.useContext)(TxContext),
|
|
20
|
+
_useContext$CustomBlo = _useContext.CustomBlocks,
|
|
21
|
+
CustomBlocks = _useContext$CustomBlo === void 0 ? _elements.coreBlocks : _useContext$CustomBlo;
|
|
24
22
|
return name && CustomBlocks[name];
|
|
25
23
|
}
|
|
26
24
|
function useTagComponent(tag) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
var _useContext2 = (0, _react.useContext)(TxContext),
|
|
26
|
+
_useContext2$CustomTa = _useContext2.CustomTags,
|
|
27
|
+
CustomTags = _useContext2$CustomTa === void 0 ? _elements.coreTags : _useContext2$CustomTa;
|
|
30
28
|
return tag && CustomTags[tag];
|
|
31
29
|
}
|
package/dist/components/Tree.js
CHANGED
|
@@ -4,44 +4,61 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = Tree;
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
7
8
|
var _Context = require("./Context");
|
|
8
9
|
var _Block = _interopRequireDefault(require("./Block"));
|
|
9
10
|
var _attribsProps = _interopRequireDefault(require("../utils/attribsProps"));
|
|
10
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
12
|
function Tree(_ref) {
|
|
12
|
-
var _node$
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} = _ref;
|
|
17
|
-
const CustomTag = (0, _Context.useTagComponent)(node.name);
|
|
13
|
+
var _node$children2;
|
|
14
|
+
var node = _ref.node,
|
|
15
|
+
block = _ref.block;
|
|
16
|
+
var CustomTag = (0, _Context.useTagComponent)(node.name);
|
|
18
17
|
(0, _attribsProps.default)(node.attribs);
|
|
19
|
-
if (node.type ===
|
|
20
|
-
if (node.data ===
|
|
18
|
+
if (node.type === 'text') {
|
|
19
|
+
if (node.data === '[innerBlocks]') {
|
|
21
20
|
var _block$innerBlocks;
|
|
22
21
|
// eslint-disable-next-line react/no-array-index-key
|
|
23
|
-
return (_block$innerBlocks = block.innerBlocks) === null || _block$innerBlocks === void 0 ? void 0 : _block$innerBlocks.map((inner, index)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
+
});
|
|
27
28
|
}
|
|
28
29
|
return node.data;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
// Handle selfclosed elements (???)
|
|
32
33
|
if (CustomTag) {
|
|
33
|
-
|
|
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,
|
|
34
48
|
attribs: node.attribs
|
|
35
49
|
});
|
|
36
50
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return /*#__PURE__*/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
}));
|
|
47
64
|
}
|
package/dist/elements/index.js
CHANGED
|
@@ -4,40 +4,36 @@ 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"));
|
|
7
8
|
var _img = _interopRequireDefault(require("./tags/img"));
|
|
8
9
|
var _selfClosing = _interopRequireDefault(require("./tags/selfClosing"));
|
|
9
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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, {
|
|
16
18
|
attribs: attribs
|
|
17
19
|
});
|
|
18
20
|
},
|
|
19
|
-
br: _ref2
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
} = _ref2;
|
|
23
|
-
return /*#__PURE__*/React.createElement(_selfClosing.default, {
|
|
21
|
+
br: function br(_ref2) {
|
|
22
|
+
var attribs = _ref2.attribs;
|
|
23
|
+
return /*#__PURE__*/_react.default.createElement(_selfClosing.default, {
|
|
24
24
|
attribs: attribs,
|
|
25
25
|
tag: "br"
|
|
26
26
|
});
|
|
27
27
|
},
|
|
28
|
-
hr: _ref3
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} = _ref3;
|
|
32
|
-
return /*#__PURE__*/React.createElement(_selfClosing.default, {
|
|
28
|
+
hr: function hr(_ref3) {
|
|
29
|
+
var attribs = _ref3.attribs;
|
|
30
|
+
return /*#__PURE__*/_react.default.createElement(_selfClosing.default, {
|
|
33
31
|
attribs: attribs,
|
|
34
32
|
tag: "hr"
|
|
35
33
|
});
|
|
36
34
|
},
|
|
37
|
-
meta: _ref4
|
|
38
|
-
|
|
39
|
-
attribs
|
|
40
|
-
} = _ref4;
|
|
35
|
+
meta: function meta(_ref4) {
|
|
36
|
+
var attribs = _ref4.attribs;
|
|
41
37
|
return null;
|
|
42
38
|
}
|
|
43
39
|
// area: ({attribs}) => <SelfClosing attribs={attribs} tag="area" />,
|
|
@@ -52,7 +48,8 @@ const coreTags = exports.coreTags = {
|
|
|
52
48
|
// track: ({attribs}) => <SelfClosing attribs={attribs} tag="track" />,
|
|
53
49
|
// wbr: ({attribs}) => <SelfClosing attribs={attribs} tag="wbr" />,
|
|
54
50
|
};
|
|
55
|
-
|
|
51
|
+
exports.coreTags = coreTags;
|
|
52
|
+
var coreBlocks = {
|
|
56
53
|
// 'core/archives': ({ block }) => doSomething(),
|
|
57
54
|
// 'core/audio': ({ block }) => doSomething(),
|
|
58
55
|
// 'core/avatar': ({ block }) => doSomething(),
|
|
@@ -154,12 +151,10 @@ const coreBlocks = exports.coreBlocks = {
|
|
|
154
151
|
* @param {object} tags - Optional object with custom blocks definitions. Empty by default.
|
|
155
152
|
* @returns {object} Object with blocks definitions
|
|
156
153
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
...tags
|
|
162
|
-
};
|
|
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);
|
|
163
158
|
};
|
|
164
159
|
|
|
165
160
|
/**
|
|
@@ -170,11 +165,8 @@ const customTags = function () {
|
|
|
170
165
|
* @returns {object} Object with blocks definitions
|
|
171
166
|
*/
|
|
172
167
|
exports.customTags = customTags;
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return {
|
|
176
|
-
...coreBlocks,
|
|
177
|
-
...blocks
|
|
178
|
-
};
|
|
168
|
+
var customBlocks = function customBlocks() {
|
|
169
|
+
var blocks = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
170
|
+
return _objectSpread(_objectSpread({}, coreBlocks), blocks);
|
|
179
171
|
};
|
|
180
172
|
exports.customBlocks = customBlocks;
|
|
@@ -5,16 +5,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = Image;
|
|
7
7
|
function Image(_ref) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
width
|
|
17
|
-
} = attribs;
|
|
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;
|
|
18
16
|
return /*#__PURE__*/React.createElement("img", {
|
|
19
17
|
alt: alt,
|
|
20
18
|
className: className,
|
|
@@ -7,11 +7,9 @@ exports.default = SelfClosing;
|
|
|
7
7
|
var _attribsProps = _interopRequireDefault(require("../../utils/attribsProps"));
|
|
8
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
9
|
function SelfClosing(_ref) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const Component = tag;
|
|
15
|
-
const attributes = (0, _attribsProps.default)(attribs);
|
|
10
|
+
var attribs = _ref.attribs,
|
|
11
|
+
tag = _ref.tag;
|
|
12
|
+
var Component = tag;
|
|
13
|
+
var attributes = (0, _attribsProps.default)(attribs);
|
|
16
14
|
return /*#__PURE__*/React.createElement(Component, attributes);
|
|
17
15
|
}
|
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 () {
|
|
8
|
+
get: function get() {
|
|
9
9
|
return _Block.default;
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "Provider", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () {
|
|
14
|
+
get: function get() {
|
|
15
15
|
return _Context.Provider;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
Object.defineProperty(exports, "Tree", {
|
|
19
19
|
enumerable: true,
|
|
20
|
-
get: function () {
|
|
20
|
+
get: function get() {
|
|
21
21
|
return _Tree.default;
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "attribsProps", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () {
|
|
26
|
+
get: function get() {
|
|
27
27
|
return _attribsProps.default;
|
|
28
28
|
}
|
|
29
29
|
});
|
|
30
30
|
Object.defineProperty(exports, "customBlocks", {
|
|
31
31
|
enumerable: true,
|
|
32
|
-
get: function () {
|
|
32
|
+
get: function get() {
|
|
33
33
|
return _elements.customBlocks;
|
|
34
34
|
}
|
|
35
35
|
});
|
|
36
36
|
Object.defineProperty(exports, "customTags", {
|
|
37
37
|
enumerable: true,
|
|
38
|
-
get: function () {
|
|
38
|
+
get: function get() {
|
|
39
39
|
return _elements.customTags;
|
|
40
40
|
}
|
|
41
41
|
});
|
|
42
42
|
Object.defineProperty(exports, "innerNode", {
|
|
43
43
|
enumerable: true,
|
|
44
|
-
get: function () {
|
|
44
|
+
get: function get() {
|
|
45
45
|
return _innerNode.default;
|
|
46
46
|
}
|
|
47
47
|
});
|
|
48
48
|
Object.defineProperty(exports, "parseBlocks", {
|
|
49
49
|
enumerable: true,
|
|
50
|
-
get: function () {
|
|
50
|
+
get: function get() {
|
|
51
51
|
return _parseBlocks.default;
|
|
52
52
|
}
|
|
53
53
|
});
|
|
@@ -7,11 +7,11 @@ exports.default = void 0;
|
|
|
7
7
|
var _reactAttrConverter = _interopRequireDefault(require("react-attr-converter"));
|
|
8
8
|
var _styleToJs = _interopRequireDefault(require("style-to-js"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
|
|
10
|
+
var attribsProps = function attribsProps(attribs) {
|
|
11
11
|
if (attribs === undefined) {
|
|
12
12
|
return {};
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
var props = Object.fromEntries(Object.entries(attribs).map(function (attribute) {
|
|
15
15
|
if (attribute[0] === 'style') {
|
|
16
16
|
return [(0, _reactAttrConverter.default)(attribute[0]), (0, _styleToJs.default)(attribute[1])];
|
|
17
17
|
}
|
|
@@ -19,4 +19,5 @@ const attribsProps = attribs => {
|
|
|
19
19
|
}));
|
|
20
20
|
return props;
|
|
21
21
|
};
|
|
22
|
-
var _default =
|
|
22
|
+
var _default = attribsProps;
|
|
23
|
+
exports.default = _default;
|
|
@@ -0,0 +1,27 @@
|
|
|
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;
|
package/dist/utils/innerNode.js
CHANGED
|
@@ -5,16 +5,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var _htmlparser = require("htmlparser2");
|
|
8
|
-
|
|
8
|
+
var innerNode = function innerNode(innerBlocks, innerContent) {
|
|
9
9
|
var _tree$children$;
|
|
10
10
|
// If no inner blocks, return the block markup.
|
|
11
11
|
// If inner blocks, return the wrapping markup.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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, {
|
|
15
15
|
lowerCaseTags: true,
|
|
16
16
|
recognizeSelfClosing: true
|
|
17
17
|
});
|
|
18
18
|
return (_tree$children$ = tree.children[0]) !== null && _tree$children$ !== void 0 ? _tree$children$ : null;
|
|
19
19
|
};
|
|
20
|
-
var _default =
|
|
20
|
+
var _default = innerNode;
|
|
21
|
+
exports.default = _default;
|
|
@@ -13,8 +13,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
13
13
|
* @param {string} html - markup rendered by Gutenberg editor.
|
|
14
14
|
* @returns {JSX.Element[]}
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontkom/block-react-parser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0-beta.2",
|
|
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
|
-
"build": "rm -rf dist && NODE_ENV=production babel src --out-dir dist --copy-files"
|
|
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
11
|
},
|
|
10
12
|
"keywords": [
|
|
11
13
|
"html",
|
|
@@ -26,12 +28,21 @@
|
|
|
26
28
|
"@babel/core": "^7.20.2",
|
|
27
29
|
"@babel/preset-env": "^7.20.2",
|
|
28
30
|
"@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",
|
|
29
40
|
"react": "^17.0.2",
|
|
30
41
|
"react-dom": "^17.0.2"
|
|
31
42
|
},
|
|
32
43
|
"peerDependencies": {
|
|
33
|
-
"react": "^17.0.2",
|
|
34
|
-
"react-dom": "^17.0.2"
|
|
44
|
+
"react": "^17.0.2 || ^18.0.0",
|
|
45
|
+
"react-dom": "^17.0.2 || ^18.0.0"
|
|
35
46
|
},
|
|
36
47
|
"babel": {
|
|
37
48
|
"presets": [
|
package/src/components/Block.jsx
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
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';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Block element.
|
|
@@ -12,8 +14,11 @@ export default function Block({ block }) {
|
|
|
12
14
|
const { blockName = null, innerContent, innerBlocks } = block;
|
|
13
15
|
const CustomBlock = useBlockComponent(blockName);
|
|
14
16
|
|
|
17
|
+
const htmlAttrs = htmlAttribs(innerContent[0]);
|
|
18
|
+
const blockExtended = { ...block, htmlAttrs };
|
|
19
|
+
|
|
15
20
|
if (CustomBlock) {
|
|
16
|
-
return <CustomBlock block={
|
|
21
|
+
return <CustomBlock block={blockExtended} />;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
// Filter out empty blocks.
|
|
@@ -23,14 +28,14 @@ export default function Block({ block }) {
|
|
|
23
28
|
// Filter out empty lines and orphaned closing tags.
|
|
24
29
|
if (
|
|
25
30
|
innerContent.length === 1 &&
|
|
26
|
-
(innerContent[0] ===
|
|
31
|
+
(innerContent[0] === '\n' || innerContent[0].substring(0, 2) === '</')
|
|
27
32
|
) {
|
|
28
33
|
return null;
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
const node = innerNode(innerBlocks, innerContent);
|
|
32
37
|
if (node) {
|
|
33
|
-
return <Tree node={node} block={
|
|
38
|
+
return <Tree node={node} block={blockExtended} />;
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
return null;
|
package/src/components/Tree.jsx
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useTagComponent } from './Context';
|
|
3
|
+
import Block from './Block';
|
|
4
|
+
import attribsProps from '../utils/attribsProps';
|
|
4
5
|
|
|
5
6
|
export default function Tree({ node, block }) {
|
|
6
7
|
const CustomTag = useTagComponent(node.name);
|
|
7
8
|
|
|
8
9
|
attribsProps(node.attribs);
|
|
9
10
|
|
|
10
|
-
if (node.type ===
|
|
11
|
-
if (node.data ===
|
|
11
|
+
if (node.type === 'text') {
|
|
12
|
+
if (node.data === '[innerBlocks]') {
|
|
12
13
|
// eslint-disable-next-line react/no-array-index-key
|
|
13
|
-
return block.innerBlocks?.map((inner, index) =>
|
|
14
|
-
<Block block={inner} key={index} />
|
|
15
|
-
));
|
|
14
|
+
return block.innerBlocks?.map((inner, index) => <Block block={inner} key={index} />);
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
return node.data;
|
|
@@ -20,7 +19,12 @@ export default function Tree({ node, block }) {
|
|
|
20
19
|
|
|
21
20
|
// Handle selfclosed elements (???)
|
|
22
21
|
if (CustomTag) {
|
|
23
|
-
|
|
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} />;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
const Component = node.name;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
1
3
|
import Image from './tags/img';
|
|
2
4
|
import SelfClosing from './tags/selfClosing';
|
|
3
5
|
|
|
@@ -5,9 +7,7 @@ export const coreTags = {
|
|
|
5
7
|
img: ({ attribs }) => <Image attribs={attribs} />,
|
|
6
8
|
br: ({ attribs }) => <SelfClosing attribs={attribs} tag="br" />,
|
|
7
9
|
hr: ({ attribs }) => <SelfClosing attribs={attribs} tag="hr" />,
|
|
8
|
-
meta: ({ attribs }) =>
|
|
9
|
-
return null;
|
|
10
|
-
},
|
|
10
|
+
meta: ({ attribs }) => null,
|
|
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" />,
|
|
@@ -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 =
|
|
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(
|
|
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;
|
|
@@ -0,0 +1,22 @@
|
|
|
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;
|
package/src/utils/innerNode.jsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { parseDocument } from
|
|
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
|
|
2
|
-
import Block from
|
|
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 =
|
|
10
|
+
const parseBlocks = html =>
|
|
11
11
|
parse(html.trim()).map((block, key) => <Block block={block} key={key} />);
|
|
12
12
|
|
|
13
13
|
export default parseBlocks;
|
package/src/elements/tags/img.js
DELETED
|
File without changes
|