Apply overlapping style ranges to a string as nested HTML
Given a string and an array representing styles, the task is to render HTML similar to the output of a rich text editor. Before building the rich text parser, review MDN’s HTML elements reference.
The input consists of a string and a style array. Each element in the style array is another array that contains three items: the start index, the end index, and the HTML tag to be applied. The goal is to apply these styles to the specified ranges within the string and generate the corresponding HTML output.
Example:
Input: 'Hello, world', [[0, 2, 'i'], [4, 9, 'b'], [7, 10, 'u']]
Output: '<i>Hel</i>l<b>o, w<u>orl</u></b><u>d</u>'
This example demonstrates overlapping style ranges where the <u> tag is applied both within and outside the <b> tag’s range, showing the complexity of nesting and ordering HTML tags based on the input style ranges.
The challenge lies in correctly applying the styles, especially when the style ranges overlap, ensuring that the HTML tags are nested and ordered properly according to their positions in the input string.