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

remove-style

v1.0.0

Published

Remove inline styles from html file

Downloads

14

Readme

remove-style

Remove inline styles from html files

Why Remove Inline Styles

Inlining styles in HTML is an easy way to style HTML elements but not a good practice especially, if there are other better approaches like using a separate CSS file or styling inside the HTML style tag. Have you ever tried changing the theme of your website after inlining your styles in the HTML code?

  • Inline styling makes code maintainability very difficult.
  • Inline styling does not allow reusing style rules for different elements.
  • It also increases the size of the HTML file which makes page loading very slow.
  • It also doesn't make you enjoy the benefits of caching by the browser.
  • and many more... Read these articles to get more insight about inline styling:

Why You May Choose Remove-Style Package

Below examples demonstrates why you should use remove-style

File1.html

    <!-- HTML with inline styling -->
    <div style="color:red;font-size:10px" class="classname1 classname2">My first text content</div>
    <div>My second text content</div>
    <div style="color:red;font-size:20px" class="classname1">My third text content</div>
    <div style="color:blue;font-size:10px">My fourth text content</div> 

File2.html

    <!-- HTML with inline styling -->
    <div style="font-size:10px" class="classname1 classname2">My first text content</div>
    <div style="color:blue;" class="classname1">My second text content</div> 

After using remove-style

File1.html

    <!-- HTML after using remove-style -->
    <div class="classname1 classname2 rs-a rs-b">My first text content</div>
    <div> My second text content</div>
    <div class="classname1 rs-a rs-c">My third text content</div>
    <div class="rs-d rs-b">My fourth text content</div>  

File2.html

    <!-- HTML after using remove-style -->
    <div class="classname1 classname2 rs-b">My first text content</div>
    <div class="classname1 rs-d">My second text content</div>  

CSS generated by remove-style

    /* Remove-Style generated css */
    .rs-a{
        color:red;
    }
    .rs-b{
        font-size:10px;
    }
    .rs-c{
        font-size:20px;
    }
    .rs-d{
        color:blue;
    }

What Does Remove-Style Gives You

  • Small file size for both HTML and CSS
  • No inline styling in your production files
  • Style rules usability. Remove-Style ensures every CSS rule is set once throughout your HTML files.
  • No switching forth and back from CSS files to HTML files. It allows you to inline all the style rules you want in the HTML file then, it takes care of removing them for you.

How To Use

First install Remove-Style from NPM.

npm install remove-style

Usage 1 - Passing HTML strings as argument

You can pass HTML strings as argument to Remove-Style to remove inline styles. In that case, Remove-Style returns the removed styles' HTML strings and the CSS string.

    var rs = require("remove-style");
    var result = rs({
        
        //Put all your HTML strings here
        htmlStrings:["your first html string","your second html string","your third html string"]
    });

    console.log(result.htmlOutputs);//["your first html string","your second html string","your third html string"]
    console.log(result.styleSheet);//CSS string generated by Remove-Style

Usage 2 - Passing files or a directory as argument

You can pass files or a directory as argument to Remove-Style to remove inline styles from all those files or all the files in the directory. Stylesheet is always generated once (universal) for all the files. This ensures each style rule can be reused by other files. In the case that you want to pass files or a directory as argument:

  • You have to set the CSS destination where the final style sheet should be written to. If a CSS destination is not set then, the stylesheet will be returned.
  • You have to choose whether the files you provide should be overwritten with the removed styles' HTML. By default, Remove-Styles will overwrite the files you provide with the removed styles' HTML. Set overWriteFiles to false to prevent that behaviour. If overWriteFiles is set to false then, Remove-Style returns the removed styles' HTML strings.

With files as argument

    var rs = require("remove-style");
    var result = rs({

        //Put the path to all your HTML files here
        filePaths:["filename1", "filename2", "filename13"],

        //Choose whether to overwrite files or not
        overWriteFiles:false||true||undefined,

        //Set a the style sheet destination or ignore to get it as string
        cssDestination:"path to style sheet destination"||undefined
    });

    console.log(result.htmlOutputs);//["filename1 html string","filename2 html string","filename3 html string"]||[]
    console.log(result.styleSheet);//CSS string generated by Remove-Style or empty string ""

With a directory as argument

    var rs = require("remove-style");
    var result = rs({

        //Place your directory path here
        dirPath:"directory path",

        //Choose whether to overwrite files or not
        overWriteFiles:false||true||undefined,

        //Set a the style sheet destination or ignore to get it as string
        cssDestination:"path to style sheet destination"||undefined
    });

    console.log(result.htmlOutputs);//["filename1 html string","filename2 html string","filename3 html string"]||[]
    console.log(result.styleSheet);//CSS string generated by Remove-Style or empty string ""
    

Order of preference

In the case when all arguments are given together, HTML strings takes the higher preference. A directory takes the lowest preference if passed as argument with any other possible argument.

A Note On the "rs-" Prefix

In order to prevent class names collision with existing class names, Remove-Style prefix class names with "rs-".

A Note On the Class Names Used by Remove-Style

Remove-Style can produce over 13 million distinct or unique class names. You can do the maths:

  • 62 Permutation 4
  • 62 Permutation 3
  • 62 Permutation 2
  • 62 Permutation 1

That is to say; Remove-Style produces the class names from 62 characters, alpha-numerals (both lowercase and uppercase). Class names are generated starting from single characters to maximum of 4 characters (excludeing the "rs-" prefix). So in the worst case, 7 characters is used for each class name.

MIT Lincensed