Module making safe substring of HTML source
interface Options {
breakWords: boolean // defaults to true
suffix: (() => string) | string | null // defaults to null
}
/**
* @param source Source HTML
* @param length Visible characters (everything but HTML tags) limit
* @param options Options object or suffix
*
* @returns stripped source by length characters
*/
function html_substring(
source: string,
length: number,
options?: string | Partial<Options>,
): string
// JavaScript/TypeScript
import html_substring from 'html-substring'
console.log(html_substring('<span><div>Hello</div> <p>World</p></span>', 6))
// <span><div>Hello</div> </span>
You can look tests for more examples