{"id":13339506,"url":"https://github.com/syntax-tree/hast-util-truncate","last_synced_at":"2025-06-29T22:03:52.438Z","repository":{"id":57261727,"uuid":"404777324","full_name":"syntax-tree/hast-util-truncate","owner":"syntax-tree","description":"utility to truncate the tree to a certain number of characters","archived":false,"fork":false,"pushed_at":"2023-08-07T17:38:54.000Z","size":56,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-21T23:40:14.046Z","etag":null,"topics":["excerpt","hast","hast-util","html","summary","truncate","unist"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-09T15:35:38.000Z","updated_at":"2023-01-11T12:41:44.000Z","dependencies_parsed_at":"2024-06-18T22:52:16.790Z","dependency_job_id":"c7cc57ae-6ced-4308-9bf7-e03ce89d82c5","html_url":"https://github.com/syntax-tree/hast-util-truncate","commit_stats":{"total_commits":31,"total_committers":1,"mean_commits":31.0,"dds":0.0,"last_synced_commit":"c1d4e2a82b52225f738319f9a187962ab9fb3194"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/hast-util-truncate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-truncate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-truncate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-truncate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-truncate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/hast-util-truncate/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-truncate/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261224248,"owners_count":23126922,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["excerpt","hast","hast-util","html","summary","truncate","unist"],"created_at":"2024-07-29T19:20:04.323Z","updated_at":"2025-06-29T22:03:52.397Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":[],"sub_categories":[],"readme":"# hast-util-truncate\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[hast][] utility to truncate the tree to a certain number of characters.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`truncate(tree[, options])`](#truncatetree-options)\n    *   [`Options`](#options)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Security](#security)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis package is a utility that takes a [hast][] (HTML) syntax tree and truncates\nit to a certain number of characters, while otherwise preserving the tree\nstructure.\n\n## When should I use this?\n\nThis is a small utility useful when you need to create a shorter version of a\npotentially long document.\n\nThis utility is similar to [`hast-util-excerpt`][hast-util-excerpt], which\ntruncates a tree to a certain comment.\n\nThe rehype plugin\n[`rehype-infer-description-meta`][rehype-infer-description-meta]\nwraps both this utility and `hast-util-excerpt` to figure out a description of a\ndocument, for use with [`rehype-meta`][rehype-meta].\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install hast-util-truncate\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {truncate} from 'https://esm.sh/hast-util-truncate@2'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {truncate} from 'https://esm.sh/hast-util-truncate@2?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay our module `example.js` looks as follows:\n\n```js\nimport {h} from 'hastscript'\nimport {truncate} from 'hast-util-truncate'\n\nconst tree = h('p', [\n  'Lorem ipsum dolor sit amet, ',\n  h('em', 'consectetur'),\n  'adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud'\n])\n\nconsole.log(truncate(tree, {ellipsis: '…'}));\n```\n\n…now running `node example.js` yields:\n\n```js\n{\n  type: 'element',\n  tagName: 'p',\n  properties: {},\n  children: [\n    {type: 'text', value: 'Lorem ipsum dolor sit amet, '},\n    {\n      type: 'element',\n      tagName: 'em',\n      properties: {},\n      children: [{type: 'text', value: 'consectetur'}]\n    },\n    {\n      type: 'text',\n      value: 'adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim…'\n    }\n  ]\n}\n```\n\n## API\n\nThis package exports the identifier [`truncate`][api-truncate].\nThere is no default export.\n\n### `truncate(tree[, options])`\n\nTruncate the tree to a certain number of characters.\n\n###### Parameters\n\n*   `tree` ([`Node`][node])\n    — tree to truncate\n*   `options` ([`Options`][api-options], optional)\n    — configuration\n\n###### Returns\n\nTruncated copy of `tree` ([`Node`][node]).\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n##### Fields\n\n###### `options.size`\n\nNumber of characters to truncate to (`number`, default: `140`).\n\n###### `options.ellipsis`\n\nValue to use at truncation point (`string`, optional).\n\n###### `options.maxCharacterStrip`\n\nHow far to walk back (`number`, default: `30`).\nThe algorithm attempts to break right after a word rather than the exact `size`.\nTake for example the `|`, which is the actual break defined by `size`, and the\n`…` is the location where the ellipsis is placed: `This… an|d that`.\nBreaking at `|` would at best look bad but could likely result in things such as\n`ass…` for `assignment`, which is not ideal.\n`maxCharacterStrip` defines how far back the algorithm will walk to find a\npretty word break.\nThis prevents a potential slow operation on larger `size`s without any\nwhitespace.\nIf `maxCharacterStrip` characters are walked back and no nice break point is\nfound, the bad break point is used.\nSet `maxCharacterStrip: 0` to not find a nice break.\n\n###### `options.ignore`\n\nNodes to exclude from the resulting tree (`Array\u003cNode\u003e`).\nThese are not counted towards `size`.\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type [`Options`][api-options].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `hast-util-truncate@^2`,\ncompatible with Node.js 16.\n\n## Security\n\nUse of `hast-util-truncate` should be safe if the tree is already safe and\nyou’re not using user content in options.\nWhen in doubt, use [`hast-util-sanitize`][hast-util-sanitize].\n\n## Related\n\n*   [`hast-util-excerpt`][hast-util-excerpt]\n    — truncate the tree to a comment\n*   [`rehype-infer-description-meta`][rehype-infer-description-meta]\n    — infer file metadata from the contents of the document\n*   [`rehype-meta`][rehype-meta]\n    — add metadata to the head of a document\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/hast-util-truncate/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/hast-util-truncate/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-truncate.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/hast-util-truncate\n\n[downloads-badge]: https://img.shields.io/npm/dm/hast-util-truncate.svg\n\n[downloads]: https://www.npmjs.com/package/hast-util-truncate\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=hast-util-truncate\n\n[size]: https://bundlejs.com/?q=hast-util-truncate\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[hast]: https://github.com/syntax-tree/hast\n\n[node]: https://github.com/syntax-tree/hast#nodes\n\n[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize\n\n[hast-util-excerpt]: https://github.com/syntax-tree/hast-util-excerpt\n\n[rehype-infer-description-meta]: https://github.com/rehypejs/rehype-infer-description-meta\n\n[rehype-meta]: https://github.com/rehypejs/rehype-meta\n\n[api-truncate]: #truncatetree-options\n\n[api-options]: #options\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-truncate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fhast-util-truncate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-truncate/lists"}