npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-marker.js

v0.3.0

Published

A JavaScript text marking library.

Downloads

10

Readme

Text Marker

A lightweight, zero dependency JavaScript text marking library.

An example of text marking

With text-marker.js, it is possible to mark/highlight text based on character indexes of a string. This is very efficient for database storage.

Installation

npm install text-marker.js

Usage

The string A carpet is an awesome object! can be considered an array of characters:

0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
A  ·  c  a  r  p  e  t  ·  i  s  ·  a  n  ·  a  w  e  s  o  m  e  ·  o  b  j  e  c  t  !

If one would like to mark A, carpet, car and awesome object as seen in the image above, the following indexes need to be used:

[0, 1]     -> A
[2, 8]     -> carpet
[2, 5]     -> car
[15, 29]   -> awesome object

Note the order of car and carpet. Because car is marked after carpet, car is layered on top.

The markings in the image above can be produced with the following code:

const text = 'A carpet is an awesome object!';

const textMarker = new TextMarker(text);
textMarker.addMarking([0, 1], '#f00');
textMarker.addMarking([2, 8], 'yellow');
textMarker.addMarking([2, 5], 'rgb(0, 255, 0)');
textMarker.addMarking([15, 29], 'rgba(0, 0, 255, .65)');

const formattedText = textMarker.getText();

document.getElementById('example-text').write(formattedText);

The HTML that the code would generate:

<mark class="tm-mark" style="--tm-color: #f00">A</mark> <mark class="tm-mark" style="--tm-color: yellow"><mark class="tm-mark" style="--tm-color: rgb(0, 255, 0)">car</mark>pet</mark> is an <mark class="tm-mark" style="--tm-color: rgba(0, 0, 255, .65)">awesome object</mark>!

And with the following CSS, the colors could be set:

mark {
    background-color: var(--tm-color);
}

Of course this can be styled any way possible with CSS.

In case of a layered marking where the marking order is switched up, the marking would of course differ:

An example of text marking with wrong order

The following CSS is used to achieve this effect:

mark {
    background-color: var(--tm-color);
    display: inline-block;
    padding: 4px 2px;
}

mark mark {
    background-color: unset;
    border-bottom-width: 3px;
    border-bottom-style: solid;
    border-bottom-color: var(--tm-color);
    padding: 0;
}

Contributing

Contributions are welcome. Feel free to create an issue or send a pull request.

Security

Found a security vulnerability? Email security at pedzed.com.

NOTE: This library does not protect against XSS attacks. Be sure to pass colors that are safe.