TextSplitter 🔪
Description
TextSplitter is a lightweight library designed to split text into individual characters, words, or lines. It also includes a new feature to preserve HTML tags when using the splitLines function.
Installation
You can install TextSplitter using npm or yarn:
npm install textsplitter --save
or
yarn add textsplitter
Usage
Import the required functions from 'textsplitter':
import { splitLetters, splitWords, splitLines } from 'textsplitter';
Use the following functions according to your requirements:
-
splitLetters(container, openingtag, closingtag)
: Splits individual characters. -
splitWords(container, openingtag, closingtag)
: Splits words. -
splitLines(container, openingtag, closingtag)
: Splits lines while preserving HTML tags.
React usage example:
useEffect(() => {
const lines = splitLines(ref.current, openingtag, closingtag);
// If not calculating correctly, this might be an issue with fonts that are not ready yet
document.fonts.ready.then(() => {
splitLines(ref.current, openingtag, closingtag);
});
return () => {
// Destroy lines (return to its initial state)
lines.destroy();
};
}, []);
Example
splitLines(document.getElementById("container"), "<span><thisiscoolhuh>", "</thisiscoolhuh></span>");
Output
License
MIT © Niels Reijnders
If you're looking for a lightweight library to split text into characters, words, or lines while preserving HTML tags, TextSplitter is the right choice! Easily integrate it into your projects using npm or yarn.