DEV Community

Discussion on: How would you build a Medium-esque highlighting feature?

allanjeremy profile image
Allan N Jeremy

How would you approach collisions? Like, I highlight this half of the passage and you highlight another. This is something I've wondered about Genius's annotations.

Haven't used them as much either but from the little I have used, here's how I would theoretically approach different highlighting collisions. Let's have User A highlight Passage A and User B highlight Passage B in this example. We will assume a table or storage medium that allows for storing of highlights with extra information such as comments

When a user highlights a section, you store the sub-string of what was highlighted as well as the starting point & end point for each highlight sub-string in our 'database'. You then highlight the entire area (by simply checking if highlights stack up with no breaks up until where there is a break is where you end the highlight) that has been highlighted using one highlight (whether there's multiple highlights or not). Store the entire highlighted area. When a user hovers over the highlighted area; You check which highlights are within the range of the highlighted area and simply display the substring and whoever highlighted it, if it is in range.

For a passage
She sells sea shells at the sea shore.

Let's assume User A highlighted She sells seashells at the and User B highlighted seashells at the seashore.

We would store highlight1 as having a start_index=0; end_index=x (bummer, this was a pain to count so I just used x, might edit in the actual value)

  • We would also store any information related to that highlight here

same thing for highlight2

How we would end up highlighting the whole string would be something close to

//Tired at the moment, 3.23am here, will attempt writing pseudocode when I can

but basically that was my thought process for the collision problem. Of course feel free to share any issues that might arise in this approach so that we can arrive at a better solution

albertofdzm profile image
Alberto Fernandez • Edited on

I think that store the whole string is a bad practice since it would increment the database size exponentially.

I know that if you don't store the string you couldn't find where was the annotation if the author decides to change the content but I don't find it that important compared to the storage cost it would provoke plus the reprocess you would have to do refinding the annotations each time an author edits the content.