truncate-html

  • Version 1.2.1
  • Published
  • 30.3 kB
  • 1 dependency
  • MIT license

Install

npm i truncate-html
yarn add truncate-html
pnpm add truncate-html

Overview

Truncate HTML and Keep Tags

Index

Functions

function truncate

truncate: typeof truncate;

    Interfaces

    interface IFullOptions

    interface IFullOptions {}
    • truncate-html full options object

    property byWords

    byWords: boolean;
    • if true, length means how many words to reserve

    property customNodeStrategy

    customNodeStrategy: ICustomNodeStrategy;
    • custom node strategy, default to Cheerio * 'remove' to remove the node * 'keep' to keep the node(and anything inside it) anyway, and won't be counted as there is no text content in it * Cheerio truncate the returned node * undefined or any falsy value to truncate original node

    property decodeEntities

    decodeEntities: boolean;
    • decode html entities(e.g. convert & to &) before counting length, default false

    property ellipsis

    ellipsis: string;
    • ellipsis sign, default '...'

    property excludes

    excludes: string | string[];
    • elements' selector you want ignore

    property keepWhitespaces

    keepWhitespaces: boolean;
    • keep whitespaces, by default continuous paces will be replaced with one space, set it true to keep them

    property length

    length: number;
    • how many letters(words if byWords is true) you want reserve

    property reserveLastWord

    reserveLastWord: boolean | number;
    • how to deal with when truncate in the middle of a word 1. by default, just cut at that position. 2. set it to true, with max exceed 10 letters can exceed to reserver the last word 3. set it to a positive number decide how many letters can exceed to reserve the last word 4. set it to negative number to remove the last word if cut in the middle.

    property stripTags

    stripTags: boolean;
    • remove all tags, default false

    property trimTheOnlyWord

    trimTheOnlyWord: boolean;
    • if reserveLastWord set to negative number, and there is only one word in the html string, when trimTheOnlyWord set to true, the extra letters will be sliced if word's length longer than length. see issue #23 for more details

    Type Aliases

    type ICustomNodeStrategy

    type ICustomNodeStrategy = (
    node: Cheerio<AnyNode>
    ) => 'remove' | 'keep' | Cheerio<AnyNode> | undefined;
    • custom node strategy, default to Cheerio * 'remove' to remove the node * 'keep' to keep the node(and anything inside it) anyway, and won't be counted as there is no text content in it * Cheerio truncate the returned node * undefined or any falsy value to truncate original node

    type IOptions

    type IOptions = Partial<IFullOptions>;
    • options interface for function

    Namespaces

    namespace truncate

    namespace truncate {}

      function setup

      setup: (options: IOptions) => void;

        Package Files (1)

        Dependencies (1)

        Dev Dependencies (10)

        Peer Dependencies (0)

        No peer dependencies.

        Badge

        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/truncate-html.

        • Markdown
          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/truncate-html)
        • HTML
          <a href="https://www.jsdocs.io/package/truncate-html"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>