I have been working on a project lately where I need to be able to annotate a sentence at specific indices using vanilla javascript. For example: The dog runs really fast.
The input I have to work with looks something like this:
[
{beginIndex: 4, endIndex: 7, style: "bold"},
{beginIndex: 5, endIndex: 7, style: "italics"},
{beginIndex: 13, endIndex: 19, style: "italics"},
]
The challenge I'm running into is when I have to deal with overlapping indices (as seen in index 0 and 1 of the array above).
I figured the best way to handle overlapping would be to restructure the array like this:
[
{beginIndex: 4, endIndex: 5, style: "bold"},
{beginIndex: 5, endIndex: 7, style: "italics bold"},
{beginIndex: 13, endIndex: 19, style: "italics"},
]
And then use the array to dynamically build some simple html that looks like this:
<span>The <span class="bold">d</span><span class="bold italics">og</span> runs <span class="italics">really</span> fast.</span>
I've spent a few hours trying to figure out how to take the input, and then generate an output that handles overlapping indices. I feel like this should be a pretty simple CS exercise, but I just can't seem to figure out how to do it. Does anybody have any advice on how to handle this situation. Or if this even is the best way to go about annotating text using vanilla js and html?
This image kind of illustrates what I'm looking for. 3 inputs that identify some indices to annotate, and the 4th line would be the output, combining all of the annotations onto one line.
