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-model

v1.0.0

Published

A representational text model

Downloads

10

Readme

Text Model

Transform functionality to move between HTML Elements and a text model.

CircleCI Coverage Status

Hello <a href="place" alt="hey">there</a> <u>person</u>!

becomes

{
  "text": "Hello there person!",
  "blocks": {
    "link": [ { "start": 6, "end": 11, "href": "place", "alt": "hey" } ],
    "emphasis": [ 12, 18 ]
  }
}

Usage

npm install --save text-model

Convert from an Element to a text model representation.

var el = document.querySelector('.some-element');
var model = textModel.fromElement(el);

Convert from a text model representation to an Element.

var el = textModel.toElement(model);
paragraphEl.appendChild(el);

Merge text together -- simple styling tags (like <i>, <b>, <em>) will be merged correctly as well.

var a = textModel.fromElement(document.querySelector('.a'));
var b = textModel.fromElement(document.querySelector('.b'));
var merged = textModel.concat(a, b);
paragraphEl.appendChild(textModel.toElement(merged));

Split text apart -- tags will be split correctly as well.

var model = textModel.fromElement(document.querySelector('.a'));
var splitModels = textModel.split(a, 15);
paragraphEl.appendChild(textModel.toElement(splitModels[0]));
paragraphEl.appendChild(textModel.toElement(splitModels[1]));

Optional Configuration

textModel.updateSameAs({
  'B': 'STRONG',
  'U': 'EM',
  'I': 'EM',
  'H1': 'H2',
  'H3': 'H2',
  'H4': 'H2',
  'H5': 'H2',
  'H6': 'H2',
  'STRIKE': 'DEL'
});

These tags will be converted if they are seen. The defaults are as above, but you can pass in an object of tag names (uppercase) that will merge and overwrite them. To remove a default conversion, simply pass <tagname>: null in the object:

textModel.updateSameAs({
  'B': null, // override default, don't convert <b> to <strong>
  'H1': 'STRONG', // convert ALL headers to <strong>
  'H2': 'STRONG',
  'H3': 'STRONG',
  'H4': 'STRONG',
  'H5': 'STRONG',
  'H6': 'STRONG',
  'SPAN': 'EM', // convert all spans to <em>
  'STRIKE': null // don't convert <strike>. this will remove the tag as <strike> is deprecated in html (and thus removed by text-model)
});

Testing

We use karma and mocha for testing.

npm test

Contribution

Fork the project and submit a PR on a branch that is not named master. We use linting tools and unit tests, which are built constantly using continuous integration. If you find a bug, it would be appreciated if you could also submit a branch with a failing unit test to show your case.