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 🙏

© 2026 – Pkg Stats / Ryan Hefner

pte-interpolation-core

v1.3.1

Published

Framework-agnostic utilities for Portable Text variable interpolation

Downloads

663

Readme

pte-interpolation-core

npm version

Demo

Framework-agnostic utilities for Portable Text variable interpolation. Extract variable keys from PTE blocks and resolve them to plain strings - with zero dependencies. Use this package when you need plain string output without a React dependency: email templates, PDF generation, Node.js scripts, SMS or push notifications, or any non-React framework (Vue, Svelte, Angular, etc.). For React rendering with rich text output, use pte-interpolation-react instead - it re-exports everything from this package.

Part of sanity-pte-interpolation. For adding variable picker inline blocks to Sanity Studio, see sanity-plugin-pte-interpolation. For React rendering with rich text output, see pte-interpolation-react.

Install

npm install pte-interpolation-core

No peer dependencies required.

Usage

Extract variable keys

extractVariableKeys returns the unique variable keys found in an array of PTE blocks, in first-occurrence order:

import {extractVariableKeys} from 'pte-interpolation-core'

const keys = extractVariableKeys(blocks)
// ['firstName', 'vouchersRemaining']

Interpolate to a plain string

interpolateToString resolves PTE blocks to a plain string, substituting variable blocks with the provided values:

import {interpolateToString} from 'pte-interpolation-core'

const text = interpolateToString(blocks, {
  firstName: 'Sarah',
  vouchersRemaining: '3',
})
// "Hi, Sarah! You have 3 vouchers remaining."

Custom fallback for missing values

By default, unresolved variables render as {variableKey} (e.g. {firstName}). Provide a fallback function to customise this:

const text = interpolateToString(blocks, {}, (variableKey) => `[${variableKey}]`)
// "Hi, [firstName]! You have [vouchersRemaining] vouchers remaining."

Detect missing variables

getMissingVariableKeys returns the variable keys present in PTE content but absent from the provided values map - useful for validating before sending emails, notifications, or other interpolated content:

import {getMissingVariableKeys} from 'pte-interpolation-core'

const missing = getMissingVariableKeys(blocks, {firstName: 'Sarah'})
// ['vouchersRemaining']

Related Packages

This package handles resolution of variable blocks that already exist in Portable Text. To add the variable picker to Sanity Studio's Portable Text Editor, use sanity-plugin-pte-interpolation:

import {defineField} from 'sanity'
import {interpolationVariables} from 'sanity-plugin-pte-interpolation'

defineField({
  name: 'message',
  type: 'array',
  of: [
    interpolationVariables([
      {id: 'firstName', name: 'First name'},
      {id: 'vouchersRemaining', name: 'Vouchers remaining'},
      {id: 'totalVouchers', name: 'Total vouchers'},
      {id: 'expiryDate', name: 'Expiry date'},
    ]),
  ],
})

Data Shape

Variable blocks in stored Portable Text look like this:

{
  "_type": "block",
  "children": [
    {"_type": "span", "text": "Hi, "},
    {"_type": "pteInterpolationVariable", "variableKey": "firstName"},
    {"_type": "span", "text": ","}
  ]
}

The variableKey maps to the id defined in the Studio variable definitions and the keys in the values record passed to interpolateToString.

API Reference

extractVariableKeys(blocks)

Returns the unique variable keys from an array of PTE blocks, in first-occurrence order.

| Parameter | Type | Description | | --------- | ------------------------- | ---------------------------- | | blocks | PortableTextBlockLike[] | Portable Text blocks to scan |

Returns string[].

getMissingVariableKeys(blocks, values)

Returns the variable keys present in PTE content but absent from the provided values map. Keys are deduplicated and returned in first-occurrence order. An empty string "" is considered a provided value.

| Parameter | Type | Description | | --------- | ------------------------- | ----------------------------------- | | blocks | PortableTextBlockLike[] | Portable Text blocks to scan | | values | Record<string, string> | Map of variable IDs to their values |

Returns string[].

interpolateToString(blocks, values, fallback?)

Resolves PTE blocks to a plain string, replacing variable blocks with the corresponding values. Multiple blocks are joined with newlines.

| Parameter | Type | Description | | ---------- | --------------------------------- | ------------------------------------------------------------------------ | | blocks | PortableTextBlockLike[] | Portable Text blocks to resolve | | values | Record<string, string> | Map of variable IDs to their resolved values | | fallback | (variableKey: string) => string | Optional function for unresolved variables (defaults to {variableKey}) |

Returns string.

VARIABLE_TYPE_PREFIX

The constant 'pteInterpolationVariable' - the _type string used for variable inline blocks in stored Portable Text. Exported for advanced use cases.

Types

| Type | Description | | ------------------------------- | ----------------------------------------------------- | | PortableTextBlockLike | Minimal block shape with optional children array | | PortableTextChild | A child node within a block (span or variable) | | PteInterpolationVariableBlock | A variable inline block with variableKey | | InterpolationValues | Record<string, string> - variable ID to value map | | InterpolationFallback | (variableKey: string) => string - fallback function |

License

MIT