Have ideas to improve npm?Join in the discussion! »

    posthtml-safe-class-names

    1.0.6 • Public • Published

    Safe Class Names

    Replace escaped characters in class names and CSS selectors

    Version License Build Downloads

    Introduction

    This plugin replaces escaped characters in class names from your <style> tags and inside class="" attributes with safe characters, that do not need escaping.

    By default, it replaces:

    • \: and \/ with -
    • \% with pc
    • \. with _

    But... why?

    Because I needed a way to use Tailwind CSS variants in my HTML emails 🤷‍♂️

    Escaped characters in CSS selectors or HTML class names are not supported by all email clients (currently Gmail being the biggest one), so you can use this plugin to replace them with safe alternatives.

    Install

    $ npm i posthtml posthtml-safe-class-names
    

    Usage

    Consider example.html:

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        .sm\:w-3\/5 {
          width: 60%;
        }
    
        @media (max-width: 600px) {
          .sm\:w-1\/2 {
            width: 50%;
          }
        }
      </style>
    </head>
    <body>
      <div class="w-3/5 sm:w-1/2">Lorem ipsum</div>
    </body>
    </html>
    const fs = require('fs')
    const posthtml = require('posthtml')
    const safeClassNames = require('posthtml-safe-class-names')
    
    const source = fs.readFileSync('./example.html')
    
    posthtml([
        safeClassNames()
      ])
      .process(source)
      .then(result => fs.writeFileSync('./after.html', result.html))

    Result:

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        .sm-w-3-5 {
          width: 60%;
        }
    
        @media (max-width: 600px) {
          .sm-w-1-2 {
            width: 50%;
          }
        }
      </style>
    </head>
    <body>
      <div class="w-3-5 sm-w-1-2">Lorem ipsum</div>
    </body>
    </html>

    Options

    replacements

    The plugin accepts an options object where you can define character replacement mappings.

    Default:

    {
      ':': '-',
      '\/': '-',
      '%': 'pc',
      '.': '_',
    }

    Besides adding new mappings, you can of course override the default ones.

    Using the same example.html, let's choose to replace \: in our class names with __ instead of -:

    posthtml([
        safeClassNames({
          replacements: {
            ':': '__',
          }
        })
      ])
      .process(source)
      .then(result => fs.writeFileSync('./after.html', result.html))

    Result:

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        .sm__w-3-5 {
          width: 60%;
        }
    
        @media (max-width: 600px) {
          .sm__w-1-2 {
            width: 50%;
          }
        }
      </style>
    </head>
    <body>
      <div class="w-3-5 sm__w-1-2">Lorem ipsum</div>
    </body>
    </html>

    Install

    npm i posthtml-safe-class-names

    DownloadsWeekly Downloads

    1,152

    Version

    1.0.6

    License

    MIT

    Unpacked Size

    16.3 kB

    Total Files

    27

    Last publish

    Collaborators

    • avatar