question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Highlight text in multiple <div> elements

See original GitHub issue

Before you start - checklist

  • I have read documentation in README
  • I have checked sample and test suites to see real life basic implementation
  • I have checked if this question is not already asked

What are you trying to achieve? Please describe. Hello! Thank you very much for this awesome react lib! I am currently using it with he annotation layer, and so far it works nicely! It finds the word pattern in the text and highlights it. But i would like to highlight e.g. multiple lines (where sometimes, these elements could be splited in different div elements in the text-layer).

Describe solutions you’ve tried

my current solution is the one that you have already mentioned on git hub! but it takes each elements of the layer and compares the text:

 const highlightPattern = (text, pattern) => {
  const splitText = text.split(pattern);

  if (splitText.length <= 1) {
    return text;
  }

  const matches = text.match(pattern);
  console.log(matches)
  return splitText.reduce((arr, element, index) => (matches[index] ? [
    ...arr,
    element,
    <mark key={uuid()}>
      {matches[index]}
    </mark>,
  ] : [...arr, element]), []);

So it will always compare the whole string in an element, correct?

Additional information Now a visual example to make it more appealing 😛 image and now once i past the threshold to the “other div”.

searchstring: “Congratulations, your computer is equipped with a PDF (Portable Document Format) #reader”

screenshot from 2018-11-25 17-34-41

Environment

  • Browser (if applicable) [Firefox 59]:
  • React-PDF version [e.g. 3.0.5]:
  • React version [e.g. 16.3.2]:
  • Webpack version (if applicable) [e.g. 4.8.3]:

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
taherbertcommented, Dec 3, 2018

I’m not sure I totally understand your request – are you trying to highlight multiple phrases, rather than just one? If so, this small modification to the recipe works for me:

  highlightPattern = (text, highlightWords) => {
    // This is the only major change -- rather than searching for a string, create a regex to search
    // for any strings in a provided array
    const regex = new RegExp(highlightWords.join('|'), 'gi')
    const splitText = text.split(regex)

    if (splitText.length <= 1) {
      return text
    }

    const matches = text.match(regex)

    return splitText.reduce(
      (arr, element, index) =>
        matches[index]
          ? [...arr, element, <mark>{matches[index]}</mark>]
          : [...arr, element],
      []
    )
  }

2reactions
taherbertcommented, Dec 4, 2018

Yes, highlightWords is an array of strings to highlight. Change your searchText to be an array instead of just a string.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Highlight text in multiple content divs from input - Stack Overflow
I have this search and highlight function which works fine until I try to search across multiple content divs. What would be the...
Read more >
mark.js – JavaScript keyword highlight
js is a text highlighter written in JavaScript. It can be used to dynamically mark search terms or custom regular expressions and offers...
Read more >
How to Highlight the Searched String Result using JavaScript
First we declare a variable which contains context from which it is going to find and highlight the searched text. Create an object...
Read more >
Highlighting html text / Mathieu Jouhet - Observable
In order for text to be highlighted across multiple html elements, we can use a very simple state machine: the basic idea is...
Read more >
The Mark Text element - HTML: HyperText Markup Language
The <mark> HTML element represents text which is marked or highlighted for reference or notation purposes due to the marked passage's ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found