npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@html-splitter/html-splitter

v1.0.0

Published

HTML Splitter

Downloads

3

Readme

HTML Splitter

在一些场景(例如博客文章缩略内容展示)下,我们需要对一段 HTML 代码进行分片。 但是简单的字符串裁剪后的 HTML 显然不会是合法的 HTML 代码,所以 HTML Splitter 的目标就是为了解决这个问题。

安装

npm install html-spliter

使用

import { Splitter } from '../src';

const splitter = new Splitter({
    isVoidTag: (tag) => ['img'].indexOf(tag) >= 0, // self-closing tag
    isUnsplittableTag: (tag: string) => ['img'].indexOf(tag) >= 0,
});

const content = '<div>如图,在<tex>\\triangle ABC</tex>中,以<tex>C</tex>点为圆心<img src="xx" height="1" width="2"><p>这是段落,段落里还有<span>标</span>签</p></div>';
const res = splitter.split(content, 0, 60); // '<div>如图,在<tex>\\triangle ABC</tex>中,以<tex>C</tex>点为圆心</div>'

API

HTML Parser 是从 vue-next fork 后修改而来, 所以 ParserOptions 可以参考 @vue/compiler-core API。

interface SplitterOptions extends ParserOptions {
    /**
     * unsplittable tags e.g. img or custom tags
     */
    isUnsplittableTag?: (tag: string) => boolean;
    /**
     * fifo cache limit, default 10
     */
    cacheLimit?: number;
}

SplitterOptionsParserOptions 之上增加了两个配置项:

  • isUnsplittableTag 判断一个标签是否是可以拆分的
    • 例如 img 是无法拆分的,拆分后就会出现两个重复的 img
    • 一些自定义标签内部结构有着特殊含义,必须视作一个整体,这类也无法做拆分
  • cacheLimit splitter 内部的 parse 做了 FIFO 缓存,优化重复 split 一段 HTML 场景下的速度,该参数控制缓存的 limit,默认为 10