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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rehype-lodash-template

v0.2.1

Published

Lodash template function for rehype text nodes

Readme

rehype-lodash-template

rehype plugin to replace template strings with values from a dictionary. It is based on the lodash.template function

Contents

What is this?

This package is a unified (rehype) plugin to replace template strings with a values from a provided dictionary. It looks through all strings in the document (attribute values and text nodes) and replaces template strings according to the replacement map. It is based on the lodash.template where you can find more about how it works internally.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that replaces template strings to values from the passed dictionary.

When should I use this?

This plugin is useful to create small templates from HTML code.

It works on the abstract syntax tree level rather on the text level and can potentially perform smarter replacements in the future but is also limited by that.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install rehype-lodash-template

In Deno with esm.sh:

import rehypeLodashTemplate from 'https://esm.sh/[email protected]'

In browsers with esm.sh:

<script type="module">
  import rehypeLodashTemplate from 'https://esm.sh/[email protected]?bundle'
</script>

Use

Say we have the following file example.html:

<h1 id=${id} title="${title}">
  ${ index }. <%= caption %>
</h1>

And our module example.js looks as follows:

import { read } from 'to-vfile'
import { rehype } from 'rehype'
import rehypeLodashTemplate from 'rehype-lodash-template'

const file = await rehype()
  .data('settings', { fragment: true })
  .use(rehypeLodashTemplate, {
    values: {
      id: 'chapter-1',
      title: 'First chapter',
      index: 1,
      caption: 'Chapter 1',
    },
  })
  .process(await read('example.html'))

console.log(String(file))
}

Now, running node example.js yields:

<h1 id="chapter-1" title="First chapter">
  1. Chapter 1
</h1>

API

This package exports no identifiers. The default export is rehypeTemplate.

unified().use(rehypeLodashTemplate[, options])

Replaces template strings with values from a dictionary.

options

Configuration (optional). Although, if you don't pass the values option with a replacement map, the plugin won't function.

options.values

A dictionary to with replacement values. It is passed unchanged to template functions compiled by the lodash.template. It will support nested structures as Lodash does out of the box.

Default is {} (an empty object).

options.templateSettings

Options passed directly to the lodash.template. See the structure and details in Lodash documentation. This object is not processed by the plugin but passed directly.

Default is undefined.

Types

This package is typed with TypeScript. It exports Options type, which specifies the interface of the accepted options.

Compatibility

Compatibility tested with Node.js v12.20 – v19.4

Security

Use of rehype-lodash-template can open you up to a cross-site scripting (XSS) attack if you pass user provided content in properties or content without proper escaping it by a mechanism provided by the lodash.template function.

License

MIT © Viktor Yakubiv