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

posthtml-extra-attributes

v3.0.0

Published

Add new attributes to elements in your HTML.

Downloads

34,595

Readme

Version Build License Downloads

Introduction

This PostHTML plugin can add extra attributes to elements in your HTML.

Features:

  • supports a variety of CSS-like selectors
  • appends class names to existing ones
  • does not overwrite existing attributes

Installation

npm i posthtml posthtml-extra-attributes

Usage

import posthtml from 'posthtml'
import addAttributes from 'posthtml-extra-attributes'

posthtml([
  addAttributes({
    attributes: {
      div: {
        class: 'new',
        id: 'new'
      }
    }
  })
])
  .process('<div class="test">Test</div>')
  .then(result => console.log(result.html))

// <div class="test new" id="new">Test</div>

Options

attributes

Type: Object
Default: {}

An object defining which elements should get what attributes.

Elements can be any posthtml-match-helper selector.

Select by tag

Add id="new" to all <div> tags:

const attributes = {
  div: {
    id: 'new'
  },
}

Select by class

Add editable="true" to all elements with a test class:

const attributes = {
  '.test': {
    'editable': true
  },
}

Select by id

Add class="new" to any element with id="test":

const attributes = {
  '#test': {
    class: 'new'
  },
}

If the element already has a class attribute, the value will be appended:

<div id="test" class="test">Test</div>

... will result in:

<div id="test" class="test new">Test</div>

Select by attribute

Adds aria-roledescription="slide" to all elements with a role attribute:

const attributes = {
  '[role]': {
    'aria-roledescription': 'slide'
  },
}

Select multiple tags

Add multiple attributes to multiple elements in one go:

const attributes = {
  'div, p': {
    class: 'test',
  },
  'div[role=alert], section.alert': {
    class: 'alert'
  },
}

overwrite

Type: Boolean
Default: false

By default, the plugin will not overwrite existing attribute values.

Set this option to true to enable attribute value overwriting:

posthtml([
  addAttributes({
    attributes: {
      div: {
        id: 'new'
      }
    },
    overwrite: true
  })
])
  .process('<div id="test">Test</div>')
  .then(result => console.log(result.html))

// <div id="new">Test</div>