Registry / serialization / simple-text-parser

simple-text-parser

JSON →
library2.1.1jsnpmunverified

Simple Text Parser is a TypeScript-based utility for synchronously parsing plain text strings using highly customizable rules defined by regular expressions or substring matches. It allows developers to transform text by replacing matches with custom HTML or by converting text into a structured tree of nodes, enabling more complex data extraction and manipulation. Currently at version 2.1.1, the library maintains a stable release cadence. Its key differentiators include its simplicity, synchronous operation for predictable performance, and the ability to define highly granular parsing rules using standard JavaScript regular expressions, making it suitable for both browser and Node.js environments. The library ships with built-in TypeScript types, providing a robust development experience.

npm install simple-text-parser
INSTALL
IMPORT
SIG · SIMPLE-TEXT-PARSER
S
simple-text-parser
serializationjavascriptv2.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
import { Parser } from 'simple-text-parser';
import Parser from 'simple-text-parser';
The Parser class is a named export, not a default export.
Parser (CommonJS)
const { Parser } = require('simple-text-parser');
const Parser = require('simple-text-parser');
Use destructuring for CommonJS require as Parser is a named export.
Parser.registerPreset
Parser.registerPreset('myPreset', myRuleFunction);
parser.registerPreset('myPreset', myRuleFunction);
registerPreset is a static method on the Parser class, not an instance method.

This quickstart demonstrates creating a Parser, adding a custom regex rule for hashtags, using a built-in preset for URLs, and then processing text with both `render()` for HTML output and `toTree()` for structured data.

import { Parser } from 'simple-text-parser'; // Create a new parser instance const parser = new Parser(); // Define a rule to parse hashtags into custom HTML // The capture group (\S+) allows extracting the tag without the '#' parser.addRule(/#([\S]+)/gi, function (fullMatch, cleanTag) { const html = `<span class="hashtag">${cleanTag}</span>`; // Return an object for 'toTree' method to capture structured data return { type: 'tag', text: html, value: cleanTag }; }); // Define another rule for URLs using a preset and a custom renderer parser.addPreset('url', function (fullMatch, href, text) { const html = `<a href="${href}" target="_blank">${text || href}</a>`; return { type: 'link', text: html, href: href }; }); const inputText = 'Check out this #awesome package: https://github.com/tyler-johnson/simple-text-parser'; // Render the text into a string with HTML replacements const renderedText = parser.render(inputText); console.log('Rendered HTML:', renderedText); // Expected: "Check out this <span class="hashtag">awesome</span> package: <a href="https://github.com/tyler-johnson/simple-text-parser" target="_blank">https://github.com/tyler-johnson/simple-text-parser</a>" // Parse the text into a tree of nodes for structured access const parsedTree = parser.toTree(inputText); console.log('Parsed Tree:', JSON.stringify(parsedTree, null, 2)); /* Expected: [ { "type": "text", "text": "Check out this " }, { "type": "tag", "text": "<span class=\"hashtag\">awesome</span>", "value": "awesome" }, { "type": "text", "text": " package: " }, { "type": "link", "text": "<a href=\"https://github.com/tyler-johnson/simple-text-parser\" target=\"_blank\">https://github.com/tyler-johnson/simple-text-parser</a>", "href": "https://github.com/tyler-johnson/simple-johnson/simple-text-parser" } ] */
Debug
Known issues
gotchaThe behavior of the `replace` function in `addRule()` determines the output format for `render()` and `toTree()`. If `replace` returns a string, `render()` will use that string, but `toTree()` will embed it as plain text. For `toTree()` to provide structured nodes with metadata, the `replace` function *must* return an object `{ type: string, text: string, ...rest }`.
fix
Ensure your `replace` function returns an object `{ type: 'yourType', text: 'replacement string', value: 'data' }` if you intend to use `toTree()` for structured data. If you only care about string replacement, returning a string is sufficient.
affects: >=2.0.0
gotchaThis parser operates synchronously, which makes it predictable and easy to use. However, for extremely large text inputs (e.g., multi-megabyte documents) or a very high number of complex rules, synchronous processing might lead to blocking the event loop in Node.js or freezing the UI in a browser environment. Consider batching input or offloading parsing to a Web Worker for such scenarios.
fix
For very large inputs or performance-critical applications, consider breaking the input text into smaller chunks and parsing them incrementally, or leverage Web Workers in browser environments to avoid blocking the main thread.
affects: >=2.0.0
gotchaThe order in which rules are added via `addRule()` can influence the parsing outcome, particularly if rules have overlapping match patterns. The parser processes rules sequentially, and an earlier rule might consume text that a later, more specific rule would have matched.
fix
Define your parsing rules from most specific to least specific, or ensure that overlapping rules are intentionally ordered to achieve the desired parsing hierarchy. Test thoroughly with various text inputs to verify rule interaction.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: parser.addRule is not a function
Attempting to call `addRule` on the `Parser` class directly instead of an instance.
fix
You must first create an instance of the `Parser` class: `const parser = new Parser();` then call `parser.addRule(...)`.
Unexpected output in `toTree()` (e.g., all nodes are 'text' type even for rules that should be parsed)
The `replace` function passed to `addRule()` is returning a string instead of a structured object.
fix
Modify your `replace` function to return an object like `{ type: 'customType', text: 'replacement HTML', value: 'extracted data' }` for `toTree()` to generate structured nodes.
My regex rule isn't matching anything, or it's matching too much/too little.
Incorrect regular expression pattern or missing/incorrect flags (e.g., `g` for global, `i` for case-insensitive).
fix
Review your regular expression pattern carefully. Use online regex testers (like regex101.com) to debug your pattern against example text. Ensure appropriate flags are included, such as `/pattern/gi` for global and case-insensitive matching.
Upgrade
Version history
2.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
OpenAI (training)
1
Resources
simple-text-parser — npm install simple-text-parser · libregistry