GithubHelp home page GithubHelp logo

posthtml / posthtml-safe-class-names Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 1.0 2.67 MB

Replace escaped characters in CSS selectors and HTML class names.

License: MIT License

JavaScript 65.88% HTML 32.75% TypeScript 1.37%
posthtml posthtml-plugin escape-char html-emails maizzle tailwindcss replace-escaped-characters css-selectors

posthtml-safe-class-names's Introduction

Safe Class Names

Replace escaped characters in class names and CSS selectors

Version Build License 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 _

See the full list of replacements.

But... why?

So you can use those cool Tailwind CSS selectors in HTML emails. ๐Ÿ˜Ž

Escaped characters in CSS selectors are not currently supported by all email clients, so you can use this plugin to replace them with HTML email-safe alternatives.

Install

npm i posthtml posthtml-safe-class-names

Usage

Consider example.html:

<!DOCTYPE html>
<html>
<head>
  <style>
    .w-3\/5 {
      width: 60%;
    }
    
    .bg-\[\#1da1f1\] {
      background-color: #1da1f1;
    }

    @media (max-width: 600px) {
      .sm\:w-1\/2 {
        width: 50%;
      }
    }
  </style>
</head>
<body>
  <div class="w-3/5 sm:w-1/2 bg-[#1da1f1]">Lorem ipsum</div>
</body>
</html>
import posthtml from 'posthtml'
import {readFileSync, writeFileSync} from 'node:fs'
import safeClassNames from 'posthtml-safe-class-names'

const source = 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%;
    }

    .bg-_1da1f1 {
      background-color: #1da1f1;
    }

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

Options

replacements

Type: Object
Default: see list

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

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

See the full list of replacements.

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

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

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

Result:

<!DOCTYPE html>
<html>
<head>
  <style>
    .sm__w-3-5 {
      width: 60%;
    }

    .bg-_1da1f1 {
      background-color: #1da1f1;
    }

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

ignored

Type: Array
Default: [{heads: '{{', tails: '}}'}, {heads: '{{{', tails: '}}}'}]

An array of objects each containing heads/tails strings that mark the start and end of a class name to ignore. If a class name matches a pattern defined here, it will not be processed.

posthtml([
    safeClassNames({
      ignored: [
        {heads: '[[', tails: ']]'},
      ]
    })
  ])
  .process('<div class="foo:bar [[ biz ]]">')
  .then(result => console.log(result.html))

  // <div class="foo-bar [[ biz ]]">

posthtml-safe-class-names's People

Contributors

cossssmin avatar dependabot-preview[bot] avatar dependabot[bot] avatar j-mori avatar scrum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

posthtml-safe-class-names's Issues

Fails to replace some special characters

This is mostly an issue with the regex we do for PostCSS, which affects replacements in the <style> tag.

For example, this will fail:

.w-\[600px\] {
  width: 600px;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.