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 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-fragments

v0.2.1

Published

> [Text Fragments](https://web.dev/text-fragments) and [Selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection) utilities.

Downloads

11

Readme

text-fragments

Text Fragments and Selection utilities.

Features

  • Normalized Selection
  • Create excerpt from selected text
  • Efficient Text Fragments generation
  • Lightweight (~1kB minified gzipped)
  • Optional React Bindings

APIs

Selection

This is not intended to be a one-stop shop for Selection and Range, but rather few useful function related to Text Fragments generation. For selection-related utilities, it will use LiteSelection with type as follows

interface LiteSelection {
  anchorNode: Node | null;
  anchorOffset: number;
  focusNode: Node | null;
  focusOffset: number;
}

getSelection(): LiteSelection | null

This function return a normalized Selection in the form of LiteSelection, or null if it's a collapsed selection or not a range selection.

import { getSelection } from 'text-fragments';

const selection = getSelection();

if (selection) {
  // do something with the selection
}

Because this is normalized, you can be sure that anchorNode is always point to the element before focusNode, and in case where it points to the same node, anchorOffset will always be less than focusOffset.

getSelectedText(selection: LiteSelection, options?: SelectedTextOptions): string

Return the text inside a selection. It accepts an optional options that you can use to pass maxLength or moreTextIndicator. This can be useful to create an excerpt or limit the

import { getSelection, getSelectedText } from 'text-fragments';

const selection = getSelection();
const text = getSelection(selection, {
  maxLength: 100, // defaults to infinity / ignored
  moreTextIndicator: '', // defaults to ...
});

Text Fragments

There are 2 main function to generate Text Fragments, one with hash & one without. The algorithm used to generate Text Fragments is optimized for smallest string possible with the high chance of correctly highlighting a text. It might highlight the wrong portion of the text but the chances are pretty slim.

getTextFragmentsWithHash(selection: LiteSelection): string

Return text fragments string in the form of location hash. You can immediately use this by appending to current URL

import { getSelection, getTextFragmentsWithHash } from 'text-fragments';

const selection = getSelection();
const hash = getTextFragmentsWithHash(selection);
const url = window.location.href + hash;
// do something with the url

getTextFragments(selection: LiteSelection): string

Same as before, but without the #:~:text= prefix.

import { getSelection, getTextFragmentsWithHash } from 'text-fragments';

const selection = getSelection();
const textFragments = getTextFragments(selection);

Optional React bindings

There's also an optional React binding in the form of hooks that you can import via text-fragments/react

useSelection(): LiteSelection | null

The value returned by useSelection automatically reflects current selection and updates whenever user changes their selection in the document.

import { getSelectedText } from 'text-fragments';
import { useSelection } from 'text-fragments/react';

const selection = useSelection();

return (
  <div>
    {selection && <p>Currently selecting {getSelectedText(selection)}</p>}
  </div>
);

useTextFragments(options?: { includeHash: boolean }): string | null

Get Text Fragments based on current selection. Similar to useSelection this value is automatically updated.

import { useTextFragments } from 'text-fragments/react';

const hash = useTextFragments({ includeHash: true });

function handleShare() {
  window.open(window.location.href + hash);
}

return (
  <div>
    <button onClick={handleShare}>Preview</button>
    <Paragraph />
  </div>
);

Related Projects

If you want a more robust library you can use fragment-generation-utils from text-fragments-polyfill. It weighs in about 7kB minified gzipped.

License

MIT