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

rehype-slots

v0.3.2

Published

[**rehype**][rehype] plugin to replace slot elements in an HTML fragment.

Downloads

43

Readme

rehype-slots

rehype plugin to replace slot elements in an HTML fragment.

Replacement value is provided as a HAST tree

Installation

This package is ESM only.

Install the rehype-slots npm module:

npm install rehype-slots
import rehypeSlots from "rehype-slots";

Example Usage

Take this HTML fragment which contains <slot> elements:

<h1>
  <slot name="title"></slot>
</h1>
<article>
  <div>
    <slot name="quote"></slot>
  </div>
  <div>
    <slot name="author">Anonymous</slot>
  </div>
</article>

Provide the slot values to be inserted, in this case using hastscript to create the HAST tree:

import { h } from "hastscript";

const values = {
  // a string that will create a text node
  title: "A Tale Of Two Cities",

  // a HAST tree that will be inserted
  quote: h("p", "It was the best of times..."),
};

In the following code, rehype-slots will traverse the HTML tree and replace each <slot> element with the value that was provided for it, based on the name attribute.

If a <slot> element is encountered but no value was provided for it, then the element's contents will be used as a default value.

import { unified } from "unified";
import rehypeParse from "rehype-parse";
import rehypeFormat from "rehype-format";
import rehypeStringify from "rehype-stringify";
import rehypeSlots from "rehype-slots";

unified()
  .use(rehypeParse, { fragment: true })
  .use(rehypeSlots, { values }) // <-- attach the rehype-slots plugin
  .use(rehypeFormat)
  .use(rehypeStringify)
  .process(input)
  .then((output) => {
    console.log(output.contents);
  });

Output:

<h1>A Tale Of Two Cities</h1>
<article>
  <div>
    <p>It was the best of times...</p>
  </div>
  <div>Anonymous</div>
</article>

Notice in the output:

  • the title slot was replaced with a string
  • the quote slot was replaced with an HTML tree (HAST)
  • no value was provided for the author slot, so the slot element's contents were used as the default value.

API

rehype().use(rehypeSlots[, options])

Replace slot elements with the provided values.

Basic options
options.values

Object containing slot names and slot values. (object, default: {})

Allowed value types:

  • String (will be converted to a HAST text node)
  • HAST node
  • HAST root (document, fragment or template)
  • Array of HAST nodes
  • Function that returns any of the above. The function will be called with the slot name as the first argument.
options.fallbackBehavior

Defines the behavior when a slot is not found in the values object. It can be one of the following:

  • "unwrap": Replace the slot with its own children, i.e. unwrap it. This is the default.
  • "remove": Remove the slot element entirely.
  • "keep": Do nothing, keep the slot element as it is.
options.replacer

A function that will be called for each slot whose name was not found in the values object. It will be called with (slotName, slotElement) and can return a value to replace the slot with, or return undefined to use the fallback behavior.

Advanced options
options.slotTagName

Tag name of the element to replace, if you want to use a different element than slot which is the default.

options.slotNameAttribute

Name of the attribute on the slot element that holds the slot's name. The default is name.

Contributing

Submit an issue if you find a bug or want to request a feature. Pull requests are welcome.

See Developing for more information on how to develop this project.

License

MIT © Marek Zaluski