truncate-html
Version:
Truncate HTML and Keep Tags
74 lines (73 loc) • 2.74 kB
TypeScript
import { CheerioAPI, Cheerio, AnyNode } from 'cheerio';
/**
* custom node strategy, default to Cheerio<AnyNode>
* * '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<AnyNode> truncate the returned node
* * undefined or any falsy value to truncate original node
*/
export type ICustomNodeStrategy = (node: Cheerio<AnyNode>) => 'remove' | 'keep' | Cheerio<AnyNode> | undefined;
/**
* truncate-html full options object
*/
export interface IFullOptions {
/**
* remove all tags, default false
*/
stripTags: boolean;
/**
* ellipsis sign, default '...'
*/
ellipsis: string;
/**
* decode html entities(e.g. convert `&` to `&`) before counting length, default false
*/
decodeEntities: boolean;
/**
* elements' selector you want ignore
*/
excludes: string | string[];
/**
* custom node strategy, default to Cheerio<AnyNode>
* * '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<AnyNode> truncate the returned node
* * undefined or any falsy value to truncate original node
*/
customNodeStrategy: ICustomNodeStrategy;
/**
* how many letters(words if `byWords` is true) you want reserve
*/
length: number;
/**
* if true, length means how many words to reserve
*/
byWords: boolean;
/**
* 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.
*/
reserveLastWord: boolean | number;
/**
* 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
*/
trimTheOnlyWord: boolean;
/**
* keep whitespaces, by default continuous paces will
* be replaced with one space, set it true to keep them
*/
keepWhitespaces: boolean;
}
/**
* options interface for function
*/
export type IOptions = Partial<IFullOptions>;
declare function truncate(html: string | CheerioAPI, length?: number | IOptions, truncateOptions?: IOptions): string;
declare namespace truncate {
var setup: (options: IOptions) => void;
}
export default truncate;