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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hankei6km/hast-util-trim-element

v0.1.0

Published

hast utility to trim an element

Downloads

7

Readme

hast-util-trim-element

Element(主に <p> を想定)から先頭と末尾の空白や改行(含む <br>)などを削除する hast ユーティリティ.

Install

npm install @hankei6km/hast-util-trim-element

Usage

import { h } from 'hastscript'
import { toHtml } from 'hast-util-to-html'
import { trimElement } from '@hankei6km/hast-util-trim-element'

const tree = h('p', [{ type: 'comment', value: 'コメント' }, '\u00A0\u00A0\u00A0\u00A0', h('span', []), '   ', h('br'), h('strong', '  \u3000あいうえお'), h('br'), 'かきくけこ', h('br'), h('strong', ['さしすせそ   ']), h('br'), '   '])

console.log(toHtml(tree))

trimElement(tree)

console.log(toHtml(tree))

yield:

<p><!--コメント-->    <span></span>   <br><strong>   あいうえお</strong><br>かきくけこ<br><strong>さしすせそ   </strong><br>   </p>
<p><!--コメント--><strong>あいうえお</strong><br>かきくけこ<br><strong>さしすせそ</strong></p>

API

trimElement(element, [options])

element 内の先頭または末尾の空白や改行を削除する。elementhastElement ノードまたは Element['children'] の配列。

options.recursiveTagNames

element 内の子ノードで options.recursiveTagNames に含まれるタグ名を持つ要素があった場合、その子要素も再帰的にトリムする、null で全て無視(RegExp または hast-util-is-elementTest、デフォルトは /^(a|strong|em|span|del|u|s|b|sub|sup)$/iu)。

options.trimExistingEmptyText

element 内の子ノードをトリムする際に、既に空のテキストノード({"type": "text", "value": ""})もトリムするか?しない場合はそこで処理を停止する(boolean、デフォルトは true)。

options.trimExistingEmptyElement

element 内の子ノードをトリムする際に、既に空の要素もトリムするか?しない場合は空の要素を残して処理を継続する(boolean、デフォルトは true)。

options.trimBecameEmptyElement

element 内の子ノードをトリムする際に、再帰的なトリムにより空になった要素もトリムするか?しない場合は空の要素を残して処理を継続する(boolean、デフォルトは true)。

options.startFilter

element 内の先頭からトリムする際に、挙動をカスタマイズするためのフィルター。

startFilter のフィールド
  • trimTextMatcher: 先頭のテキストノードからトリムするテキストを決定するマッチャー(RegExp、デフォルトは /^\s+/u)。
  • trimTagNameMatcher: 先頭のelementノードをトリムするかを決定するマッチャー、null で全て無視(RegExp または hast-util-is-elementTest、デフォルトは /^br$/iu)。
  • stopTagNameMatcher: 先頭のelementノードを見つけた際に、トリムを停止するかを決定するマッチャー、null で全て無視(RegExp または hast-util-is-elementTest、デフォルトは /^(img|hr|input|meta|link|area|base|col|embed|source|track|wbr)$/iu)。他の条件より優先される(例: 子要素が 0 個は継続されるが、stopTagNameMatcher にマッチした場合は処理が停止される)。

options.endFilter

element 内の末尾からトリムする際に、挙動をカスタマイズするためのフィルター。

endFilter のフィールド
  • trimTextMatcher: 末尾のテキストノードからトリムするテキストを決定するマッチャー(RegExp、デフォルトは /\s+$/u)。
  • trimTagNameMatcher: 末尾のelementノードをトリムするかを決定するマッチャー、null で全て無視(RegExp または hast-util-is-elementTest、デフォルトは /^br$/iu)。
  • stopTagNameMatcher: 末尾のelementノードを見つけた際に、トリムを停止するかを決定するマッチャー、null で全て無視(RegExp または hast-util-is-elementTest、デフォルトは /^(img|hr|input|meta|link|area|base|col|embed|source|track|wbr)$/iu)。他の条件より優先される(例: 子要素が 0 個は継続されるが、stopTagNameMatcher にマッチした場合は処理が停止される)。

License

MIT License

Copyright (c) 2025 hankei6km