Skip to main content
Works with
JSR Score
23%
Published
2 months ago

hugo-lunr-ml

Package for multilingual (or not) hugo site

Generates ready to use by lunr.js `lunr-index.json` file.

Hugo Lunr Multilanguage package

NPM version npm-typescript License Install size

hugo-lunr-ml npm package designed to enhance your Hugo site with powerful, multilingual search capabilities. This package automatically generates a lunr-index.json file that is ready to integrate with lunr.js, providing a seamless search experience for your Hugo site, whether it's multilingual or not.

Features

  • Multilingual Support: Generate search indexes in multiple languages for your Hugo site.
  • Easy Integration: Directly integrates with lunr.js for a powerful search experience.
  • Customizable: Offers options for custom input paths, output paths, and default languages to fit your site's structure.

Getting Started

Installation

Install the hugo-lunr-ml utility via npm:

npm install hugo-lunr-ml

Adding to Your Project

Add the following scripts to your package.json file. This script simplifies the generation of your search index.

package.json

  "scripts": {
    "create-index": "hugo-lunr-ml"
  },

Configuration Options

hugo-lunr-ml can be customized through various command-line options to suit your project's needs:

  • -i: Set the input path to parse (default: content/**)
  • -o: Set the output path for the index file (default: /static/search/index.json)
  • -l: Set the default language. Utilizes language codes (e.g., [.en, .ru, etc] in the index.json (default: system language) )
  • -ol: Set the output path for the lunr index file (default: /static/seacrh/lunr-index.json)

Generating the Index

To generate your site's search index, execute the npm script you've added:

npm run create-index

Integrating with lunr.js

After generating the lunr-index.json, you can easily integrate it with lunr.js by either installing lunr via npm:

npm install lunr

or including lunr.js in your Hugo template:

<script src="https://unpkg.com/lunr/lunr.js"></script>

Use the following JavaScript snippet to fetch and utilize the lunr-index.json for search:

let pagesStore = {}; // Mapping for titles and URIs, e.g., {"/local-href": "post title"}
const getIndexData = async () => {
  let response = await fetch(`/search/lunr-index.json`);
  if (response.status !== 200) {
    throw new Error("Server Error");
  }
  let textData = await response.text();
  const idxData = JSON.parse(textData);
  const lngIdx = idxData[languageMode];
  const idx = lunr.Index.load(lngIdx);
  pagesStore = idxData['contentMap'][languageMode];
  return idx;
}

const idx = await getIndexData();
const results = idx.search('my search query');

// Example: Retrieve the first found page title
const foundUri = results[0].ref;
const foundPageTitle = pagesStore[foundUri];

Add Package

deno add @romankurnovskii/hugo-lunr-ml

Import symbol

import * as mod from "@romankurnovskii/hugo-lunr-ml";

Add Package

npx jsr add @romankurnovskii/hugo-lunr-ml

Import symbol

import * as mod from "@romankurnovskii/hugo-lunr-ml";

Add Package

yarn dlx jsr add @romankurnovskii/hugo-lunr-ml

Import symbol

import * as mod from "@romankurnovskii/hugo-lunr-ml";

Add Package

pnpm dlx jsr add @romankurnovskii/hugo-lunr-ml

Import symbol

import * as mod from "@romankurnovskii/hugo-lunr-ml";

Add Package

bunx jsr add @romankurnovskii/hugo-lunr-ml

Import symbol

import * as mod from "@romankurnovskii/hugo-lunr-ml";