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

v1.1.1

Published

PostHTML plugin for transform custom tags to BEM methodology

Downloads

4

Readme

Bike plugin

NPM Build

This plugin transform custom tags to BEM-like HTML

Install

npm i -D posthtml-bike

Usage

const { readFileSync } = require('fs');
const posthtml = require('posthtml');
const bike = require('posthtml-bike');

const html = readFileSync('index.html');

posthtml([ bike() ]).process(html).then((result) => console.log(result.html));

Example

Default

<component name="example">
    <header></header>
    <main></main>
    <footer></footer>
</component>

Transformed to:

<section class="example">
    <header class="example__header"></header>
    <article class="example__main"></article>
    <footer class="example__footer"></footer>
</section>

With mods

<component name="example" mod-theme="dark" mod-active>
    <header></header>
    <main mod-hidden></main>
    <footer></footer>
</component>

Transformed to:

<section class="example example_theme_dark example_active">
    <header class="example__header"></header>
    <article class="example__main example__main_hidden"></article>
    <footer class="example__footer"></footer>
</section>

With tag attr

<component name="button" tag="button">
    <main tag="span"></main>
</component>

Transformed to:

<button class="button">
    <span class="button__main"></span>
</button>

Options

{
    /**
    * Component root tag name
    * @default
    */
    tag: 'component',
    
    /**
    * Default component root HTML tag
    * @default
    */
    replaceComponentTag: 'section',
    
    /**
    * Default element HTML tag
    * @default
    */
    replaceElemTag: 'div',
    
    /**
    * Skip HTML tags list
    * @default
    */
    skipTags: ['b', 'strong', 'i', 'span', 'div', 'section'],
    
    /**
    * These elements will be replaced to defined HTML tags
    * @default
    */
    autoTags: {
        header: 'header',
        main: 'article',
        footer: 'footer',
        title: 'h2',
        list: 'ul',
        'list-item': 'li',
        link: 'a'
    },
    
    /**
    * Config for generated custom element name by HTML tag
    * @default
    */
    autoClasses: {
      li: () => {}, // Generate element name for `li` tag
      a: () => {}, // Generate element name for `a` tag
    },
   
    /**
    * Config for process styles in component 
    * @default
    */
    postcss: false,
}

Use with postcss-bike

Example for use with postcss-bike

<style type="text/postcss">
    @component app {
      display: flex;
      flex-flow: column nowrap;
      justify-content: space-between;
    
      @elem header {
        flex: 0 0 40px;
      }
    }
</style>
<component name="app" mod-theme="dark"></component>

Transformed to:

<style type="text/css">
    .app {
      display: flex;
      flex-flow: column nowrap;
      justify-content: space-between;
    }
    .app__header {
      flex: 0 0 40px;
    }
</style>
<section class="app app_theme_dark"></section>

Options

{
  postcss: {
    match: 'text/postcss', // Match `style` tag by type
    plugins: [], // Postcss plugins
    process: (css, node) => { // Save processed css function
      node.attrs.type = 'text/css';
      node.content = ['\n', css];
      return node;
    }
  }
}

Example for save all components styles in one file:

import { appendFileSync } from 'fs';
import gulp from 'gulp';
import gulpPosthtml from 'gulp-posthtml';
import gulpClean from 'gulp-clean';

import postCssBike from 'postcss-bike';

gulp.task('clean', () => (
  gulp.src('examples/dist/components.css').pipe(gulpClean())
));

gulp.task('html', ['clean'], () => {
  gulp.src('/components/*.html')
    .pipe(gulpPosthtml([
      bike({
        postcss: {
          match: 'text/postcss',
          plugins: [ postCssBike() ],
          appendTo: '/dist/components.css',
          process(css, node, options) {
            appendFileSync(options.postcss.appendTo, css);
            node.tag = false;
            node.content = [''];
            return node;
          }
        },
      }),
    ]))
    .pipe(gulp.dest('/dist'))
});

Auto classes

Example config for li tag, result class - ${componentName}__${parentElementName}-item

{
  li: ({ component }) => (`${component.elem}-item`)
}
<component name="example">
    <list>
      <li>Item 1</li>
      <li>Item 2</li>
    </list>
</component>

Transformed to:

<section class="example">
    <ul class="example__list">
      <li class="example__list-item">Item 1</li>
      <li class="example__list-item">Item 2</li>
    </ul>
</section>

Auto class function args:

  • parent - Parent node object
    • component - { name, elem, parent } Custom item with plugin info
  • options - Plugin options

For example:

import bike, { AUTO_TAGS, AUTO_CLASSES } from 'posthtml-bike';

const options = {
  autoTags: {
    ...AUTO_TAGS,
    'my-tag': 'p'
  },
  autoClasses: {
    ...AUTO_CLASSES,
    i: ({ component }) => (`${component.elem}-icon`)
  }
}