{"name":"hast-util-truncate","version":"2.0.0","description":"hast utility to truncate the tree to a certain number of characters","license":"MIT","keywords":["unist","hast","hast-util","util","utility","html","truncate","excerpt","summary"],"repository":{"type":"git","url":"git+https://github.com/syntax-tree/hast-util-truncate.git"},"bugs":{"url":"https://github.com/syntax-tree/hast-util-truncate/issues"},"funding":{"type":"opencollective","url":"https://opencollective.com/unified"},"author":{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"},"contributors":[{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"}],"sideEffects":false,"type":"module","exports":"./index.js","dependencies":{"@types/hast":"^3.0.0","@ungap/structured-clone":"^1.0.0","micromark-util-character":"^2.0.0"},"devDependencies":{"@types/node":"^20.0.0","@types/ungap__structured-clone":"^0.3.0","c8":"^8.0.0","hast-util-select":"^6.0.0","hastscript":"^8.0.0","prettier":"^3.0.0","remark-cli":"^11.0.0","remark-preset-wooorm":"^9.0.0","type-coverage":"^2.0.0","typescript":"^5.0.0","xo":"^0.55.0"},"scripts":{"prepack":"npm run build && npm run format","build":"tsc --build --clean && tsc --build && type-coverage","format":"remark . -qfo && prettier . -w --log-level warn && xo --fix","test-api":"node --conditions development test.js","test-coverage":"c8 --100 --reporter lcov npm run test-api","test":"npm run build && npm run format && npm run test-coverage"},"prettier":{"bracketSpacing":false,"semi":false,"singleQuote":true,"tabWidth":2,"trailingComma":"none","useTabs":false},"remarkConfig":{"plugins":["remark-preset-wooorm"]},"typeCoverage":{"atLeast":100,"detail":true,"ignoreCatch":true,"strict":true},"xo":{"prettier":true,"rules":{"unicorn/prefer-code-point":"off"}},"gitHead":"c1d4e2a82b52225f738319f9a187962ab9fb3194","types":"./index.d.ts","homepage":"https://github.com/syntax-tree/hast-util-truncate#readme","_id":"hast-util-truncate@2.0.0","_nodeVersion":"20.0.0","_npmVersion":"9.7.2","dist":{"integrity":"sha512-QjtUhkt0hT75dHwwTYS+E9Rge12IVClGUErJgss7q7EewBuSr6LoGjq7nPp3R6BThqWJ6CbarPVPCbklwnUf/g==","shasum":"1becac58e223eeec5efe16d61e9d3f68c09b80de","tarball":"http://registry-npm.ywwl.com/hast-util-truncate/download/hast-util-truncate-2.0.0.tgz","fileCount":7,"unpackedSize":17872,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDLYBvSwa0F4knamA7tByrJWev035Symc2YgijcrjPUgQIgLlWFy57G8GoOo+wkdscWbPMibOBYcPh3yTH9JmetunQ="}],"size":5996,"noattachment":false,"key":"/hast-util-truncate/-/hast-util-truncate-2.0.0.tgz"},"_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"directories":{},"maintainers":[{"name":"wooorm","email":"tituswormer@gmail.com"},{"name":"kmck","email":"keith@mcknig.ht"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/hast-util-truncate_2.0.0_1691429935903_0.6026658388697963"},"_hasShrinkwrap":false,"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<script type=\"module\">\n  import {truncate} from 'https://esm.sh/hast-util-truncate@2?bundle'\n</script>\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<Node>`).\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<!-- Definitions -->\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&query=$.size.compressedSize&url=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","publish_time":1691429936051,"_cnpm_publish_time":1691429936051,"dist-tags":{"latest":"2.0.0"}}