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

@barnabask/astro-minisearch

v2.1.1

Published

Utilities to add a static full text index to an Astro project

Downloads

742

Readme

@barnabask/astro-minisearch

@barnabask/astro-minisearch

This package adds MiniSearch support to your Astro project. It contains a rehype plugin that helps extract text from Markdown and MDX files. It also contains helper functions for generating and loading a static JSON search index.

The complete API documentation is here.

Installation

Add the package to your project:

npm install @barnabask/astro-minisearch

Adjust your astro.config.* file to add plainTextPlugin to rehypePlugins as follows:

astro.config.mjs

import { defineConfig } from "astro/config";
import { plainTextPlugin } from "@barnabask/astro-minisearch";

export default defineConfig({
  site: "http://example.com",
  markdown: {
    rehypePlugins: [plainTextPlugin()]
  },
});

Once the plugin is installed and configured, all Markdown pages and content collections should have a new frontmatter property. You can inspect it with Astro's <Debug /> component. After configuring this plugin and adding the following line to your page or layout:

<Debug {frontmatter.plainText} />

...you may see some output like this (obviously with your own content):

[
  [
    "",
    "Welcome to Astro! This is the docs starter template...."
  ],
  [
    "Getting Started",
    "To get started with this theme, check out the README.md..."
  ]
]

Configuration success! Congratulations.

Generating a search index

Create a JSON endpoint file under your src/pages/ directory named search.json.js or similar. This example adds local static markdown files and two Astro content collections to a search index:

import { getCollection } from "astro:content";
import {
  getSearchIndex,
  pagesGlobToDocuments,
  collectionToDocuments,
} from "@barnabask/astro-minisearch";

export async function get() {
  return await getSearchIndex([
    pagesGlobToDocuments(import.meta.glob(`./**/*.md*`)),
    collectionToDocuments(getCollection("articles"), "/articles/"),
    collectionToDocuments(getCollection("blog"), "/blog/"),
  ]);
}

Refer to the API documentation for more info on getSearchIndex, pagesGlobToDocuments, and collectionToDocuments.

Next steps

This package is only for generating a static search index file when your Astro site is built. To actually do the search at runtime, you'll either need some client-side JavaScript or you'll need to enable SSR and render search results on the server.

This package does provide a function called loadIndex as a convenience for SSR scenarios. See the pages and endpoints in the source code demo directory for a working example with the standard Astro blog template.

Frontmatter

You can still generate a the search index without the plaintext plugin. Or you may want to control the searchable content for a specific page, or prevent a page from being included in the search index later. In those cases you should supply a plainText frontmatter property (or whatever property name you standardize on) manually. The plaintext plugin will not overwrite manually specified plaintext properties.

To replace the text, you can specify the plaintext value as a string, like this:

plainText: All about the aardvark.
This page is about a certain burrowing mammal native to Africa.

If your page has multiple sections, you can use a YAML array of arrays. Each sub item in the list has a header first, then the rest of the text after. The "header" for text that comes before any headers can be a blank string. For example:

plainText:
- - ''
  - This is the top of the page
- - Section 1
  - Here is some content for section 1.
- - Section 2
  - This is other text for section 2.
This is the top of the page.

## Section 1

Here is some content for section 1.

## Section 2

This is other text for section 2.

To prevent a page from being added to the search index, set the plaintext field to false or a blank string.

plainText: false