{"library":"simple-text-parser","title":"Simple Text Parser","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install simple-text-parser"],"cli":null},"imports":["import { Parser } from 'simple-text-parser';","const { Parser } = require('simple-text-parser');","Parser.registerPreset('myPreset', myRuleFunction);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Parser } from 'simple-text-parser';\n\n// Create a new parser instance\nconst parser = new Parser();\n\n// Define a rule to parse hashtags into custom HTML\n// The capture group (\\S+) allows extracting the tag without the '#'\nparser.addRule(/#([\\S]+)/gi, function (fullMatch, cleanTag) {\n  const html = `<span class=\"hashtag\">${cleanTag}</span>`;\n  // Return an object for 'toTree' method to capture structured data\n  return { type: 'tag', text: html, value: cleanTag };\n});\n\n// Define another rule for URLs using a preset and a custom renderer\nparser.addPreset('url', function (fullMatch, href, text) {\n  const html = `<a href=\"${href}\" target=\"_blank\">${text || href}</a>`;\n  return { type: 'link', text: html, href: href };\n});\n\nconst inputText = 'Check out this #awesome package: https://github.com/tyler-johnson/simple-text-parser';\n\n// Render the text into a string with HTML replacements\nconst renderedText = parser.render(inputText);\nconsole.log('Rendered HTML:', renderedText);\n// 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>\"\n\n// Parse the text into a tree of nodes for structured access\nconst parsedTree = parser.toTree(inputText);\nconsole.log('Parsed Tree:', JSON.stringify(parsedTree, null, 2));\n/*\nExpected:\n[\n  { \"type\": \"text\", \"text\": \"Check out this \" },\n  { \"type\": \"tag\", \"text\": \"<span class=\\\"hashtag\\\">awesome</span>\", \"value\": \"awesome\" },\n  { \"type\": \"text\", \"text\": \" package: \" },\n  { \"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\" }\n]\n*/","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}