3.2.4 • Published 2 months ago

hugo-lunr-ml v3.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

NPM version npm-typescriptLicense]github-license-urlInstall size]install-size-url

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];
3.2.4

2 months ago

3.2.0

2 months ago

3.1.1

3 months ago

3.1.0

12 months ago

3.0.2

1 year ago

3.0.1

1 year ago

2.3.0

1 year ago

2.3.1

1 year ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago