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

hastml

v0.1.0

Published

Hasty HTML, XML and SGML walker

Downloads

1,078

Readme

Hasty HTML, XML and SGML walker

npm hastml Build Status codecov

🚶🏻‍♀️ A quick and extremely permissive way to process XML-like inputs.

Installation

yarn add hastml

~or~

npm install --save hastml

API

walk (default)

The walk function accepts two arguments. The text, and a callback.

import walk from 'hastml';

const myHtml = '<title>hello!</title>';

const callback = (match, tagFragment, offset, string, thisTag, stack) => {
  console.log(tagFragment, offset);
};

const output = walk(
  myHtml,
  callback
);

// Console output:
//  => '<', 0
//  => '>', 6
//  => '</', 13
//  => '>', 20

Callback function

The callback function is passed six arguments denoting the state of the walker.

  • match: The text which will be replaced if the callback returns a string value.
  • tagFragment: The part of the HTML tag we're currently stepping over. One of <, >, </, />, <!-- or -->.
  • offset: Numeric index of the current match.
  • string: The full HTML string which is being walked over.
  • thisTag: An object representing the state of the current tag.
  • stack: Array representing the path to the current tag through the document's structure.

If the callback function returns a string value, that string will replace the value of match in the output from walk. Otherwise, no change is made.

Tag objects

Tag objects contain data about tags being processed. What information they contain depends on where the walker is in relation to the tag it represents.

If the walker is at the start of the tag, for instance, it will only contain the tagName, openIndex and an "open" state. If the walker has reached the closing tag, it will contain more indexes, and have changed state.

The tag objects (passed to the callback function as thisTag, and within the stack) can contain the following information:

  • tagName: The tag name, i.e. <html> would give html.
  • state: The current state of the tag.
  • openIndex: The index at which the tag was opened.
  • contentIndex: The index immediately before content of the tag begins. Not included for void elements.
  • closingIndex: The index where the closing part of the tag begins. Not included for void elements.
  • closeIndex: The index at which the tag was closed.

The indexes can be visualised as follows:

openIndex       contentIndex     closeIndex
    ↓                 ↓               ↓
    <span class="text">This span</span>
                                ↑
                          closingIndex

    <img src="//plz-give.cat/random.jpg" />
    ↑                                     ↑
openIndex                           closeIndex

Tag object state

The state property of tag objects indicate where the walker is in relation to the tag. The following states are possible:

  • "open": The opening part of the tag has been found.
  • "content": The tag's content has been walked to.
  • "closing": The closing part of the tag has been found.

These states can be visualised as follows:

"open"           "content"
   ↓                 ↓
   <span class="text">This span</span>
                               ↑
                           "closing"

   <img src="//plz-give.cat/random.jpg" />
   ↑
"open"

Note that there is not currently a "closed" state indicating the tag has no further content or closing tag. This may be added in the future. A tag object with a populated closeIndex and state of either "open" or "content" indicates this state.