Code Monkey home page Code Monkey logo

highlight-words-core's Introduction

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


๐ŸŽ‰ Become a sponsor or โ˜• Buy me a coffee


API

The primary API for this package is a function exported as findAll. This method searches a string of text for a set of search terms and returns an array of "chunks" that describe the matches found.

Each "chunk" is an object consisting of a pair of indices (chunk.start and chunk.end) and a boolean specfifying whether the chunk is a match (chunk.highlight). For example:

import { findAll } from "highlight-words-core";

const textToHighlight = "This is some text to highlight.";
const searchWords = ["This", "i"];

const chunks = findAll({
  searchWords,
  textToHighlight
});

const highlightedText = chunks
  .map(chunk => {
    const { end, highlight, start } = chunk;
    const text = textToHighlight.substr(start, end - start);
    if (highlight) {
      return `<mark>${text}</mark>`;
    } else {
      return text;
    }
  })
  .join("");

Run this example on Code Sandbox.

findAll

The findAll function accepts several parameters, although only the searchWords array and textToHighlight string are required.

Parameter Required? Type Description
autoEscape boolean Escape special regular expression characters
caseSensitive boolean Search should be case sensitive
findChunks Function Custom find function (advanced)
sanitize Function Custom sanitize function (advanced)
searchWords โœ… Array<string> Array of words to search for
textToHighlight โœ… string Text to search and highlight

License

MIT License - fork, modify and use however you want.

highlight-words-core's People

Contributors

asapach avatar bigappleinsider avatar bvaughn avatar clauderic avatar colinrtaylor avatar edmorley avatar jnachtigall avatar richardbradley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

highlight-words-core's Issues

Access active element

Is there anyway (like a ref or something) to access the active highlighted element? I need to scroll to the new active element each time the activeIndex changes.

babel-runtime dependency

babel-runtime is listed as dependency in package.json rather than devDependency - is that intended? How is it required to use this lib? There's no dependency on React, but why is there a dependency on Babel?

Return Text Fragments for linking to highlights?

Hi @bvaughn, thanks for this package, very useful!

I wanted to see whether you'd be open to also returning a Text Fragment (blog post) in addition to the chunks matched in a string.

Eg.

  1. Returning the Text Fragment Modules in Web for usage in the URL below, for matching and scrolling to that part of the page:
    https://blog.chromium.org/2019/12/chrome-80-content-indexing-es-modules.html#:~:text=Modules%20in%20Web
    Screenshot 2023-10-15 at 22 29 35

  2. Returning the Text Fragment details%20about%20the-,API,-%2C%20see%20Experimenting%20with for usage in the URL below, for matching a specific instance of API in the page:
    https://blog.chromium.org/2019/12/chrome-80-content-indexing-es-modules.html#:~:text=details%20about%20the-,API,-%2C%20see%20Experimenting%20with
    Screenshot 2023-10-15 at 22 32 50

Ideas for implementation can be inspired by the doGenerateFragment function in fragment-generation-utils.js from the text-fragments-polyfill package:

https://github.com/GoogleChromeLabs/text-fragments-polyfill//blob/e5252cb6eba768cdc4166b27cbe8080fc67f37c7/src/fragment-generation-utils.js#L145-L243

There is a case that cannot be resolved

const textToHighlight = "This is C++ some text to highlight."
const searchWords = ["This", "i", "C++"];

==ใ€‹

SyntaxError
Invalid regular expression: /C++/: Nothing to repeat
eval
https://n9544l.csb.app/node_modules/highlight-words-core/dist/index.js:180:18
defaultFindChunks
https://n9544l.csb.app/node_modules/highlight-words-core/dist/index.js:173:5
findAll
https://n9544l.csb.app/node_modules/highlight-words-core/dist/index.js:112:16
$csb$eval
/src/index.js:5:23
2 |
3 | const textToHighlight = "This is C++ some text to highlight."
4 | const searchWords = ["This", "i", "C++"];

5 | const chunks = findAll({
| ^
6 | caseSensitive: false,
7 | searchWords,
8 | textToHighlight

smart truncate

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.

Error when searchWords is immutable?

I'm not sure why yet, but if searchWords is immutable, findAll is failing.

Error: The sort method cannot be invoked on an Immutable data structure.

Looks like it's this sort:

.sort((first, second) => first.start - second.start)

The content of chunks is immutable, and the objects inside as well.

But I don't see why/when searchWords gets modified.
It's used only in findChunks, and a new array is returned so I don't understand :/

I'll try to find the source later, but I'm creating the ticket in case you have an idea. FWIW, I'm using seamless-immutable.

Ignore markup while searching

I noticed that package would mess up a string if I pass in text that has markup in it. I needed it to ignore anchor tags, so that it would not mess up the href.

I was able to solve it by modifying the findChunks method

from

var regex = new RegExp(searchWord, 'gi');

to

var regex = new RegExp('('+searchWord+')(?![^<]*>)', 'gi');

Would this be something you want to include in this package? Right now im using my own fork.
I could submit a PR for this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.