bvaughn / highlight-words-core

Utility functions shared by react-highlight-words and react-native-highlight-words

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

smart truncate

ComLock opened this issue · comments

So this can become rather complicated, and I have not thought this quite through yet, but here we go:

I would like a truncate functionality that limits the length of the highlighted text.
It should be able to replace the start, the end or both with a custom ellipsis.
It should find the index of the first highlight and perhaps allow for a custom number of letters or words to be present before the highlighted word. I guess knowing what is a word becomes a problem of language so number of letters should be a sufficient starting point.

It should not insert ellipsis if the string is short enough.

I needs to do all that before applying html elements like for instance bold.

I could not find any npm module for this. And due to its complexity I figure it needs to be part of the highlighter.

If all the highlighted terms are towards the end of the string it should still try to fill the truncated text to its limit.

What you're describing sounds pretty specific. This package is meant to be more of a generic solution.

You'd be welcome to implement what you're describing above as a PR for consideration but I don't have the time to add such a feature to this package, or the need for it. 😄

Going to close this issue (since I don't plan on taking any action on it) but we can keep talking here after it's closed!

This only does end truncation but worth a mention https://github.com/alexghr/html-ellipsis

Yup that works. Made a PR too. dominictarr/highlight-search-result#4
I'm done for now.

Cool! Glad you found something that works for you :)