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

@tplx/core

v0.0.5

Published

Core template parsing and formatting logic for tplx

Readme

@tplx/core

Core template parsing and formatting logic for tplx.

This package contains the foundational JavaScript API for:

  • Parsing templates with customizable delimiters
  • Extracting input placeholders from templates
  • Formatting templates with provided values
  • Reading template files with front matter support

Installation

npm i @tplx/core

Usage

Basic Template Formatting

import * as tplx from '@tplx/core'

const templateContent = 'Hello, {name}! Welcome to {location}.'
const result = tplx.format(templateContent, { name: 'World', location: 'tplx' })
console.log(result) // "Hello, World! Welcome to tplx."

Extracting Template Inputs

import * as tplx from '@tplx/core'

const templateContent = 'Hello, {name}! Welcome to {location}.'
const inputs = tplx.getInputs(templateContent)
console.log(inputs) // ['name', 'location']

Reading Template Files

import * as tplx from '@tplx/core'

// Asynchronous
const template = await tplx.read('greeting.md')
console.log(template.content) // Template content without front matter
console.log(template.inputs) // Array of required inputs
console.log(template.options) // Options from front matter

// Synchronous
import * as tplx from '@tplx/core'
const template = tplx.readSync('greeting.md')

Custom Delimiters

import * as tplx from '@tplx/core'

const delimiters = { open: '<<', close: '>>' }
const templateContent = 'Hello, <<name>>!'

const inputs = tplx.getInputs(templateContent, delimiters)
const result = tplx.format(templateContent, { name: 'World' }, { delimiters })
console.log(result) // "Hello, World!"

Front Matter Support

Templates can include YAML front matter with tplx configuration:

---
tplx:
  delimiters:
    open: '<<'
    close: '>>'
  strict: false
---

Hello, <<name>>!

When using read() or readSync(), the front matter is automatically parsed and options are extracted.

API

format(templateContent, values, options)

Formats a template string with the provided values.

Parameters:

  • templateContent (string): The template string to format
  • values (object): Object containing values for template placeholders
  • options (object, optional):
    • delimiters (object): Custom delimiters with open and close properties (default: { open: '{', close: '}' })
    • strict (boolean): Whether to throw an error on missing values (default: false)

Returns: String with placeholders replaced by values

Example:

tplx.format('Hello, {name}!', { name: 'World' })
// "Hello, World!"

tplx.format(
  'Hello, <<name>>!',
  { name: 'World' },
  {
    delimiters: { open: '<<', close: '>>' },
  },
)
// "Hello, World!"

getInputs(templateContent, delimiters)

Extracts input placeholder names from a template string.

Parameters:

  • templateContent (string): The template string to parse
  • delimiters (object, optional): Custom delimiters with open and close properties (default: { open: '{', close: '}' })

Returns: Array of unique placeholder names

Example:

tplx.getInputs('Hello, {name}! Welcome to {location}.')
// ['name', 'location']

tplx.getInputs('Hello, <<name>>!', { open: '<<', close: '>>' })
// ['name']

read(templateFileName, options)

Asynchronously reads and parses a template file, extracting content, inputs, and front matter options.

Parameters:

  • templateFileName (string): Path to the template file
  • options (object, optional, overrides front matter):
    • title (string): Title for the template
    • description (string): Description for the template
    • delimiters (object): Custom delimiters
    • strict (boolean): Whether to error on missing values

Returns: Promise resolving to an object with:

  • content (string): Template content without front matter
  • inputs (array): Array of required input names
  • options (object): Merged options from front matter and parameters

Example:

const template = await tplx.read('greeting.md')
console.log(template.content) // "Hello, {name}!"
console.log(template.inputs) // ['name']
console.log(template.options) // { delimiters: { open: '{', close: '}' } }

readSync(templateFileName, options)

Synchronously reads and parses a template file. Same as read() but synchronous.

Parameters: Same as read()

Returns: Object with same structure as read()

Example:

const template = tplx.readSync('greeting.md')

Related Packages

  • tplx - CLI tool and JavaScript SDK (re-exports @tplx/core)
  • @tplx/mcp - MCP server for exposing templates as MCP prompts