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

@knaw-huc/text-annotation-segmenter

v0.8.0

Published

[![npm version](https://img.shields.io/npm/v/@knaw-huc/text-annotation-segmenter.svg?color=green)](https://www.npmjs.com/package/@knaw-huc/text-annotation-segmenter)

Readme

@knaw-huc/text-annotation-segmenter

npm version

Utility functions to render annotations with character position offsets in a text.

Annotations on a text have a non-hierarchical nature, i.e., they can overlap:

         text:  abc
annotation ab:  __
annotation bc:   __

However, HTML is hierarchical. How to display annotations that do not live inside or next to each other, but that cut across each other?

The segment function creates an array of segments: a flat, non-overlapping list where each segment links to both the text and all the annotations that apply. Each segment translates into a single dom/jsx element. Elements linked to multiple overlapping annotations can now be decorated with their own styling, classes and callbacks:

<span class="ab">a</span>
<span class="ab bc">b</span>
<span class="bc">c</span>

API

Functions

Types

  • SegmentGroup<T> A Group<T> (with annotation and recursive children) or Ungrouped<T> (with plain segments).
  • SegmentRange Start and end segment index of an annotation (excluding last segment).
  • TextSegment<T> Start and end positions of text segment (excluding last character), plus the annotations that apply.

Examples

Create text segments

A text 'abc' with two overlapping annotations at 'ab' and 'bc' will be split up in three segments:

         text:  abc
annotation ab:  __
annotation bc:   __
import {segment} from "@knaw-huc/text-annotation-segmenter";

const text = 'abc';
const ab = {id: 'ab', start: 0, end: 2};
const bc = {id: 'bc', start: 1, end: 3};

const getOffsets = annotation => annotation;

const segments = segment(text, [ab, bc], getOffsets);

expect(segments).toEqual([
  {index: 0, start: 0, end: 1, value: 'a', annotations: [ab]},
  {index: 1, start: 1, end: 2, value: 'b', annotations: [ab, bc]},
  {index: 2, start: 2, end: 3, value: 'c', annotations: [bc]},
]);

Group segments

We can use segments to build up a hierachy of elements:

Given the text 'ab' with a section spanning the whole text and a paragraph spanning the first half:

     text:  aabb
  section:  ____
paragraph:  __
import {segment, groupSegments} from "@knaw-huc/text-annotation-segmenter";

const text = 'aabb';
const section = {id: 'section', type: 'section', start: 0, end: 4};
const paragraph = {id: 'paragraph', type: 'paragraph', start: 0, end: 2};

const getOffsets = annotation => annotation;
const segments = segment(text, [section, paragraph], getOffsets);

const isGroup = a => a.type === 'section' || a.type === 'paragraph';
const getId = a => a.id;

const groups = groupSegments(
  segments,
  isGroup,
  getId,
);
const aaSegment = segments[0];
const bbSegment = segments[1];

expect(groups).toEqual([
  {annotation: section, isGroup: true, children: [
    {annotation: paragraph, isGroup: true, children: [
        {segments: [aaSegment], isGroup: false}
    ]},
    {segments: [bbSegment], isGroup: false},
  ]}
]);

Markers

A special case is the marker: an annotation of zero width marking a position in the text. Markers result in zero-width segments, including all annotations that cover, start or end at that position:

text with marker:  a‸b
paragraph 1:       _
paragraph 2:         _
const text = 'ab';
const marker: Annotation = {start: 1, end: 1, id: 'm'};
const p1: Annotation = {start: 0, end: 1, id: 'p1'};
const p2: Annotation = {start: 1, end: 2, id: 'p2'};

const segments = segment(text, [marker, p1, p2], getOffsets);
const markerSegment = segments[1];
expect(markerSegment).toEqual(
  {index: 1, start: 1, end: 1, value: '', annotations: [marker, p1, p2]},
);

Note how the marker segment contains both paragraphs as the marker borders both. We can filter the annotations of a marker segment, for example to control which block element contains the marker, using filterSegmentAnnotations. For example, if we would want to 'postfix' the marker, like a note at the end of a paragraph:

const result = filterSegmentAnnotations(segments, (annotation, segment) => {
  if (segment.start !== segment.end) {
    // Skip non-marker segments:
    return true;
  }
  if (annotation.type === 'marker') {
    // Keep marker in marker segments:
    return true;
  }
  // Keep annotations starting before marker:
  return annotation.start < segment.start;
});
const markerSegment = result[1];
expect(markerSegment).toEqual(
  {index: 1, start: 1, end: 1, value: '', annotations: [marker, p1]},
);

More examples: