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

usfmtools

v1.1.0

Published

Parse .usfm files

Readme

usfmtools-js

A TypeScript/JavaScript parser for USFM (Unified Standard Format Markers), the standard encoding format for Scripture translations.

Description

usfmtools-js parses USFM text into an abstract syntax tree of typed marker objects that you can traverse, query, validate, and serialize back to USFM.

Installation

npm install usfmtools

Requirements

We targeted ES2020 and Node.js 14+.

Contributing

Yes please! A couple things would be very helpful

  • Testing: Because I can't test every single possible USFM document in existence. If you find something that doesn't look right in the parsing or rendering please submit an issue.
  • Adding support for other markers to the parser. There are still plenty of things in the USFM spec that aren't implemented.

Usage

Parsing a USFM file

import { USFMParser, CMarker, VMarker, TextBlock } from "usfmtools";
import fs from "fs";

const parser = new USFMParser();
const usfm = fs.readFileSync("01-GEN.usfm", "utf8");
const doc = parser.parseFromString(usfm);

Querying markers

getChildMarkers<T>() recursively finds all descendants of a given marker type:

// Get all chapters
const chapters = doc.getChildMarkers(CMarker);
for (const ch of chapters) {
  console.log(`Chapter ${ch.number}`);
}

// Get all verses in the document
const verses = doc.getChildMarkers(VMarker);
for (const v of verses) {
  console.log(`Verse ${v.verseNumber}`);
}

// Get verses within a specific chapter
const chapter1 = chapters.find(c => c.number === 1);
const versesInCh1 = chapter1.getChildMarkers(VMarker);

// Get all text content within a verse
const textBlocks = versesInCh1[0].getChildMarkers(TextBlock);
const verseText = textBlocks.map(tb => tb.text).join("");

Constructor options

USFMParser accepts three optional parameters:

// Default: parse everything
const parser = new USFMParser();

// Ignore specific tags (e.g. bold markers)
const parser = new USFMParser(["bd", "bd*"]);

// Ignore unknown markers (second parameter)
// Unknown markers become TextBlocks with just their text content
const parser = new USFMParser(null, true);

// Ignore invalid markers (third parameter)
// Invalid markers (markers missing required values) are silently dropped
const parser = new USFMParser(null, false, true);

Working with the document tree

Every marker has a contents array of child markers:

const doc = parser.parseFromString("\\c 1 \\p \\v 1 In the beginning");

// Direct traversal
const chapter = doc.contents[0] as CMarker;      // \c 1
const paragraph = chapter.contents[0] as PMarker; // \p
const verse = paragraph.contents[0] as VMarker;   // \v 1
const text = verse.contents[0] as TextBlock;       // "In the beginning"

// Merging documents
const doc2 = parser.parseFromString("\\c 2 \\p \\v 1 More text");
doc.insert(doc2);

Serializing back to USFM

Use getRawContents() to convert the marker tree back to a USFM string:

const doc = parser.parseFromString("\\id GEN \\c 1 \\p \\v 1 In the beginning");
const usfm = doc.getRawContents();
// => "\\id GEN \\c 1 \\p \\v 1 In the beginning"

Handling unknown and invalid markers

The parser produces two special marker types for error handling:

  • UnknownMarker -- for marker tags that aren't part of the USFM spec (e.g. \xyz).
  • InvalidMarker -- for known markers that are missing their required value (e.g. \c without a chapter number, \v without a verse number).
import { UnknownMarker, InvalidMarker } from "usfmtools";

const doc = parser.parseFromString("\\id GEN \\c \\p \\v 1 Text \\xyz data");

// Find markers with missing required values
const invalid = doc.getChildMarkers(InvalidMarker);
for (const m of invalid) {
  console.log(`Invalid \\${m.parsedIdentifier} at line ${m.line}`);
}

// Find unrecognized markers
const unknown = doc.getChildMarkers(UnknownMarker);
for (const m of unknown) {
  console.log(`Unknown \\${m.parsedIdentifier}: ${m.parsedValue}`);
}

Marker validation

Markers that require a value override isValid(). You can check validity on any marker instance:

const cm = new CMarker();
cm.preProcess("5");
cm.isValid(); // true

const cm2 = new CMarker();
cm2.preProcess("");
cm2.isValid(); // false

Markers with required values: \c, \v, \id, \ide, \usfm, \sts, \f, \x, \fr, \xo, \ca, \va, \fv, \fig, \w.

Footnotes and cross references

import { FMarker, FRMarker, FTMarker, XMarker, XOMarker } from "usfmtools";

const doc = parser.parseFromString(
  "\\v 1 Text \\f + \\fr 1.1 \\ft A footnote. \\f*"
);

const verse = doc.getChildMarkers(VMarker)[0];
const footnotes = verse.getChildMarkers(FMarker);

for (const fn of footnotes) {
  console.log(`Caller: ${fn.footNoteCaller}`);
  const ref = fn.getChildMarkers(FRMarker)[0];
  console.log(`Reference: ${ref?.verseReference}`);
}

Word entries and attributes

import { WMarker } from "usfmtools";

const doc = parser.parseFromString(
  '\\v 1 \\w gracious|lemma="grace" strong="G5485" \\w*'
);

const words = doc.getChildMarkers(WMarker);
for (const w of words) {
  console.log(`Term: ${w.term}`);
  console.log(`Lemma: ${w.attributes["lemma"]}`);
  console.log(`Strong: ${w.attributes["strong"]}`);
}