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

reshape-content

v0.3.0

Published

flexible content transform for reshape

Downloads

18

Readme

Reshape Content

npm tests dependencies coverage

Flexible content transform for reshape

Note: This project is in early development, and versioning is a little different. Read this for more details.

Why Should You Care?

Rather than having a separate plugin for each kind of content transform you want to be able to do, why not just have one? Parse natural language, markdown, or whatever else you want with a minimalistic and simple interface 🍻

Install

npm i reshape-content --save

Note: This project is compatible with node v6+ only

Usage

Start with some html you want to transform in some way. Add attributes of your choosing to an element that has contents you want to transform, and they will be transformed in that order.

<p windoge>Please use windows 98</p>

Now pass in an object to reshape-content. Each key in the object represents an attribute that will be searched for in the html. The value is a function that will get that element's contents as a string, and replace the contents with whatever string is returned from the function.

const content = require('reshape-content')({
  windoge: (str) => str.replace(/windows/g, 'winDOGE')
})

reshape({ plugins: content })
  .process(html)
  .then((res) => res.output())

The plugin will remove the custom attributes from the element and replace its contents with your transformed version. Wow!

<p>Please use winDOGE 98</p>

If you return an A+ compliant promise from your content function, it will resolve and work in your templates as well.

You can use external libraries for this as well, no problem. Just make sure you are passing in a function that takes a string and returns a string. You might have to wrap the library function if it doesn't behave like this, but it will work with anything that transforms content.

Examples

Markdown

<p md>Wow, it's **Markdown**!</p>
const markdown = require('markdown-it')(/* options */)
const content = require('reshape-content')({
  md: (md) => markdown.render(md)
})

reshape({ plugins: content })
  .process(html)
  .then((res) => res.output())
<p>Wow, it's <strong>Markdown</strong>!</p>

PostCSS

<style postcss>
  .test
    text-transform: uppercase;

    &__hello
      color: red;

    &__world
      color: blue;
</style>
const postcss = require('postcss')([ require('postcss-nested')() ])
const options = { parser: require('sugarss'), map: false }

const content = require('reshape-content')({
  postcss: (css) => postcss.process(css, options).css
})

reshape({ plugins: content })
  .process(html)
  .then((res) => res.output())
<style>
  .test {
    text-transform: uppercase;
  }

  .test__hello {
    color: red;
  }

  .test__world {
    color: blue;
  }
</style>

Babel

<script babel>
  const hello = 'Hello World!'
  let greeter = {
    greet (msg) { alert (msg) }
  }
  greeter.greet(hello)
</script>
const babel = require('babel-core')
const options = { presets: ["es2015"], sourceMaps: false }

const content = require('reshape-content')({
  babel: (js) => babel.transform(js, options).code
})

reshape({ plugins: content })
  .process(html)
  .then((res) => res.output())
<script>
  'use strict';
  var hello = "Hello World!";
  var greeter = {
    greet: function greet (msg) {
      alert(msg);
    };
  };
  greeter.greet(hello);
</script>

Return a Promise

<style postcss>
  .test
    text-transform: uppercase;

    &__hello
      color: red;

    &__world
      color: blue;
</style>
const postcss = require('postcss')([ require('postcss-nested')() ])
const options = { parser: require('sugarss'), map: false }

const content = require('reshape-content')({
  postcss: (css) => {
    return postcss.process(css, options).then((res) => res.css)
  }
})

reshape({ plugins: content })
  .process(html)
  .then((res) => res.output())

License & Contributing