{"_id":"@cjser/alcalzone__ansi-tokenize","_rev":"2-1895ee1f4491b273e7db9099eb1ab7d7","name":"@cjser/alcalzone__ansi-tokenize","dist-tags":{"latest":"0.3.1-cjser.2"},"versions":{"0.3.0-cjser.2":{"name":"@cjser/alcalzone__ansi-tokenize","version":"0.3.0-cjser.2","author":{"name":"AlCalzone","email":"d.griesel@gmx.net"},"license":"MIT","_id":"@cjser/alcalzone__ansi-tokenize@0.3.0-cjser.2","maintainers":[{"name":"nanahira","email":"nanahira@momobako.com"}],"dist":{"shasum":"fb4ad3678a41b739add76abca9c818a4c7fe0161","tarball":"https://registry.npmjs.org/@cjser/alcalzone__ansi-tokenize/-/alcalzone__ansi-tokenize-0.3.0-cjser.2.tgz","fileCount":28,"integrity":"sha512-7jqsWv3tAEVt20vvnA3YEGtOWtXu/SuM78UfOOvBfhGc3qHnAvybnwBlNmsVc1Oq+xGs4KFXoIOW7sk9drkXKQ==","signatures":[{"sig":"MEQCIEFjaHM57efkqLpooPE7NtOHaI7pNhniQ/fz+8g/uMisAiAP/7UXDkMzp7vAlnJC9eHdiK9KNL/CiunWhG4YDfeqEA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":52885},"main":"./dist-cjser/index.cjs","type":"module","cjser":{"original":{"name":"@alcalzone/ansi-tokenize","files":["build"],"exports":{".":{"types":"./build/index.d.ts","import":"./build/index.js"},"./package.json":"./package.json"},"scripts":{"lint":"eslint .","test":"vitest run","build":"tsc -p tsconfig.build.json","prepare":"tsc -p tsconfig.build.json","release":"release-script"},"version":"0.3.0","dependencies":{"ansi-styles":"^6.2.1","is-fullwidth-code-point":"^5.0.0"}},"cjserVersion":2,"sourceVersion":"0.3.0"},"types":"build/index.d.ts","module":"build/index.js","engines":{"node":">=18"},"exports":{".":{"types":"./build/index.d.ts","import":"./build/index.js","require":"./dist-cjser/index.cjs"},"./package.json":"./package.json"},"gitHead":"0168e4aac54a1a9ca6f921419fa5ed9edb53ef4a","scripts":{"lint":"eslint .","test":"vitest run","build":"tsc -p tsconfig.build.json","release":"release-script"},"_npmUser":{"name":"nanahira","email":"nanahira@momobako.com"},"repository":{"url":"https://code.moenext.com/3rdeye/cjser.git","type":"git"},"_npmVersion":"10.7.0","description":"Efficiently modify strings containing ANSI escape codes","directories":{},"_nodeVersion":"20.14.0","dependencies":{"@cjser/ansi-styles":"6.2.3-cjser.2","@cjser/is-fullwidth-code-point":"5.1.0-cjser.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"packageManager":"yarn@4.10.3","devDependencies":{"eslint":"^8.36.0","vitest":"^3.2.4","prettier":"^2.8.4","typescript":"^5.0.2","@types/node":"^18.19.130","@tsconfig/node18":"^18.2.4","eslint-config-prettier":"^8.7.0","eslint-plugin-prettier":"^4.2.1","@alcalzone/release-script":"~4.0.0","@typescript-eslint/parser":"^5.55.0","@typescript-eslint/eslint-plugin":"^5.55.0","@alcalzone/release-script-plugin-license":"~4.0.0"},"_npmOperationalInternal":{"tmp":"tmp/alcalzone__ansi-tokenize_0.3.0-cjser.2_1778130358395_0.5552926422858822","host":"s3://npm-registry-packages-npm-production"}},"0.3.1-cjser.2":{"_id":"@cjser/alcalzone__ansi-tokenize@0.3.1-cjser.2","dist":{"shasum":"78f98ac5b741b0d6e069284a23c72e16cdfd408c","tarball":"https://registry.npmjs.org/@cjser/alcalzone__ansi-tokenize/-/alcalzone__ansi-tokenize-0.3.1-cjser.2.tgz","fileCount":28,"integrity":"sha512-4EN1FG0iaofkycsmZ+zbjJkK/FObjfOX/u8X/eRhhF6DjXWz8pcYGKdK9pOrdFdjN9ZFFjgh9nYmllVsr0QGHw==","signatures":[{"sig":"MEUCIFvK3TqVoafNFvHBVhO41xw5K7HcznfTCMUOrCTJBUqwAiEA6JBVWBBeN2F1vqdLg6fIZw7g9p8DHRohWkhQ8VNOlxY=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDHHaX/7bimdM7FGcLWsyuyeZz8cRLsKendAxffh12mNgIhAJ0pnoEm9wuKJLV3hjTklax1bi1CJvlXvsxFqkIv6+pg"}],"unpackedSize":53856},"main":"./dist-cjser/index.cjs","name":"@cjser/alcalzone__ansi-tokenize","type":"module","cjser":{"original":{"name":"@alcalzone/ansi-tokenize","files":["build"],"exports":{".":{"types":"./build/index.d.ts","import":"./build/index.js"},"./package.json":"./package.json"},"scripts":{"lint":"eslint .","test":"vitest run","build":"tsc -p tsconfig.build.json","prepare":"tsc -p tsconfig.build.json","release":"release-script"},"version":"0.3.1","repository":{"url":"git+https://github.com/AlCalzone/ansi-tokenize.git","type":"git"},"dependencies":{"ansi-styles":"^6.2.1","is-fullwidth-code-point":"^5.0.0"}},"cjserVersion":2,"sourceVersion":"0.3.1"},"types":"build/index.d.ts","author":{"name":"AlCalzone","email":"d.griesel@gmx.net"},"module":"build/index.js","engines":{"node":">=18"},"exports":{".":{"types":"./build/index.d.ts","import":"./build/index.js","require":"./dist-cjser/index.cjs"},"./package.json":"./package.json"},"gitHead":"a4bef54d7d22d3d9a6b84528457938d84cd65fe7","license":"MIT","scripts":{"lint":"eslint .","test":"vitest run","build":"tsc -p tsconfig.build.json","release":"release-script"},"version":"0.3.1-cjser.2","_npmUser":{"name":"nanahira","email":"nanahira@momobako.com"},"homepage":"https://github.com/AlCalzone/ansi-tokenize","repository":{"url":"https://code.moenext.com/3rdeye/cjser.git","type":"git"},"_npmVersion":"11.9.0","description":"Efficiently modify strings containing ANSI escape codes","directories":{},"maintainers":[{"name":"nanahira","email":"nanahira@momobako.com"}],"_nodeVersion":"24.14.0","dependencies":{"@cjser/ansi-styles__v6_2_3":"6.2.3-cjser.2","@cjser/is-fullwidth-code-point":"5.1.0-cjser.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"packageManager":"yarn@4.17.0","devDependencies":{"eslint":"^8.36.0","vitest":"^3.2.4","prettier":"^2.8.4","typescript":"^5.0.2","@types/node":"^18.19.130","@tsconfig/node18":"^18.2.4","eslint-config-prettier":"^8.7.0","eslint-plugin-prettier":"^4.2.1","@alcalzone/release-script":"~5.2.0","@typescript-eslint/parser":"^5.55.0","@typescript-eslint/eslint-plugin":"^5.55.0","@alcalzone/release-script-plugin-license":"~5.2.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/alcalzone__ansi-tokenize_0.3.1-cjser.2_1789492540482_0.528970558632915"}}},"time":{"created":"2026-05-07T05:05:58.191Z","modified":"2026-09-15T17:15:40.990Z","0.3.0-cjser.2":"2026-05-07T05:05:58.545Z","0.3.1-cjser.2":"2026-09-15T17:15:40.573Z"},"author":{"name":"AlCalzone","email":"d.griesel@gmx.net"},"license":"MIT","repository":{"url":"https://code.moenext.com/3rdeye/cjser.git","type":"git"},"description":"Efficiently modify strings containing ANSI escape codes","maintainers":[{"name":"nanahira","email":"nanahira@momobako.com"}],"readme":"# @alcalzone/ansi-tokenize\n\n> Efficiently modify strings containing [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)\n\nIf you find yourself modifying styled strings repeatedly, alternatives like [`slice-ansi`](https://github.com/chalk/slice-ansi/) may end up doing a lot of unnecessary work by re-parsing the string each time. This module provides a way to parse the string into an array of tokens (characters or ANSI codes), which can then be modified and re-serialized into a styled string.\n\n## Install\n\n```\n$ npm install @alcalzone/ansi-tokenize\n```\n\n## Usage\n\n### Tokenize a string\n\n```js\nimport { tokenize } from \"@alcalzone/ansi-tokenize\";\n\n// red \"foo\", followed by unstyled \"bar\"\nconst str = \"\\x1B[31mfoo\\x1B[39mbar\";\nconst tokens = tokenize(str);\n\n// tokens will now look like this:\n[\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: \"\\x1B[31m\",\n\t\tendCode: \"\\x1B[39m\",\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"f\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: \"\\x1B[39m\",\n\t\tendCode: \"\\x1B[39m\",\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"b\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"a\",\n\t\tfullWidth: false,\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"r\",\n\t\tfullWidth: false,\n\t},\n];\n```\n\nEach token is either a character\n\n```ts\nexport interface Char {\n\ttype: \"char\";\n\tvalue: string;\n\tfullWidth: boolean;\n}\n```\n\nwhere\n\n- `value` is the string representation of the character\n- `fullWidth` is `true` if the character is full width (takes up 2 characters in monospace, like CJK characters)\n\nor an ANSI code\n\n```ts\nexport interface AnsiCode {\n\ttype: \"ansi\";\n\tcode: string;\n\tendCode: string;\n}\n```\n\nwhere\n\n- `code` is the ANSI code that starts the style\n- and `endCode` is the corresponding ANSI code that ends the style.\n\nAn `AnsiCode` can also be an end code, in which case `code` and `endCode` will be the same.\n\n### Convert an array of tokens into an array of \"styled\" chars\n\nThis representation is a 1:1 mapping of the original string, but not very useful for modifying the string. The `styledCharsFromTokens` function converts a token array to an array of characters, where each character has an all currently active styles associated with it:\n\n```ts\nexport interface StyledChar {\n\ttype: \"char\";\n\tvalue: string;\n\tfullWidth: boolean;\n\tstyles: AnsiCode[];\n}\n```\n\nUsing the above example:\n\n```js\nimport { tokenize, styledCharsFromTokens } from \"@alcalzone/ansi-tokenize\";\n\n// red \"foo\", followed by unstyled \"bar\"\nconst str = \"\\x1B[31mfoo\\x1B[39mbar\";\nconst tokens = tokenize(str);\n\nconst styledChars = styledCharsFromTokens(tokens);\n\n// styledChars will contain the following:\n[\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"f\",\n\t\tfullWidth: false,\n\t\tstyles: [\n\t\t\t{\n\t\t\t\ttype: \"ansi\",\n\t\t\t\tcode: \"\\x1B[31m\",\n\t\t\t\tendCode: \"\\x1B[39m\",\n\t\t\t},\n\t\t],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t\tstyles: [\n\t\t\t{\n\t\t\t\ttype: \"ansi\",\n\t\t\t\tcode: \"\\x1B[31m\",\n\t\t\t\tendCode: \"\\x1B[39m\",\n\t\t\t},\n\t\t],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"o\",\n\t\tfullWidth: false,\n\t\tstyles: [\n\t\t\t{\n\t\t\t\ttype: \"ansi\",\n\t\t\t\tcode: \"\\x1B[31m\",\n\t\t\t\tendCode: \"\\x1B[39m\",\n\t\t\t},\n\t\t],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"b\",\n\t\tfullWidth: false,\n\t\tstyles: [],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"a\",\n\t\tfullWidth: false,\n\t\tstyles: [],\n\t},\n\t{\n\t\ttype: \"char\",\n\t\tvalue: \"r\",\n\t\tfullWidth: false,\n\t\tstyles: [],\n\t},\n];\n```\n\n### Modify an array of styled characters\n\nFor modification simply edit the items in the array as necessary, as long as the following rules are followed:\n\n1. The `code` and `endCode` properties must match. You can use the `ansi-styles` module to do this.\n2. The `fullWidth` property must be correct. You can use the `is-fullwidth-code-point` module to do this, or if working with multiple strings, turn those into styled char arrays first.\n\nE.g. to make the first `o` blue and bold:\n\n```js\nimport ansiStyles from \"ansi-styles\";\n\n// ... include the above code\n\nstyledChars[1].styles = [\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: ansiStyles.blue.open,\n\t\tendCode: ansiStyles.blue.close,\n\t},\n\t{\n\t\ttype: \"ansi\",\n\t\tcode: ansiStyles.bold.open,\n\t\tendCode: ansiStyles.bold.close,\n\t},\n];\n```\n\n### Serialize a styled character array back to a string\n\nThe `styledCharsToString` function converts a styled character array back to a string:\n\n```js\nimport { styledCharsToString } from \"@alcalzone/ansi-tokenize\";\n\n// ... include the above code\n\nconst strOut = styledCharsToString(styledChars);\n\n// str will now be '\\x1B[31mf\\x1B[34m\\x1B[1mo\\x1B[22m\\x1B[31mo\\x1B[39mbar'\n```\n\nThis automatically figures out the least amount of escape codes necessary to achieve the desired result, as long as the `styles` arrays contain no unnecessary styles, e.g. blue + red foreground.\n\n## Changelog\n\n<!--\n\tPlaceholder for next release:\n\t### __WORK IN PROGRESS__\n-->\n### 0.3.1 (2026-09-15)\n\n- Fix: Prevent OSC hyperlink end codes from leaking into the following styled chars (#82)\n\n### 0.3.0 (2026-02-20)\n\n- Fix: preserve non-hyperlink OSC sequences when tokenizing (#54)\n- Fix: support ST-terminated OSC hyperlinks (#53)\n\n### 0.2.5 (2026-02-11)\n\n- Fix: preserve grapheme clusters when tokenizing (#51)\n\n### 0.2.4 (2026-01-29)\n\n- Fix: Support hyperlinks with parameters (#45)\n\n### 0.2.3 (2026-01-02)\n\n- Fix: Regression when rendering adjacent `dim` and `bold` styles (#43, #44)\n\n### 0.2.2 (2025-10-22)\n\n- Fix: Support compound SGR sequences with multiple attributes (#39, #40)\n\n### 0.2.1 (2025-10-20)\n\n- Fix: Prevent `dim` and `bold` modifiers from canceling each other (#37)\n\n### 0.2.0 (2025-04-24)\n\n- Breaking: Require Node.js 18+\n- Fix: Detect emojis as being full width\n\n### 0.1.3 (2023-09-07)\n\n- Fix: Support links\n\n### 0.1.2 (2023-08-07)\n\n- Fix: Reduce minimum Node.js version to `14.13.1`\n\n### 0.1.1 (2023-04-05)\n\n- Fix: Active styles are now correctly reset at the end of the string\n\n### 0.1.0 (2023-03-20)\n\nInitial release\n\n## cjser\n\nThis package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.\nOriginal repository: https://github.com/AlCalzone/ansi-tokenize\n","readmeFilename":"README.md","homepage":"https://github.com/AlCalzone/ansi-tokenize"}