@hypericon/locust

0.4.2 • Public • Published

Locust 🦗

Local client-side search for static sites.

Inspired by Algolia, uses Fuse.js internally.

Locust screenshot with example data

Sample data from: https://github.com/benoitvallon/100-best-books

Quickstart

Install from NPM, including Typescript types: npm install @hypericon/locust

Prepare JSON data

Generate the JSON data with: npx locust -i ./data/input-file.json -o ./site/locust-data.json

More options and config file format are detailed below.

Search the data

Include the client script, for example:

<button id="locust-search">Search</button>

<script defer src="https://unpkg.com/@hypericon/locust/dist/lib/locust.umd.cjs"></script>
<link rel="stylesheet" href="unpkg.com/@hypericon/locust/dist/lib/style.css">

<script defer>
  setupLocustSearch({
    // Where to load the prepared data from
    jsonData: "/locust-data.json",
    // Which keys in the data objects should be indexed
    searchKeys: [
      { name: "title", weight: 2 },
      "author",
      "language",
    ],
    // How the results should be displayed
    modalResult: {
      thumbnail: "imageLink",
      fields: [
        { key: "title", style: "large" },
        { key: "author" },
        { key: "language", style: "small" },
        { text: (record) => `Year: ${record.year}`, style: "small" },
      ],
    },
  });
</script>

More options are detailed below.

Note: If you only want to consume the search results programmatically and use your own UI, you're probably better off just using Fuse.js on its own.

Overview

Locust includes two components:

  • CLI tool to prepare data
  • UI component to perform searches

Once the data is prepared, the JSON file is generally written to the the output directory of the static site. It can also be made available over HTTP from an existing server (not included).

The UI component uses the generated data to serve search requests. The UI component can load the data lazily (default) or aggressively.

Prepare Data

Locust includes tools to extract data from the following sources:

  • Javascript objects already in memory
  • HTML, Markdown, or JSON files stored locally
  • HTML, Markdown, or JSON files retrieved via HTTP

Search Data

Development

Clone the repo, install dependencies with npm install

The CLI and UI components are built separately

  • npm run dev-cli to start compiling the Typescript CLI source in watch mode
  • npm run dev-lib to start a hot-reloading local dev server serving an example page containing the UI
  • npm run dev to run both of the above scripts concurrently

The build scripts are similar:

  • npm run build-cli to compile the CLI Typescript, including types and maps in the output
  • npm run build-lib to build and minify the UI library
  • npm run build to run both of the above scripts sequentially

Dev Todo

  • Page > narrow screen mode

  • Grouping results

    • apply a function specified in the client settings to group results
    • results are grouped visually to tidy up the result list
    • How are results grouped if they differ in relevance?
  • Add option to reload the data each time the modal is opened, or after a timeout

    • for more dynamic data loaded from an API instead of a static JSON file
  • Correctly build types for UI component as Vite doesn't when in library mode

Readme

Keywords

none

Package Sidebar

Install

npm i @hypericon/locust

Weekly Downloads

19

Version

0.4.2

License

MIT

Unpacked Size

1.31 MB

Total Files

23

Last publish

Collaborators

  • dyenamic
  • neonfish
  • unany