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

css2html

v2.1.0

Published

The library for converting CSS to HTML

Downloads

61

Readme

Special thanks to the author of the idea akopyl.

Installation

Attention! This library works with the APIs provided by node.js .

npm i css2html
yarn add css2html
import { CssToHtml } from 'css2html';

let result = new CssToHtml({ css: 'div{}' });

console.log(result.outputHTML);

What is this?

It converts this:

section#some-id {
  /* {{ This is text inside }} */
  --attr-title: 'Title';

  background: red;
  color: aliceblue;
}
section#some-id header[data-attribute='v'] {
  /* {{ This is the header text }} */

  color: blue;
}
section#some-id span {
  /* 
    {{ Text of span }} 
    Text after 
  */

  color: peru;
}

To this:

<section id="some-id" title="Title">
  This is text inside
  <header data-attribute="v">This is the header text</header>
  <span>Text of span</span>
  Text after
</section>

How to use this?

Elements

You can create an element via selector:

div.classname#id[attr-1][attr-2='v'] {
  /* None of the parts of a selector are mandatory */
  /* But at least something needs to be left */
}
<!-- Result -->
<div id="id" class="classname" attr-1 attr-2="v"></div>

Nesting is supported:

div {
}
div span {
}
<div>
  <span></span>
</div>

If you want to add styles but not add elements (that is, so that some selectors are ignored), add one of the following to the selector:

  • Pseudo-class
  • Pseudo-element
  • One of these selectors: *, +, >, ||, |, ~
  • Or wrap it in an @at-rule

Example - these selectors will be ignored:

> div.classname#id[attr-1][attr-2='v'] {
}
div::before {
  /* Yes, and this one too */
}
div:not(:has(span)) {
  /* And this one too! */
}
@container (width > 1440px) {
  div[data-a='This element will be ignored too'] {
  }
}

Text and attributes

Attributes can be set via a selector (it can be useful for styling), or you can use a custom property:

/* In a selector */
a[title='Title!'] {
  /* Specific attribute */
  --attr-href: './index.html';
  --data-attribute: 'Value';

  /* And massively! */
  --attrs: 'target="_self" rel="noopener"';
}
<a
  title="Title!"
  data-attribute="Value"
  href="./index.html"
  target="_self"
  rel="noopener"
>
</a>

You can add text inside the tag using comments or variables:

/* The old way is not recommended for use */
div {
  --text-before: 'The battle ';
  --text: 'continues';
  --text-after: ' again';
}
/* New way, recommended for use. Curly braces are required! */
section {
  /* 
    The battle
    {{ continues }}
    again
  */
}
The battle <div>continues</div> again

The battle
<section> continues </section>
again

API

The very minimum to run looks like this:

// This code outputs to the terminal/console the result of processing the simplest CSS from the single tag.
import { CssToHtml } from 'css2html';

let result = new CssToHtml({ css: 'div{}' });

console.log(result.outputHTML);

Writing to a file

To write in a file, add the write parameter: (Attention! The entire file will be overwritten)

new CssToHtml({
  ...,
  write: {
    in: "your_path_to_html_file",
  },
})

Overwriting a part of a file

Using the after and/or before parameters, you will not overwrite the entire file, but specify the area to be overwritten. You can omit one of these parameters or not specify them at all.

Without after and before parameters:

new CssToHtml({
  ...,
  write: {
    in: "your_path_to_html_file",
  },
})
<some-html-content>
  <div>Your content from CSS</div>
</some-html-content>

<!-- to... -->

<div>Your content from CSS</div>

With after and before parameters:

new CssToHtml({
  ...,
  write: {
    ...,
    after: '<some-html-content>',
    before: '</some-html-content>',
  },
})
<some-html-content>
  <div>Your content from CSS</div>
</some-html-content>

<!-- Without changes -->

<some-html-content>
  <div>Your content from CSS</div>
</some-html-content>

Formatting

Before giving you html, it is formatted by the html-format library. You can either enable or disable formatting:

new CssToHtml({
  format: true, // default value
});

If you find a bug, please create an issue here.

If this project was useful to you, you can give it a ★ in repository.