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 🙏

© 2025 – Pkg Stats / Ryan Hefner

string-style-html

v1.1.6

Published

Converts a string and a style-instructions string to HTML

Downloads

45

Readme

string-style-html

Converts a string and a simple 'style'-instructions string, to HTML code that has styling tags inserted.
It also HTML-encodes any &, <, and > characters in the string, so they don't interfere with any inserted style tags.

This functionality is used by both vsm-autocomplete and vsm-box.

Specification

This package provides a function f(str, style, extraContent).
All arguments are Strings, and the last two are optional.

  • If style is not given, or a not String, or '', then it returns str unchanged.

    • E.g. f('abcd', '') returns 'abcd'.
  • If style is a String that contains a '<', then it is supposed to hold a styled version of str, with HTML-tags already inserted. In this case it returns style.

    • E.g. f('abcd', 'a<i>bc</i>d') returns 'a<i>bc</i>d'.
  • Else it returns str, with HTML styling tags inserted:

    • If style is the single character 'i', 'b', 's', or 'u', then it applies the italic, bold, subscript, or superscript style resp., to the entire string.
      • E.g. f('abcd', 'i') returns '<i>abcd</i>'.
    • style can also include a range '{startIndex}-{stopIndex}' that says which part of the string should be styled. E.g. 'i0-3' applies italic to the first three characters of str.
      Indexes count from 0, and stopIndex is the location just before where styling stops.
      • E.g. f('abcd', 'i1-3') returns 'a<i>bc</i>d'.
    • style can also include a single index for a one-character range.
      • E.g. f('abcd', 'i2') returns 'ab<i>c</i>d'.
    • style can include multiple styling instructions, separated by a ;.
      (Note: overlapping ranges are handled correctly, see index.test.js).
      • E.g. f('abcd', 'i1;u2-4') returns 'a<i>b</i><sup>cd</sup>'.
  • It html-encodes <, >, and & (also if no style is given).

    • E.g. f('<b&d>', 'b2') returns '&lt;b<b>&amp;</b>d&gt;'.
  • If given an extraContent argument, it adds that extra content to inserted opening-tags.

    • E.g. f('abc', 'i', 'style="pointer-events: none;"') returns '<i style="pointer-events: none;">abc</i>'.

Example Use

const stringStyleHtml = require('string-style-html');

console.dir( stringStyleHtml('cdc2', 'i') );
// => '<i>cdc2</i>'.

console.dir( stringStyleHtml('Ca2+', 'u2-4') );
// => 'Ca<sup>2+</sup>'.

console.dir( stringStyleHtml('HCO3-', 's3;u4') );
// =>'HCO<sub>3</sub><sup>-</sup>'.

console.dir( stringStyleHtml('abc', 'ab<span style="color: #f00;">c</span>') );
// =>'ab<span style="color: #f00;">c</span>'.

License

This project is licensed under the AGPL license - see LICENSE.md.