@20minutes/draft-convert 3.1.1 → 3.1.3

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.
Files changed (42) hide show
  1. package/{lib → dist}/blockEntities.js +6 -6
  2. package/{esm/blockEntities.js → dist/blockEntities.mjs} +8 -8
  3. package/{lib → dist}/blockInlineStyles.js +6 -6
  4. package/{esm/blockInlineStyles.js → dist/blockInlineStyles.mjs} +6 -6
  5. package/{lib → dist}/convertFromHTML.js +24 -66
  6. package/{esm/convertFromHTML.js → dist/convertFromHTML.mjs} +25 -26
  7. package/{lib → dist}/convertToHTML.js +10 -51
  8. package/{esm/convertToHTML.js → dist/convertToHTML.mjs} +14 -14
  9. package/{esm/encodeBlock.js → dist/encodeBlock.mjs} +2 -2
  10. package/dist/index.d.ts +127 -0
  11. package/dist/index.mjs +4 -0
  12. package/{lib → dist}/util/getBlockTags.js +4 -4
  13. package/{esm/util/getBlockTags.js → dist/util/getBlockTags.mjs} +4 -4
  14. package/{lib → dist}/util/getElementHTML.js +2 -2
  15. package/{esm/util/getElementHTML.js → dist/util/getElementHTML.mjs} +3 -3
  16. package/{esm/util/getElementTagLength.js → dist/util/getElementTagLength.mjs} +1 -1
  17. package/{lib → dist}/util/getNestedBlockTags.js +3 -3
  18. package/{esm/util/getNestedBlockTags.js → dist/util/getNestedBlockTags.mjs} +3 -3
  19. package/{lib → dist}/util/splitReactElement.js +3 -3
  20. package/{esm/util/splitReactElement.js → dist/util/splitReactElement.mjs} +1 -1
  21. package/package.json +21 -25
  22. package/esm/index.js +0 -4
  23. package/types/index.d.ts +0 -114
  24. package/{lib → dist}/default/defaultBlockHTML.js +0 -0
  25. package/{esm/default/defaultBlockHTML.js → dist/default/defaultBlockHTML.mjs} +0 -0
  26. package/{lib → dist}/default/defaultInlineHTML.js +0 -0
  27. package/{esm/default/defaultInlineHTML.js → dist/default/defaultInlineHTML.mjs} +0 -0
  28. package/{lib → dist}/encodeBlock.js +1 -1
  29. package/{lib/index.js → dist/index.cjs} +1 -1
  30. /package/{lib → dist}/util/accumulateFunction.js +0 -0
  31. /package/{esm/util/accumulateFunction.js → dist/util/accumulateFunction.mjs} +0 -0
  32. /package/{lib → dist}/util/blockTypeObjectFunction.js +0 -0
  33. /package/{esm/util/blockTypeObjectFunction.js → dist/util/blockTypeObjectFunction.mjs} +0 -0
  34. /package/{lib → dist}/util/getElementTagLength.js +0 -0
  35. /package/{lib → dist}/util/parseHTML.js +0 -0
  36. /package/{esm/util/parseHTML.js → dist/util/parseHTML.mjs} +0 -0
  37. /package/{lib → dist}/util/rangeSort.js +0 -0
  38. /package/{esm/util/rangeSort.js → dist/util/rangeSort.mjs} +0 -0
  39. /package/{lib → dist}/util/styleObjectFunction.js +0 -0
  40. /package/{esm/util/styleObjectFunction.js → dist/util/styleObjectFunction.mjs} +0 -0
  41. /package/{lib → dist}/util/updateMutation.js +0 -0
  42. /package/{esm/util/updateMutation.js → dist/util/updateMutation.mjs} +0 -0
@@ -1,18 +1,18 @@
1
- import invariant from 'invariant';
1
+ import DraftJS from 'draft-js';
2
2
  import React from 'react';
3
3
  import ReactDOMServer from 'react-dom/server';
4
- import * as DraftJS from 'draft-js';
5
- import encodeBlock from './encodeBlock.js';
6
- import blockEntities from './blockEntities.js';
7
- import blockInlineStyles from './blockInlineStyles.js';
8
- import accumulateFunction from './util/accumulateFunction.js';
9
- import blockTypeObjectFunction from './util/blockTypeObjectFunction.js';
10
- import getBlockTags from './util/getBlockTags.js';
11
- import getNestedBlockTags from './util/getNestedBlockTags.js';
12
- import defaultBlockHTML from './default/defaultBlockHTML.js';
4
+ import invariant from 'tiny-invariant';
5
+ import blockEntities from './blockEntities.mjs';
6
+ import blockInlineStyles from './blockInlineStyles.mjs';
7
+ import defaultBlockHTML from './default/defaultBlockHTML.mjs';
8
+ import encodeBlock from './encodeBlock.mjs';
9
+ import accumulateFunction from './util/accumulateFunction.mjs';
10
+ import blockTypeObjectFunction from './util/blockTypeObjectFunction.mjs';
11
+ import getBlockTags from './util/getBlockTags.mjs';
12
+ import getNestedBlockTags from './util/getNestedBlockTags.mjs';
13
13
  const { convertToRaw } = DraftJS;
14
- const defaultEntityToHTML = (entity, originalText)=>originalText;
15
- const defaultValidateHTML = (html)=>true;
14
+ const defaultEntityToHTML = (_entity, originalText)=>originalText;
15
+ const defaultValidateHTML = (_html)=>true;
16
16
  const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defaultEntityToHTML, validateHTML = defaultValidateHTML })=>(contentState)=>{
17
17
  invariant(contentState !== null && contentState !== undefined, 'Expected contentState to be non-null');
18
18
  let getBlockHTML;
@@ -61,7 +61,7 @@ const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defa
61
61
  } else {
62
62
  html = blockHTML.start + innerHTML + blockHTML.end;
63
63
  }
64
- if (innerHTML.length === 0 && Object.prototype.hasOwnProperty.call(blockHTML, 'empty')) {
64
+ if (innerHTML.length === 0 && Object.hasOwn(blockHTML, 'empty')) {
65
65
  if (/*#__PURE__*/ React.isValidElement(blockHTML.empty)) {
66
66
  html = ReactDOMServer.renderToStaticMarkup(blockHTML.empty);
67
67
  } else {
@@ -78,7 +78,7 @@ const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defa
78
78
  return result;
79
79
  };
80
80
  export default ((...args)=>{
81
- if (args.length === 1 && Object.prototype.hasOwnProperty.call(args[0], '_map') && args[0].getBlockMap != null) {
81
+ if (args.length === 1 && Object.hasOwn(args[0], '_map') && args[0].getBlockMap != null) {
82
82
  // skip higher-order function and use defaults
83
83
  return convertToHTML({})(...args);
84
84
  }
@@ -1,5 +1,5 @@
1
- import updateMutation from './util/updateMutation.js';
2
- import rangeSort from './util/rangeSort.js';
1
+ import rangeSort from './util/rangeSort.mjs';
2
+ import updateMutation from './util/updateMutation.mjs';
3
3
  const ENTITY_MAP = {
4
4
  '&': '&',
5
5
  '<': '&lt;',
@@ -0,0 +1,127 @@
1
+ // Based on: https://github.com/HubSpot/draft-convert/issues/107#issuecomment-488581709 by <https://github.com/sbusch>
2
+ // Grabbed from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/draft-convert/index.d.ts
3
+
4
+ import {
5
+ ContentState,
6
+ DraftBlockType,
7
+ DraftEntityMutability,
8
+ DraftInlineStyleType,
9
+ Entity,
10
+ RawDraftContentBlock,
11
+ RawDraftEntity,
12
+ } from 'draft-js'
13
+ import { ReactNode } from 'react'
14
+
15
+ export type RawDraftContentBlockWithCustomType<T> = Omit<RawDraftContentBlock, 'type'> & {
16
+ type: T
17
+ }
18
+
19
+ export type ExtendedHTMLElement<T> = (HTMLElement | HTMLLinkElement) & T
20
+
21
+ export type EntityKey = string
22
+
23
+ export type ContentStateConverter = (contentState: ContentState) => string
24
+ export type HTMLConverter = (html: string) => ContentState
25
+
26
+ export type Tag =
27
+ | ReactNode
28
+ | { start: string; end: string; empty?: string | undefined }
29
+ | {
30
+ element: ReactNode
31
+ empty?: ReactNode | undefined
32
+ }
33
+
34
+ export interface IConvertToHTMLConfig<
35
+ S = DraftInlineStyleType,
36
+ B extends DraftBlockType = DraftBlockType,
37
+ E extends RawDraftEntity = RawDraftEntity,
38
+ > {
39
+ // Inline styles:
40
+ styleToHTML?: ((style: S) => Tag | null | undefined | undefined) | undefined
41
+
42
+ // Block styles:
43
+ blockToHTML?:
44
+ | ((block: RawDraftContentBlockWithCustomType<B>) => Tag | null | undefined)
45
+ | undefined
46
+
47
+ // Entity styling:
48
+ entityToHTML?: ((entity: E, originalText: string) => Tag | null | undefined) | undefined
49
+
50
+ // HTML validation:
51
+ validateHTML?: ((html: string) => boolean) | undefined
52
+ }
53
+
54
+ export interface IConvertFromHTMLConfig<
55
+ S extends {
56
+ [name: string]: unknown
57
+ } = DOMStringMap,
58
+ B extends DraftBlockType = DraftBlockType,
59
+ E extends RawDraftEntity = RawDraftEntity,
60
+ > {
61
+ // Inline styles:
62
+ htmlToStyle?:
63
+ | ((nodeName: string, node: ExtendedHTMLElement<S>, currentStyle: Set<string>) => Set<string>)
64
+ | undefined
65
+
66
+ // Block styles:
67
+ htmlToBlock?:
68
+ | ((
69
+ nodeName: string,
70
+ node: ExtendedHTMLElement<S>
71
+ ) => B | { type: B; data: object } | false | undefined)
72
+ | undefined
73
+
74
+ // Html entities
75
+ htmlToEntity?:
76
+ | ((
77
+ nodeName: string,
78
+ node: ExtendedHTMLElement<S>,
79
+ createEntity: (
80
+ type: E['type'],
81
+ mutability: DraftEntityMutability,
82
+ data: E['data']
83
+ ) => EntityKey,
84
+ getEntity: (key: EntityKey) => Entity,
85
+ mergeEntityData: (key: string, data: object) => void,
86
+ replaceEntityData: (key: string, data: object) => void
87
+ ) => EntityKey | undefined)
88
+ | undefined
89
+
90
+ // Text entities
91
+ textToEntity?:
92
+ | ((
93
+ text: string,
94
+ createEntity: (
95
+ type: E['type'],
96
+ mutability: DraftEntityMutability,
97
+ data: E['data']
98
+ ) => EntityKey,
99
+ getEntity: (key: EntityKey) => Entity,
100
+ mergeEntityData: (key: string, data: object) => void,
101
+ replaceEntityData: (key: string, data: object) => void
102
+ ) => Array<{
103
+ entity: EntityKey
104
+ offset: number
105
+ length: number
106
+ result?: string | undefined
107
+ }>)
108
+ | undefined
109
+ }
110
+
111
+ export function convertToHTML<
112
+ S = DraftInlineStyleType,
113
+ B extends DraftBlockType = DraftBlockType,
114
+ E extends RawDraftEntity = RawDraftEntity,
115
+ >(config: IConvertToHTMLConfig<S, B, E>): ContentStateConverter
116
+ export function convertToHTML(contentState: ContentState): string
117
+
118
+ export function convertFromHTML<
119
+ S extends {
120
+ [name: string]: unknown
121
+ } = DOMStringMap,
122
+ B extends DraftBlockType = DraftBlockType,
123
+ E extends RawDraftEntity = RawDraftEntity,
124
+ >(config: IConvertFromHTMLConfig<S, B, E>): HTMLConverter
125
+ export function convertFromHTML(html: string): ContentState
126
+
127
+ export function parseHTML(html: string): HTMLBodyElement
package/dist/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import convertFromHTML from './convertFromHTML.mjs';
2
+ import convertToHTML from './convertToHTML.mjs';
3
+ import parseHTML from './util/parseHTML.mjs';
4
+ export { convertFromHTML, convertToHTML, parseHTML };
@@ -8,9 +8,9 @@ Object.defineProperty(exports, "default", {
8
8
  return getBlockTags;
9
9
  }
10
10
  });
11
- const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
12
11
  const _react = /*#__PURE__*/ _interop_require_default(require("react"));
13
12
  const _server = /*#__PURE__*/ _interop_require_default(require("react-dom/server"));
13
+ const _tinyinvariant = /*#__PURE__*/ _interop_require_default(require("tiny-invariant"));
14
14
  const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement.js"));
15
15
  function _interop_require_default(obj) {
16
16
  return obj && obj.__esModule ? obj : {
@@ -21,7 +21,7 @@ function hasChildren(element) {
21
21
  return /*#__PURE__*/ _react.default.isValidElement(element) && _react.default.Children.count(element.props.children) > 0;
22
22
  }
23
23
  function getBlockTags(blockHTML) {
24
- (0, _invariant.default)(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
24
+ (0, _tinyinvariant.default)(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
25
25
  if (typeof blockHTML === 'string') {
26
26
  return blockHTML;
27
27
  }
@@ -31,12 +31,12 @@ function getBlockTags(blockHTML) {
31
31
  }
32
32
  return (0, _splitReactElement.default)(blockHTML);
33
33
  }
34
- if (Object.prototype.hasOwnProperty.call(blockHTML, 'element') && /*#__PURE__*/ _react.default.isValidElement(blockHTML.element)) {
34
+ if (Object.hasOwn(blockHTML, 'element') && /*#__PURE__*/ _react.default.isValidElement(blockHTML.element)) {
35
35
  return {
36
36
  ...blockHTML,
37
37
  ...(0, _splitReactElement.default)(blockHTML.element)
38
38
  };
39
39
  }
40
- (0, _invariant.default)(Object.prototype.hasOwnProperty.call(blockHTML, 'start') && Object.prototype.hasOwnProperty.call(blockHTML, 'end'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
40
+ (0, _tinyinvariant.default)(Object.hasOwn(blockHTML, 'start') && Object.hasOwn(blockHTML, 'end'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
41
41
  return blockHTML;
42
42
  }
@@ -1,7 +1,7 @@
1
- import invariant from 'invariant';
2
1
  import React from 'react';
3
2
  import ReactDOMServer from 'react-dom/server';
4
- import splitReactElement from './splitReactElement.js';
3
+ import invariant from 'tiny-invariant';
4
+ import splitReactElement from './splitReactElement.mjs';
5
5
  function hasChildren(element) {
6
6
  return /*#__PURE__*/ React.isValidElement(element) && React.Children.count(element.props.children) > 0;
7
7
  }
@@ -16,12 +16,12 @@ export default function getBlockTags(blockHTML) {
16
16
  }
17
17
  return splitReactElement(blockHTML);
18
18
  }
19
- if (Object.prototype.hasOwnProperty.call(blockHTML, 'element') && /*#__PURE__*/ React.isValidElement(blockHTML.element)) {
19
+ if (Object.hasOwn(blockHTML, 'element') && /*#__PURE__*/ React.isValidElement(blockHTML.element)) {
20
20
  return {
21
21
  ...blockHTML,
22
22
  ...splitReactElement(blockHTML.element)
23
23
  };
24
24
  }
25
- invariant(Object.prototype.hasOwnProperty.call(blockHTML, 'start') && Object.prototype.hasOwnProperty.call(blockHTML, 'end'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
25
+ invariant(Object.hasOwn(blockHTML, 'start') && Object.hasOwn(blockHTML, 'end'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
26
26
  return blockHTML;
27
27
  }
@@ -8,9 +8,9 @@ Object.defineProperty(exports, "default", {
8
8
  return getElementHTML;
9
9
  }
10
10
  });
11
- const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
12
11
  const _react = /*#__PURE__*/ _interop_require_default(require("react"));
13
12
  const _server = /*#__PURE__*/ _interop_require_default(require("react-dom/server"));
13
+ const _tinyinvariant = /*#__PURE__*/ _interop_require_default(require("tiny-invariant"));
14
14
  const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement.js"));
15
15
  function _interop_require_default(obj) {
16
16
  return obj && obj.__esModule ? obj : {
@@ -38,7 +38,7 @@ function getElementHTML(element, text = null) {
38
38
  }
39
39
  return tags;
40
40
  }
41
- (0, _invariant.default)(Object.prototype.hasOwnProperty.call(element, 'start') && Object.prototype.hasOwnProperty.call(element, 'end'), 'convertToHTML: received conversion data without either an HTML string, ReactElement or an object with start/end tags');
41
+ (0, _tinyinvariant.default)(Object.hasOwn(element, 'start') && Object.hasOwn(element, 'end'), 'convertToHTML: received conversion data without either an HTML string, ReactElement or an object with start/end tags');
42
42
  if (text !== null) {
43
43
  const { start, end } = element;
44
44
  return start + text + end;
@@ -1,7 +1,7 @@
1
- import invariant from 'invariant';
2
1
  import React from 'react';
3
2
  import ReactDOMServer from 'react-dom/server';
4
- import splitReactElement from './splitReactElement.js';
3
+ import invariant from 'tiny-invariant';
4
+ import splitReactElement from './splitReactElement.mjs';
5
5
  function hasChildren(element) {
6
6
  return /*#__PURE__*/ React.isValidElement(element) && React.Children.count(element.props.children) > 0;
7
7
  }
@@ -23,7 +23,7 @@ export default function getElementHTML(element, text = null) {
23
23
  }
24
24
  return tags;
25
25
  }
26
- invariant(Object.prototype.hasOwnProperty.call(element, 'start') && Object.prototype.hasOwnProperty.call(element, 'end'), 'convertToHTML: received conversion data without either an HTML string, ReactElement or an object with start/end tags');
26
+ invariant(Object.hasOwn(element, 'start') && Object.hasOwn(element, 'end'), 'convertToHTML: received conversion data without either an HTML string, ReactElement or an object with start/end tags');
27
27
  if (text !== null) {
28
28
  const { start, end } = element;
29
29
  return start + text + end;
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import splitReactElement from './splitReactElement.js';
2
+ import splitReactElement from './splitReactElement.mjs';
3
3
  const getElementTagLength = (element, type = 'start')=>{
4
4
  if (/*#__PURE__*/ React.isValidElement(element)) {
5
5
  const splitElement = splitReactElement(element);
@@ -8,8 +8,8 @@ Object.defineProperty(exports, "default", {
8
8
  return getNestedBlockTags;
9
9
  }
10
10
  });
11
- const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
12
11
  const _react = /*#__PURE__*/ _interop_require_default(require("react"));
12
+ const _tinyinvariant = /*#__PURE__*/ _interop_require_default(require("tiny-invariant"));
13
13
  const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement.js"));
14
14
  function _interop_require_default(obj) {
15
15
  return obj && obj.__esModule ? obj : {
@@ -17,7 +17,7 @@ function _interop_require_default(obj) {
17
17
  };
18
18
  }
19
19
  function getNestedBlockTags(blockHTML, depth) {
20
- (0, _invariant.default)(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
20
+ (0, _tinyinvariant.default)(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
21
21
  if (typeof blockHTML.nest === 'function') {
22
22
  const { start, end } = (0, _splitReactElement.default)(blockHTML.nest(depth));
23
23
  return {
@@ -34,6 +34,6 @@ function getNestedBlockTags(blockHTML, depth) {
34
34
  nestEnd: end
35
35
  };
36
36
  }
37
- (0, _invariant.default)(Object.prototype.hasOwnProperty.call(blockHTML, 'nestStart') && Object.prototype.hasOwnProperty.call(blockHTML, 'nestEnd'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
37
+ (0, _tinyinvariant.default)(Object.hasOwn(blockHTML, 'nestStart') && Object.hasOwn(blockHTML, 'nestEnd'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
38
38
  return blockHTML;
39
39
  }
@@ -1,6 +1,6 @@
1
- import invariant from 'invariant';
2
1
  import React from 'react';
3
- import splitReactElement from './splitReactElement.js';
2
+ import invariant from 'tiny-invariant';
3
+ import splitReactElement from './splitReactElement.mjs';
4
4
  export default function getNestedBlockTags(blockHTML, depth) {
5
5
  invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
6
6
  if (typeof blockHTML.nest === 'function') {
@@ -19,6 +19,6 @@ export default function getNestedBlockTags(blockHTML, depth) {
19
19
  nestEnd: end
20
20
  };
21
21
  }
22
- invariant(Object.prototype.hasOwnProperty.call(blockHTML, 'nestStart') && Object.prototype.hasOwnProperty.call(blockHTML, 'nestEnd'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
22
+ invariant(Object.hasOwn(blockHTML, 'nestStart') && Object.hasOwn(blockHTML, 'nestEnd'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
23
23
  return blockHTML;
24
24
  }
@@ -8,9 +8,9 @@ Object.defineProperty(exports, "default", {
8
8
  return splitReactElement;
9
9
  }
10
10
  });
11
- const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
12
11
  const _react = /*#__PURE__*/ _interop_require_default(require("react"));
13
12
  const _server = /*#__PURE__*/ _interop_require_default(require("react-dom/server"));
13
+ const _tinyinvariant = /*#__PURE__*/ _interop_require_default(require("tiny-invariant"));
14
14
  function _interop_require_default(obj) {
15
15
  return obj && obj.__esModule ? obj : {
16
16
  default: obj
@@ -38,8 +38,8 @@ function splitReactElement(element) {
38
38
  return _server.default.renderToStaticMarkup(element);
39
39
  }
40
40
  const tags = _server.default.renderToStaticMarkup(/*#__PURE__*/ _react.default.cloneElement(element, {}, '\r')).split('\r');
41
- (0, _invariant.default)(tags.length > 1, `convertToHTML: Element of type ${element.type} must render children`);
42
- (0, _invariant.default)(tags.length < 3, `convertToHTML: Element of type ${element.type} cannot use carriage return character`);
41
+ (0, _tinyinvariant.default)(tags.length > 1, `convertToHTML: Element of type ${element.type} must render children`);
42
+ (0, _tinyinvariant.default)(tags.length < 3, `convertToHTML: Element of type ${element.type} cannot use carriage return character`);
43
43
  return {
44
44
  start: tags[0],
45
45
  end: tags[1]
@@ -1,6 +1,6 @@
1
- import invariant from 'invariant';
2
1
  import React from 'react';
3
2
  import ReactDOMServer from 'react-dom/server';
3
+ import invariant from 'tiny-invariant';
4
4
  // see http://w3c.github.io/html/syntax.html#writing-html-documents-elements
5
5
  const VOID_TAGS = [
6
6
  'area',
package/package.json CHANGED
@@ -1,32 +1,34 @@
1
1
  {
2
2
  "name": "@20minutes/draft-convert",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "Extensibly serialize & deserialize Draft.js ContentState",
5
- "main": "lib/index.js",
6
- "types": "types/index.d.ts",
7
- "module": "esm/index.js",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
- "types": "./types/index.d.ts",
11
- "require": "./lib/index.js",
12
- "import": "./esm/index.js"
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs",
13
+ "default": "./dist/index.mjs"
13
14
  }
14
15
  },
15
16
  "repository": "20minutes/draft-convert",
16
17
  "scripts": {
17
- "build": "yarn build:cjs && yarn build:esm",
18
- "build:cjs": "swc src -d lib --config-file .swcrc.cjs --strip-leading-paths --delete-dir-on-start",
19
- "build:esm": "swc src -d esm --config-file .swcrc.esm --strip-leading-paths --delete-dir-on-start",
18
+ "build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:types",
19
+ "build:cjs": "swc src -d dist --config-file .cjs.swcrc --strip-leading-paths && node scripts/rename-dist-cjs-entry.js",
20
+ "build:esm": "swc src -d dist --config-file .esm.swcrc --strip-leading-paths --delete-dir-on-start --out-file-extension mjs && node scripts/fix-dist-esm-imports.js",
21
+ "build:types": "node scripts/build-dist-types.js",
22
+ "check:package": "es-check es2022 'dist/**/*.{js,mjs,cjs}' --module",
20
23
  "test": "jest",
21
24
  "test:watch": "jest --watch",
22
- "clean": "rimraf ./lib ./esm",
25
+ "clean": "rimraf ./dist ./lib ./esm",
23
26
  "build-and-test": "yarn clean && yarn test",
24
- "lint": "eslint src/ test/"
27
+ "lint": "biome check --max-diagnostics=none",
28
+ "lint:fix": "biome check --write --unsafe --max-diagnostics=none"
25
29
  },
26
30
  "files": [
27
- "types",
28
- "lib",
29
- "esm"
31
+ "dist"
30
32
  ],
31
33
  "keywords": [
32
34
  "draft",
@@ -37,7 +39,7 @@
37
39
  "author": "bbriggs@hubspot.com",
38
40
  "license": "Apache-2.0",
39
41
  "engines": {
40
- "node": ">=22"
42
+ "node": ">=24"
41
43
  },
42
44
  "peerDependencies": {
43
45
  "draft-js": ">=0.7.0",
@@ -46,23 +48,17 @@
46
48
  },
47
49
  "dependencies": {
48
50
  "immutable": "~5.1.4",
49
- "invariant": "^2.2.1"
51
+ "tiny-invariant": "^1.3.3"
50
52
  },
51
53
  "devDependencies": {
52
- "@20minutes/eslint-config": "^2.0.0",
54
+ "@20minutes/eslint-config": "^3.0.4",
53
55
  "@swc/cli": "^0.8.0",
54
56
  "@swc/core": "^1.9.2",
55
57
  "@swc/jest": "^0.2.36",
56
58
  "draft-js": "^0.11.7",
57
- "eslint": "^9.0.0",
58
- "eslint-config-prettier": "^10.1.8",
59
- "eslint-plugin-import": "^2.32.0",
60
- "eslint-plugin-prettier": "^5.5.4",
61
- "eslint-plugin-react": "7.37.5",
62
- "eslint-plugin-react-hooks": "^7.0.1",
59
+ "es-check": "^9.6.4",
63
60
  "jest": "^30.2.0",
64
61
  "jest-environment-jsdom": "^30.2.0",
65
- "prettier": "^3.6.2",
66
62
  "react": "^18.0.0",
67
63
  "react-dom": "^18.0.0",
68
64
  "rimraf": "6.1.3",
package/esm/index.js DELETED
@@ -1,4 +0,0 @@
1
- import convertToHTML from './convertToHTML.js';
2
- import convertFromHTML from './convertFromHTML.js';
3
- import parseHTML from './util/parseHTML.js';
4
- export { convertToHTML, convertFromHTML, parseHTML };
package/types/index.d.ts DELETED
@@ -1,114 +0,0 @@
1
- // Based on: https://github.com/HubSpot/draft-convert/issues/107#issuecomment-488581709 by <https://github.com/sbusch>
2
- // Grabbed from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/draft-convert/index.d.ts
3
-
4
- declare module "@20minutes/draft-convert" {
5
- import {
6
- ContentState,
7
- DraftBlockType,
8
- DraftEntityMutability,
9
- DraftInlineStyleType,
10
- Entity,
11
- RawDraftContentBlock,
12
- RawDraftEntity,
13
- } from "draft-js";
14
- import { ReactNode } from "react";
15
-
16
- type RawDraftContentBlockWithCustomType<T> = Omit<RawDraftContentBlock, "type"> & {
17
- type: T;
18
- };
19
-
20
- type ExtendedHTMLElement<T> = (HTMLElement | HTMLLinkElement) & T;
21
-
22
- interface IConvertToHTMLConfig<
23
- S = DraftInlineStyleType,
24
- B extends DraftBlockType = DraftBlockType,
25
- E extends RawDraftEntity = RawDraftEntity,
26
- > {
27
- // Inline styles:
28
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
29
- styleToHTML?: ((style: S) => Tag | void) | undefined;
30
-
31
- // Block styles:
32
- blockToHTML?: ((block: RawDraftContentBlockWithCustomType<B>) => Tag) | undefined;
33
-
34
- // Entity styling:
35
- entityToHTML?: ((entity: E, originalText: string) => Tag) | undefined;
36
-
37
- // HTML validation:
38
- validateHTML?: ((html: string) => boolean) | undefined;
39
- }
40
-
41
- type EntityKey = string;
42
-
43
- interface IConvertFromHTMLConfig<
44
- S extends {
45
- [name: string]: unknown;
46
- } = DOMStringMap,
47
- B extends DraftBlockType = DraftBlockType,
48
- E extends RawDraftEntity = RawDraftEntity,
49
- > {
50
- // Inline styles:
51
- htmlToStyle?:
52
- | ((nodeName: string, node: ExtendedHTMLElement<S>, currentStyle: Set<string>) => Set<string>)
53
- | undefined;
54
-
55
- // Block styles:
56
- htmlToBlock?:
57
- | ((
58
- nodeName: string,
59
- node: ExtendedHTMLElement<S>,
60
- ) => B | { type: B; data: object } | false | undefined)
61
- | undefined;
62
-
63
- // Html entities
64
- htmlToEntity?:
65
- | ((
66
- nodeName: string,
67
- node: ExtendedHTMLElement<S>,
68
- createEntity: (type: E["type"], mutability: DraftEntityMutability, data: E["data"]) => EntityKey,
69
- getEntity: (key: EntityKey) => Entity,
70
- mergeEntityData: (key: string, data: object) => void,
71
- replaceEntityData: (key: string, data: object) => void,
72
- ) => EntityKey | undefined)
73
- | undefined;
74
-
75
- // Text entities
76
- textToEntity?:
77
- | ((
78
- text: string,
79
- createEntity: (type: E["type"], mutability: DraftEntityMutability, data: E["data"]) => EntityKey,
80
- getEntity: (key: EntityKey) => Entity,
81
- mergeEntityData: (key: string, data: object) => void,
82
- replaceEntityData: (key: string, data: object) => void,
83
- ) => Array<{
84
- entity: EntityKey;
85
- offset: number;
86
- length: number;
87
- result?: string | undefined;
88
- }>)
89
- | undefined;
90
- }
91
-
92
- type ContentStateConverter = (contentState: ContentState) => string;
93
- type HTMLConverter = (html: string) => ContentState;
94
-
95
- type Tag = ReactNode | { start: string; end: string; empty?: string | undefined } | {
96
- element: ReactNode;
97
- empty?: ReactNode | undefined;
98
- };
99
-
100
- export function convertToHTML<
101
- S = DraftInlineStyleType,
102
- B extends DraftBlockType = DraftBlockType,
103
- E extends RawDraftEntity = RawDraftEntity,
104
- >(config: IConvertToHTMLConfig<S, B, E>): ContentStateConverter;
105
- export function convertToHTML(contentState: ContentState): string;
106
- export function convertFromHTML<
107
- S extends {
108
- [name: string]: unknown;
109
- } = DOMStringMap,
110
- B extends DraftBlockType = DraftBlockType,
111
- E extends RawDraftEntity = RawDraftEntity,
112
- >(config: IConvertFromHTMLConfig<S, B, E>): HTMLConverter;
113
- export function convertFromHTML(html: string): ContentState;
114
- }
File without changes
File without changes
@@ -8,8 +8,8 @@ Object.defineProperty(exports, "default", {
8
8
  return _default;
9
9
  }
10
10
  });
11
- const _updateMutation = /*#__PURE__*/ _interop_require_default(require("./util/updateMutation.js"));
12
11
  const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort.js"));
12
+ const _updateMutation = /*#__PURE__*/ _interop_require_default(require("./util/updateMutation.js"));
13
13
  function _interop_require_default(obj) {
14
14
  return obj && obj.__esModule ? obj : {
15
15
  default: obj
@@ -19,8 +19,8 @@ _export(exports, {
19
19
  return _parseHTML.default;
20
20
  }
21
21
  });
22
- const _convertToHTML = /*#__PURE__*/ _interop_require_default(require("./convertToHTML.js"));
23
22
  const _convertFromHTML = /*#__PURE__*/ _interop_require_default(require("./convertFromHTML.js"));
23
+ const _convertToHTML = /*#__PURE__*/ _interop_require_default(require("./convertToHTML.js"));
24
24
  const _parseHTML = /*#__PURE__*/ _interop_require_default(require("./util/parseHTML.js"));
25
25
  function _interop_require_default(obj) {
26
26
  return obj && obj.__esModule ? obj : {
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes