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

hugo-lyra

v0.4.2

Published

Node module for creating lyra search indexes for static Hugo sites

Downloads

2

Readme

Hugo-Lyra

Tests

Hugo is a fantastic static site generator and, like most of them, is not natively capable of implementing a dynamic search engine like we are used to using on more traditional server-side platforms, with more or less advanced query capabilities.

This project aims to solve this problem by integrating the Lyra search engine with Hugo.

It works by parsing the content/\*\*/\*.md files into raw text and using it to generate a Lyra index, which will become a static asset you can publish alongside the other files of Hugo public dist.

This project also offers a client-side javascript library to easily consume the index from your Hugo website; you can see it in action on my personal blog here and here for the implementation. You can jump to the client section for more details.

The parser can parse just Markdown files, and standard front-matter elements, arrays like tags, and categories are imploded and space-separated.

Schema definition:

const hugoDb = create({
  schema: {
    title: "string",
    body: "string",
    uri: "string",
    meta: {
      date: "string",
      description: "string",
      tags: "string",
      categories: "string",
      keywords: "string",
      summary: "string",
    },
  },
  defaultLanguage: "english",
});

Client side

You need to import the library from the CDN:

<script src=https://unpkg.com/hugo-lyra@latest/dist/browser/hugo-lyra.js></script>

Or better using Hugo Pipes on your base template:

{{ $script := resources.GetRemote https://unpkg.com/hugo-lyra@latest/dist/browser/hugo-lyra.js | minify | fingerprint }}
<script src="{{ $script.RelPermalink }}" integrity="{{ $script.Data.Integrity }}"></script>

The client code bundles the Lyra search index code to ensure that generated index is compatible on the client and server side; you do not need to import it separately. The limitation of this approach is that you cannot change the Lyra version.

Once instantiated, you'll find a new global object on the window.HugoLyra global object you can use to implement your search engine.

How you'll use it it's up to you to better fit your needs; you can find below a straightforward raw javascript implementation you can use as a starting point:

(async () => {
  try {
    const params = new URLSearchParams(window.location.search);
    const query = params.get("q");
    if (query) {
      const db = await HugoLyra.fetchDb(`/hugo-lyra-english.json`);
      const res = await HugoLyra.search(db, { term: query, properties: "*" });
      document.getElementById("search-input").setAttribute("value", res.options.term);
      let resultList = "";
      const searchResults = document.getElementById("results");
      if (res?.search?.count) {
        for (const hit of res.search.hits) {
          const doc = hit.document;
          resultList += "<li>";
          resultList += '<span class="date">' + doc.meta.date + "</span>";
          resultList += '<a class="title" href="' + doc.uri + '">' + doc.title + "</a>";
          resultList += "</li>";
        }
      }
      searchResults.innerHTML = resultList.length ? resultList : "No results found";
    }
  } catch (e) {
    console.error(e);
  }
})();

This code is part of my blog; you can dig it more here.

Server side

npm i paolomainardi/hugo-lyra

yarn add paolomainardi/hugo-lyra

Usage

Hugo-Lyra is simple to use.

Once installed, you can integrate it into your package.json in the scripts section like this:


"scripts": {
  "index": "hugo-lyra"
}

By default, Hugo-Lyra will read the content directory of you and output the Lyra index to public/hugo-lyra-english.msp.

You can tweak some configurations (like input, output, language, format, ecc.) by passing input arguments to the cli; you can see all the options by running:

npx hugo-lyra -h

Of course, you can regenerate the index programmatically just by running:

npx hugo-lyra

> TIP: If you run it with DEBUG=hugo-lyra npx hugo lyra, you can make profit

> of extra debugging logs.

For instance, assuming we want to:

  • Generate the index in JSON format

  • Save it on dist/search

  • Parse content/posts

The command will be: npx --yes hugo-lyra --content content/posts --indexFormat json --indexFilePath output/search

TIP: If you run it with DEBUG=hugo-lyra npx hugo lyra, you can make profit

How to use hugo-lyra API

Hugo-Lyra is packaged with ES modules, CommonJS.

In most cases, import or require paolomainardi/hugo-lyra, and your environment will choose the most appropriate build.

Sometimes, you may need to import or require specific files (such as types). The following builds are included in the Lyra package:

You can use it from Javascript/Typescript:

Typescript

import { cwd } from "process";
import { generateIndex } from "hugo-lyra";
import { LyraOptions } from "hugo\-lyra/dist/esm/types";

(async () => {
  const res = await generateIndex("./content", <LyraOptions>{
  indexFilePath: cwd(),
});

console.log(res);

})();

Javascript

const { cwd } = require("process");
const { generateIndex } = require("hugo-lyra");

(async () => {
  const res = await generateIndex("./content", {
    indexFilePath: process.cwd(),
  });
  console.log(res);
})();