@takeshape/util 9.33.1 → 9.33.5

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/dist/draftjs.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RawDraftContentState, Entity } from 'draft-js';
1
+ import { RawDraftContentState, Entity, RawDraftEntityRange } from 'draft-js';
2
2
  export interface Content {
3
3
  [name: string]: any;
4
4
  _id: string;
@@ -13,6 +13,34 @@ type DraftToMarkdownOptionsItems = Record<string, {
13
13
  open: (entity?: DraftEntityWithData) => string;
14
14
  close: (entity?: DraftEntityWithData) => string;
15
15
  }>;
16
+ /**
17
+ * Adjust entity ranges to insert `amount` space starting at `after`.
18
+ * For example, take this visualization of a range with offset 5 and length 5, where
19
+ * the vertical bars are the start and end of the range, inclusive:
20
+ * -----|---|-----
21
+ * If you insert amount 5 after 0 you get this:
22
+ * ----------|---|-----
23
+ * If you insert amount 5 after 7 you get this:
24
+ * -----|--------|-----
25
+ * If you insert amount 5 after 10 you get this:
26
+ * -----|---|----------
27
+ * If you insert amount -2 after 7 you get this:
28
+ * -----|-|-----
29
+ */
30
+ export declare const adjustEntityRanges: (entityRanges: RawDraftEntityRange[], after: number, amount: number) => {
31
+ offset: number;
32
+ length: number;
33
+ key: number;
34
+ }[];
35
+ /**
36
+ * Encode HTML entities in this draftjs state.
37
+ * We adjust entity ranges so that their offsets and lengths are still correct with the
38
+ * encoded text.
39
+ * EG `&#x27;` is 5 characters longer than `'` so if there is a single apostrophe before a link,
40
+ * the link entity's offset needs to be increased by 5 when the text is encoded.
41
+ * This also applies to the entity's length if there are escaped characters inside the entity.
42
+ */
43
+ export declare const encodeHtmlEntities: (draftjs: RawDraftContentState) => RawDraftContentState;
16
44
  export declare function fromDraftjs(draftjs: RawDraftContentState, styleItems?: DraftToMarkdownOptionsItems, entityItems?: DraftToMarkdownOptionsItems): string;
17
45
  export declare function draftjsToMd(draftjs: RawDraftContentState, assets: Record<string, MaybeContent>): string;
18
46
  export declare function draftjsToMdx(draftjs: RawDraftContentState, assets: Record<string, MaybeContent>, prefix?: (tagName: string) => string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"draftjs.d.ts","sourceRoot":"","sources":["../../src/draftjs.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,oBAAoB,EAAwB,MAAM,EAAiB,MAAM,UAAU,CAAC;AAsB5F,MAAM,WAAW,OAAO;IACtB,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,SAAS,CAAC;AAE/C,UAAU,mBAAoB,SAAQ,MAAM;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC5B;AA2BD,KAAK,2BAA2B,GAAG,MAAM,CACvC,MAAM,EACN;IACE,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,mBAAmB,KAAK,MAAM,CAAC;IAC/C,KAAK,EAAE,CAAC,MAAM,CAAC,EAAE,mBAAmB,KAAK,MAAM,CAAC;CACjD,CACF,CAAC;AAqDF,wBAAgB,WAAW,CACzB,OAAO,EAAE,oBAAoB,EAC7B,UAAU,CAAC,EAAE,2BAA2B,EACxC,WAAW,CAAC,EAAE,2BAA2B,GACxC,MAAM,CAyER;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,GAAG,MAAM,CA0DvG;AAmBD,wBAAgB,YAAY,CAC1B,OAAO,EAAE,oBAAoB,EAC7B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,EACpC,MAAM,8BAAqB,GAC1B,MAAM,CAyDR;AA4MD,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,o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BAA2B,GACxC,MAAM,CAyER;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,GAAG,MAAM,CA0DvG;AAmBD,wBAAgB,YAAY,CAC1B,OAAO,EAAE,oBAAoB,EAC7B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,EACpC,MAAM,8BAAqB,GAC1B,MAAM,CAyDR;AA4MD,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB,CAgD7D;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvD;AA+ND;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAwB5D;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB,CA0G9D"}
package/dist/draftjs.js CHANGED
@@ -3,8 +3,10 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.adjustEntityRanges = void 0;
6
7
  exports.draftjsToMd = draftjsToMd;
7
8
  exports.draftjsToMdx = draftjsToMdx;
9
+ exports.encodeHtmlEntities = void 0;
8
10
  exports.fromDraftjs = fromDraftjs;
9
11
  exports.getImagePathFromUrl = getImagePathFromUrl;
10
12
  exports.insertBreaksAroundBlocks = insertBreaksAroundBlocks;
@@ -101,25 +103,76 @@ const handleMultiword = (markdown, marker, markdownSyntax, markdownRegex) => {
101
103
 
102
104
  const handleExternalLinks = markdown => {
103
105
  const regex = /(<TSExternalLink text="TEMPORARY_EXTERNAL_LINK_MARKER_HP3vprmERkc9ZAss" href="[^"]+">)([^<]+)<\/TSExternalLink>/g;
104
- return markdown.replace(regex, (match, openTag, content) => match.replace(EXTERNAL_LINK_MARKER, (0, _escape.default)(content)));
106
+ return markdown.replace(regex, (match, openTag, content) => match.replace(EXTERNAL_LINK_MARKER, content));
105
107
  };
108
+ /**
109
+ * Adjust entity ranges to insert `amount` space starting at `after`.
110
+ * For example, take this visualization of a range with offset 5 and length 5, where
111
+ * the vertical bars are the start and end of the range, inclusive:
112
+ * -----|---|-----
113
+ * If you insert amount 5 after 0 you get this:
114
+ * ----------|---|-----
115
+ * If you insert amount 5 after 7 you get this:
116
+ * -----|--------|-----
117
+ * If you insert amount 5 after 10 you get this:
118
+ * -----|---|----------
119
+ * If you insert amount -2 after 7 you get this:
120
+ * -----|-|-----
121
+ */
106
122
 
107
- const encodeHtmlEntities = draftjs => {
108
- return { ...draftjs,
109
- blocks: draftjs.blocks.map(block => ({ ...block,
110
- text: _he.default.encode(block.text)
111
- }))
112
- };
123
+
124
+ const adjustEntityRanges = (entityRanges, after, amount) => {
125
+ return entityRanges.map(entityRange => ({ ...entityRange,
126
+ offset: entityRange.offset > after ? entityRange.offset + amount : entityRange.offset,
127
+ length: entityRange.offset <= after && entityRange.offset + entityRange.length > after ? entityRange.length + amount : entityRange.length
128
+ }));
113
129
  };
130
+ /**
131
+ * Encode HTML entities in this draftjs state.
132
+ * We adjust entity ranges so that their offsets and lengths are still correct with the
133
+ * encoded text.
134
+ * EG `&#x27;` is 5 characters longer than `'` so if there is a single apostrophe before a link,
135
+ * the link entity's offset needs to be increased by 5 when the text is encoded.
136
+ * This also applies to the entity's length if there are escaped characters inside the entity.
137
+ */
138
+
139
+
140
+ exports.adjustEntityRanges = adjustEntityRanges;
114
141
 
115
- const decodeHtmlEntities = draftjs => {
116
- return { ...draftjs,
117
- blocks: draftjs.blocks.map(block => ({ ...block,
118
- text: _he.default.decode(block.text)
119
- }))
142
+ const encodeHtmlEntities = draftjs => {
143
+ const result = { ...draftjs,
144
+ blocks: draftjs.blocks.map(block => {
145
+ let {
146
+ text
147
+ } = block;
148
+ let {
149
+ entityRanges
150
+ } = block;
151
+
152
+ for (let i = 0; i < text.length; i++) {
153
+ const char = text.charAt(i);
154
+
155
+ const encodedChar = _he.default.encode(char);
156
+
157
+ if (encodedChar !== char) {
158
+ const offset = encodedChar.length - char.length;
159
+ entityRanges = adjustEntityRanges(entityRanges, i, offset);
160
+ text = text.substring(0, i) + encodedChar + text.substring(i + 1);
161
+ i += offset;
162
+ }
163
+ }
164
+
165
+ return { ...block,
166
+ entityRanges,
167
+ text
168
+ };
169
+ })
120
170
  };
171
+ return result;
121
172
  };
122
173
 
174
+ exports.encodeHtmlEntities = encodeHtmlEntities;
175
+
123
176
  function fromDraftjs(draftjs, styleItems, entityItems) {
124
177
  let markdown = (0, _markdownDraftJs.draftToMarkdown)(encodeHtmlEntities(draftjs), {
125
178
  styleItems: {
@@ -549,7 +602,7 @@ function fromMd(md, blockEntities, blockTypes) {
549
602
  lang: item.params || ''
550
603
  },
551
604
  // Using the text handling from markdown-to-draft
552
- text: (item.content || '').replace(/\n$/, ''),
605
+ text: _he.default.decode((item.content || '').replace(/\n$/, '')),
553
606
  entityRanges: [],
554
607
  inlineStyleRanges: []
555
608
  };
@@ -703,8 +756,11 @@ function addLinks(state, links) {
703
756
  entity.type = 'LINK';
704
757
  entity.mutability = 'MUTABLE';
705
758
  entity.data = link;
706
- entity.text = link.text;
707
- entityRange.length += link.text.length;
759
+
760
+ const decodedText = _he.default.decode(link.text);
761
+
762
+ entity.text = decodedText;
763
+ entityRange.length += decodedText.length;
708
764
  linkNumber++;
709
765
  }
710
766
 
@@ -934,5 +990,5 @@ function mdxToDraftjs(mdx) {
934
990
 
935
991
  return ((_entity$data11 = entity.data) === null || _entity$data11 === void 0 ? void 0 : _entity$data11.marker) === undefined && !entityKeysToExclude.includes(Number(key));
936
992
  });
937
- return decodeHtmlEntities(result);
993
+ return result;
938
994
  }
package/es/draftjs.js CHANGED
@@ -73,25 +73,69 @@ const handleMultiword = (markdown, marker, markdownSyntax, markdownRegex) => {
73
73
 
74
74
  const handleExternalLinks = markdown => {
75
75
  const regex = /(<TSExternalLink text="TEMPORARY_EXTERNAL_LINK_MARKER_HP3vprmERkc9ZAss" href="[^"]+">)([^<]+)<\/TSExternalLink>/g;
76
- return markdown.replace(regex, (match, openTag, content) => match.replace(EXTERNAL_LINK_MARKER, escape(content)));
76
+ return markdown.replace(regex, (match, openTag, content) => match.replace(EXTERNAL_LINK_MARKER, content));
77
77
  };
78
+ /**
79
+ * Adjust entity ranges to insert `amount` space starting at `after`.
80
+ * For example, take this visualization of a range with offset 5 and length 5, where
81
+ * the vertical bars are the start and end of the range, inclusive:
82
+ * -----|---|-----
83
+ * If you insert amount 5 after 0 you get this:
84
+ * ----------|---|-----
85
+ * If you insert amount 5 after 7 you get this:
86
+ * -----|--------|-----
87
+ * If you insert amount 5 after 10 you get this:
88
+ * -----|---|----------
89
+ * If you insert amount -2 after 7 you get this:
90
+ * -----|-|-----
91
+ */
78
92
 
79
- const encodeHtmlEntities = draftjs => {
80
- return { ...draftjs,
81
- blocks: draftjs.blocks.map(block => ({ ...block,
82
- text: he.encode(block.text)
83
- }))
84
- };
93
+
94
+ export const adjustEntityRanges = (entityRanges, after, amount) => {
95
+ return entityRanges.map(entityRange => ({ ...entityRange,
96
+ offset: entityRange.offset > after ? entityRange.offset + amount : entityRange.offset,
97
+ length: entityRange.offset <= after && entityRange.offset + entityRange.length > after ? entityRange.length + amount : entityRange.length
98
+ }));
85
99
  };
100
+ /**
101
+ * Encode HTML entities in this draftjs state.
102
+ * We adjust entity ranges so that their offsets and lengths are still correct with the
103
+ * encoded text.
104
+ * EG `&#x27;` is 5 characters longer than `'` so if there is a single apostrophe before a link,
105
+ * the link entity's offset needs to be increased by 5 when the text is encoded.
106
+ * This also applies to the entity's length if there are escaped characters inside the entity.
107
+ */
86
108
 
87
- const decodeHtmlEntities = draftjs => {
88
- return { ...draftjs,
89
- blocks: draftjs.blocks.map(block => ({ ...block,
90
- text: he.decode(block.text)
91
- }))
109
+ export const encodeHtmlEntities = draftjs => {
110
+ const result = { ...draftjs,
111
+ blocks: draftjs.blocks.map(block => {
112
+ let {
113
+ text
114
+ } = block;
115
+ let {
116
+ entityRanges
117
+ } = block;
118
+
119
+ for (let i = 0; i < text.length; i++) {
120
+ const char = text.charAt(i);
121
+ const encodedChar = he.encode(char);
122
+
123
+ if (encodedChar !== char) {
124
+ const offset = encodedChar.length - char.length;
125
+ entityRanges = adjustEntityRanges(entityRanges, i, offset);
126
+ text = text.substring(0, i) + encodedChar + text.substring(i + 1);
127
+ i += offset;
128
+ }
129
+ }
130
+
131
+ return { ...block,
132
+ entityRanges,
133
+ text
134
+ };
135
+ })
92
136
  };
137
+ return result;
93
138
  };
94
-
95
139
  export function fromDraftjs(draftjs, styleItems, entityItems) {
96
140
  let markdown = draftToMarkdown(encodeHtmlEntities(draftjs), {
97
141
  styleItems: {
@@ -520,7 +564,7 @@ function fromMd(md, blockEntities, blockTypes) {
520
564
  lang: item.params || ''
521
565
  },
522
566
  // Using the text handling from markdown-to-draft
523
- text: (item.content || '').replace(/\n$/, ''),
567
+ text: he.decode((item.content || '').replace(/\n$/, '')),
524
568
  entityRanges: [],
525
569
  inlineStyleRanges: []
526
570
  };
@@ -673,8 +717,9 @@ function addLinks(state, links) {
673
717
  entity.type = 'LINK';
674
718
  entity.mutability = 'MUTABLE';
675
719
  entity.data = link;
676
- entity.text = link.text;
677
- entityRange.length += link.text.length;
720
+ const decodedText = he.decode(link.text);
721
+ entity.text = decodedText;
722
+ entityRange.length += decodedText.length;
678
723
  linkNumber++;
679
724
  }
680
725
 
@@ -903,5 +948,5 @@ export function mdxToDraftjs(mdx) {
903
948
 
904
949
  return ((_entity$data11 = entity.data) === null || _entity$data11 === void 0 ? void 0 : _entity$data11.marker) === undefined && !entityKeysToExclude.includes(Number(key));
905
950
  });
906
- return decodeHtmlEntities(result);
951
+ return result;
907
952
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takeshape/util",
3
- "version": "9.33.1",
3
+ "version": "9.33.5",
4
4
  "description": "Shared utilities",
5
5
  "homepage": "https://www.takeshape.io",
6
6
  "repository": {
@@ -32,7 +32,7 @@
32
32
  "prismjs": "^1.25.0",
33
33
  "shortid": "^2.2.16",
34
34
  "url-parse": "^1.5.3",
35
- "@takeshape/routing": "9.33.1"
35
+ "@takeshape/routing": "9.33.5"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/classnames": "^2.2.9",
@@ -45,7 +45,7 @@
45
45
  "@types/prismjs": "^1.16.2",
46
46
  "@types/shortid": "^0.0.29",
47
47
  "stripe": "8.81.0",
48
- "@takeshape/typescript-jest-junit-reporter": "9.33.1"
48
+ "@takeshape/typescript-jest-junit-reporter": "9.33.5"
49
49
  },
50
50
  "engines": {
51
51
  "node": ">=16"