Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to use with v-html description ? #48

Open
frufin opened this issue Apr 6, 2021 · 3 comments
Open

Any way to use with v-html description ? #48

frufin opened this issue Apr 6, 2021 · 3 comments

Comments

@frufin
Copy link

frufin commented Apr 6, 2021

Hi Albert,
How would it be possible to search query inside bound text (v-html) ?

@zhangzef
Copy link

I have the same question

@adamkhan
Copy link

adamkhan commented Sep 9, 2021

+1. v-html is how Vue is supposed to work, given that they deprecated the triple moustache. I at least can't really use Text Highlight without this.

@adlairise
Copy link

adlairise commented Nov 2, 2021

I had this problem as well - seems like it should get fixed at some point, but I used this work around:

My desired vue markup would have looked like this:

<text-highlight :queries="queries">
  <div class="card-summary" v-html="item.Summary"></div>
</text-highlight>`

So instead I added a highlightBody filter in the script export default that replaces that should be highlighted with a wrapping tag that it finds using regex. The markup looks like:

<div class="card-summary" v-html="$options.filters.highlightText(item.Summary, queries)"></div>

and the filter looks like:

filters: {
  highlightText: function (str, queries) {
    const query = queries[0];
    const check = new RegExp(query, 'ig');
    return str.toString().replace(check, function (matchedText, a, b){
      return (`<mark class="text__highlight">${ matchedText }</mark>`);
    });
    return str;
  }
}

In my case I only needed the first query, so if you need more you may have to do a loop of all queries and do a replace on the string each time. Also, this solution leaves out the index and text properties on the <mark> that vue-text-highlight includes on the nodes. Depending on your implementation you may need to add further logic to include those values, but in my use case I didn't.

However, importantly it allows for the same styling that's used in other places with . Additionally, it's important to keep in mind that this is not a bulletproof solution - this logic exists entirely outside of vue-text-highlight, meaning that any place where you change logic/classnames with options like highlightClass or highlightComponent, you will have to reimplement the logic in the above filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants