@expensify/react-native-live-markdown 0.1.12 → 0.1.14

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.
@@ -7,8 +7,9 @@ type MarkdownType = 'bold' | 'italic' | 'strikethrough' | 'link' | 'code' | 'pre
7
7
 
8
8
  type MarkdownRange = {
9
9
  type: MarkdownType;
10
- startIndex: number;
10
+ start: number;
11
11
  length: number;
12
+ depth?: number;
12
13
  };
13
14
 
14
15
  type NestedNode = {
@@ -77,6 +78,20 @@ function addSubstringAsTextNode(root: HTMLElement, text: string, startIndex: num
77
78
  }
78
79
  }
79
80
 
81
+ function ungroupRanges(ranges: MarkdownRange[]): MarkdownRange[] {
82
+ const ungroupedRanges: MarkdownRange[] = [];
83
+ ranges.forEach((range) => {
84
+ if (!range.depth) {
85
+ ungroupedRanges.push(range);
86
+ }
87
+ const {depth, ...rangeWithoutDepth} = range;
88
+ Array.from({length: depth!}).forEach(() => {
89
+ ungroupedRanges.push(rangeWithoutDepth);
90
+ });
91
+ });
92
+ return ungroupedRanges;
93
+ }
94
+
80
95
  function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownStyle: PartialMarkdownStyle = {}, disableInlineStyles = false): HTMLElement {
81
96
  const root: HTMLElement = document.createElement('span');
82
97
  root.className = 'root';
@@ -86,7 +101,7 @@ function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownS
86
101
  return root;
87
102
  }
88
103
 
89
- const stack = [...ranges];
104
+ const stack = ungroupRanges(ranges);
90
105
  const nestedStack: NestedNode[] = [{node: root, endIndex: textLength}];
91
106
  let lastRangeEndIndex = 0;
92
107
  while (stack.length > 0) {
@@ -99,10 +114,10 @@ function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownS
99
114
  break;
100
115
  }
101
116
 
102
- const endOfCurrentRange = range.startIndex + range.length;
103
- const nextRangeStartIndex = stack.length > 0 && !!stack[0] ? stack[0].startIndex || 0 : textLength;
117
+ const endOfCurrentRange = range.start + range.length;
118
+ const nextRangeStartIndex = stack.length > 0 && !!stack[0] ? stack[0].start || 0 : textLength;
104
119
 
105
- addSubstringAsTextNode(currentRoot.node, text, lastRangeEndIndex, range.startIndex); // add text with newlines before current range
120
+ addSubstringAsTextNode(currentRoot.node, text, lastRangeEndIndex, range.start); // add text with newlines before current range
106
121
 
107
122
  const span = document.createElement('span');
108
123
  if (disableInlineStyles) {
@@ -115,9 +130,9 @@ function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownS
115
130
  // tag nesting
116
131
  currentRoot.node.appendChild(span);
117
132
  nestedStack.push({node: span, endIndex: endOfCurrentRange});
118
- lastRangeEndIndex = range.startIndex;
133
+ lastRangeEndIndex = range.start;
119
134
  } else {
120
- addSubstringAsTextNode(span, text, range.startIndex, endOfCurrentRange);
135
+ addSubstringAsTextNode(span, text, range.start, endOfCurrentRange);
121
136
  currentRoot.node.appendChild(span);
122
137
  lastRangeEndIndex = endOfCurrentRange;
123
138
 
@@ -162,14 +177,7 @@ function parseText(
162
177
  }
163
178
  const ranges = global.parseExpensiMarkToRanges(text);
164
179
 
165
- const markdownRanges: MarkdownRange[] = ranges.map((range) => {
166
- const [type, startIndex, length] = range;
167
- return {
168
- type: type as MarkdownType,
169
- startIndex,
170
- length,
171
- };
172
- });
180
+ const markdownRanges: MarkdownRange[] = ranges as MarkdownRange[];
173
181
 
174
182
  targetElement.innerHTML = '';
175
183
  targetElement.innerText = '';