Skip to content

Commit

Permalink
fix newlines in web parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid committed Feb 7, 2024
1 parent 190cb80 commit bbde776
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/web/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,12 @@ function addSubstringAsTextNode(root: HTMLElement, text: string, startIndex: num
}
}

function addTextWithNewlines(root: HTMLElement, text: string, startIndex: number, endIndex: number) {
const textAfterLastRange = text.substring(startIndex, endIndex);
if (textAfterLastRange.length > 0) {
textAfterLastRange.split('\n').forEach((line, index, array) => {
if (index < array.length - 1) {
addSubstringAsTextNode(root, line, 0, line.length);
root.appendChild(document.createElement('br'));
} else {
addSubstringAsTextNode(root, line, 0, line.length);
}
});
}
}

function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownStyle: PartialMarkdownStyle = {}, disableInlineStyles = false): HTMLElement {
const root: HTMLElement = document.createElement('span');
root.className = 'root';
const textLength = text.length;
if (ranges.length === 0) {
addTextWithNewlines(root, text, 0, textLength);
addSubstringAsTextNode(root, text, 0, textLength);
return root;
}

Expand All @@ -116,7 +102,7 @@ function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownS
const endOfCurrentRange = range.startIndex + range.length;
const nextRangeStartIndex = stack.length > 0 && !!stack[0] ? stack[0].startIndex || 0 : textLength;

addTextWithNewlines(currentRoot.node, text, lastRangeEndIndex, range.startIndex); // add text with newlines before current range
addSubstringAsTextNode(currentRoot.node, text, lastRangeEndIndex, range.startIndex); // add text with newlines before current range

const span = document.createElement('span');
if (disableInlineStyles) {
Expand All @@ -131,13 +117,13 @@ function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownS
nestedStack.push({node: span, endIndex: endOfCurrentRange});
lastRangeEndIndex = range.startIndex;
} else {
addTextWithNewlines(span, text, range.startIndex, endOfCurrentRange);
addSubstringAsTextNode(span, text, range.startIndex, endOfCurrentRange);
currentRoot.node.appendChild(span);
lastRangeEndIndex = endOfCurrentRange;

// end of tag nesting
while (nestedStack.length - 1 > 0 && nextRangeStartIndex >= currentRoot.endIndex) {
addTextWithNewlines(currentRoot.node, text, lastRangeEndIndex, currentRoot.endIndex);
addSubstringAsTextNode(currentRoot.node, text, lastRangeEndIndex, currentRoot.endIndex);
const prevRoot = nestedStack.pop();
if (!prevRoot) {
break;
Expand All @@ -155,7 +141,7 @@ function parseRangesToHTMLNodes(text: string, ranges: MarkdownRange[], markdownS
}
}

addTextWithNewlines(root, text, lastRangeEndIndex, textLength);
addSubstringAsTextNode(root, text, lastRangeEndIndex, textLength);
return root;
}

Expand Down

0 comments on commit bbde776

Please sign in to comment.