UNPKG

hast-util-truncate

Version:

hast utility to truncate the tree to a certain number of characters

53 lines (52 loc) 1.92 kB
/** * Truncate the tree to a certain number of characters. * * @template {Nodes} Tree * Type of tree. * @param {Tree} tree * Tree to truncate. * @param {Options | null | undefined} [options] * Configuration (optional). * @returns {Tree} * A shallow copy of `tree`, truncated. */ export function truncate<Tree extends import("hast").Nodes>(tree: Tree, options?: Options | null | undefined): Tree; export type Nodes = import('hast').Nodes; export type RootContent = import('hast').RootContent; export type Text = import('hast').Text; /** * Configuration. */ export type Options = { /** * Value to use at truncation point (optional). */ ellipsis?: string | null | undefined; /** * Nodes to exclude from the resulting tree; these are not counted towards * `size` (optional). */ ignore?: Array<RootContent> | null | undefined; /** * How far to walk back (default: `30`). * * The algorithm attempts to break right after a word rather than the exact * `size`. * Take for example the `|`, which is the actual break defined by `size`, and * the `…` is the location where the ellipsis is placed: `This… an|d that`. * Breaking at `|` would at best look bad but could likely result in things * such as `ass…` for `assignment` — which is not ideal. * `maxCharacterStrip` defines how far back the algorithm will walk to find * a pretty word break. * This prevents a potential slow operation on larger `size`s without any * whitespace. * If `maxCharacterStrip` characters are walked back and no nice break point * is found, the bad break point is used. * Set `maxCharacterStrip: 0` to not find a nice break. */ maxCharacterStrip?: number | null | undefined; /** * Number of characters to truncate to (default: `140`). */ size?: number | null | undefined; };