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

svelte-codemirror-search-field

v0.11.0

Published

Svelte search field powered by CodeMirror with tag autocomplete and conceal/pill rendering

Readme

svelte-codemirror-search-field

A Svelte search field component that uses CodeMirror and the conceal mechanism to highlight tags and boolean operators as pills, with autocompletion on #.

Installation

pnpm add svelte-codemirror-search-field

Usage

<script>
  import SearchField from 'svelte-codemirror-search-field';
</script>

<SearchField doc="#linux #git" tags={['linux', 'git', 'nginx']} />

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | doc | string | '' | Document content (controlled; external changes replace the doc in place) | | ph | string | 'Search...' | Placeholder text when empty | | tags | string[] \| (() => Promise<string[]>) | null | Tag list for autocomplete (null = disabled) | | matchTags | (query: string, tag: string) => number \| null | null | Custom tag filtering/ranking for autocomplete; score >= 0 = match, higher score ranks first; null keeps the default startsWith behavior | | threshold | number | 0 | Distance in chars before conceal activates | | autocompleteMinChars | number | 0 | Min chars after # before autocomplete triggers | | autocompleteDebounceMs | number | 200 | Debounce delay for autocomplete popup | | showImplicit | boolean | false | Show implicit AND/OR between consecutive tags | | implicitOp | 'and' \| 'or' | 'and' | Default implicit operator | | implicitAutoCloseParents | boolean | false | Show a virtual ) for unclosed parentheses and treat them as closed for SQL generation | | onchange | (value: string) => void | () => {} | Called on every document change |

Tags with spaces

Tags are written #name. A tag may contain spaces using the quoted form #"foo bar" — the whole #"foo bar" is rendered as a single pill, and the underlying tag name is foo bar. Autocomplete quotes a tag automatically when you pick one that contains a space. Allowed characters inside the quotes: letters, digits, _ - . ' and spaces.

Custom tag matching with matchTags

By default tags are filtered with a case-insensitive startsWith and listed in their original order. Pass a matchTags hook to filter and rank tags yourself: it receives the query text after # and a tag, and returns a score (>= 0) when the tag matches — higher scores rank first, ties keep the original list order — or null when it does not match.

The minimal fuzzy matcher below matches any subsequence of the tag:

// Case-insensitive subsequence match: score = number of matched characters.
function subsequenceMatch(query, tag) {
  const lowerQuery = query.toLowerCase();
  const lowerTag = tag.toLowerCase();
  let index = 0;
  for (const char of lowerQuery) {
    index = lowerTag.indexOf(char, index);
    if (index === -1) return null;
    index += 1;
  }
  return lowerQuery.length;
}
<script>
  import SearchField from 'svelte-codemirror-search-field';

  const tags = ['postgresql', 'sqlite', 'mysql', 'nginx'];
</script>

<SearchField doc="#sql" tags={tags} matchTags={subsequenceMatch} />

Typing #sql now suggests postgresql and mysql in addition to sqlite.

Example with async tag source

<script>
  import SearchField from 'svelte-codemirror-search-field';

  const fetchTags = async () => {
    const res = await fetch('/api/tags');
    return res.json();
  };
</script>

<SearchField
  doc="#linux or #git"
  tags={fetchTags}
  threshold={2}
  showImplicit
  implicitOp="or"
/>

Controlled doc

doc is a controlled prop: external changes (e.g. navigation carrying a new query string) replace the document in place, without recreating the editor. The replacement is skipped while the field has focus, so debounced typing is never clobbered by an in-flight navigation — the editor is the source of truth until the user leaves the field.

Parser sub-export

The framework-agnostic search DSL parser is exposed as a sub-export for server-side reuse (e.g. to build a DNF for SQL predicates):

import { parse } from 'svelte-codemirror-search-field/parser';

const { ast, errors, elements } = parse('#linux and #git');

It depends only on chevrotain and has no Svelte or CodeMirror dependency, so it runs in plain Node.

License

MIT