site-search

A lightweight self-hosted alternative to DocSearch

Usage no npm install needed!

<script type="module">
  import siteSearch from 'https://cdn.skypack.dev/site-search';
</script>

README

site-search

A lightweight self-hosted alternative to DocSearch.

site-search will run your website locally, crawl its pages and index their content in a lunr index. It also provides a node.js request handler that can be initialized with this index to provide a search endpoint for your website.

How to use

  1. Install the package
yarn add -D site-search
  1. Add a site-search.config.js to the root of your project containing
module.exports = {
  siteStartCmd: `yarn start`,
  siteOrigin: 'http://localhost:3000',
  startUrl: '/',
  outputPath: './site-search-index.json',
  rules: [
    {
      hierarchy: [
        { selector: 'h1' },
        { selector: 'h2' },
        { selector: 'h3' },
        { selector: 'h4' },
      ],
      text: { selector: 'p' },
    },
  ],
};
  1. Run site-search:

  2. Add the search endpoint to your app:

const path = require('path');
const handler = require('site-search/handler');
// ...
app.use(
  '/search',
  handler({
    filename: path.resolve(__dirname, '../site-search-index.json'),
  })
);
  1. Now you can use /search?q=foo to find document matching "foo"

Config

siteStartCmd: Command that should be run to start your website siteOrigin: Url of where the running website can be reached startUrl: Url where crawling should start outputPath: Where to store the resulting index data rules: Rules to extract hierarchy. A bit similar to how Algolia does it