hugo-lunr-ml

3.1.0 • Public • Published

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

Installation

Install the hugo-lunr-ml utility via npm:

$ npm install hugo-lunr-ml

Usage

The easiest way to use hugo-lunr is via npm scripts:

package.json

  "scripts": {
    "create-index": "hugo-lunr-ml",
    "create-index-io": "hugo-lunr-ml -i "content/**" -o static/my-index.json"
  },

Options

By default module will read the content directory of you and output the lunr index to lunr-index.json.

-i  set input path to parse (default: content/**)
-o  set output index file path (default: /static/seacrh/index.json')
-l  set default language. Will use this code ( [.en, .ru etc] in the index.json(default: system language) )
-ol  set output lunr index file path (default: /static/seacrh/lunr-index.json')

Execute

$ npm run create-index

Example of result lunr-index.json file:

{
  "ru": { ...some lunr staff... },
  "en": { ...some lunr staff... },
  "contentMap": {
    "ru": { "/posts/post-sub01": "Test post 01 Ru" },
    "en": { "/posts/post-sub01": "Test post 01 Eng" }
  }
}

Example of result index.json file:

{
    "ru": [
        {
            "uri": "/posts/post-1",
            "title": "Test post 01 Ru",
            "content": "\nTest post",
            "tags": [],
            "lang": "ru"
        },
        {
            "uri": "/posts/post-2",
            "title": "Test post 02 Ru",
            "content": "\nTest post",
            "tags": [],
            "lang": "ru"
        }
    ],
    "en": [
        {
            "uri": "/posts/post-1",
            "title": "Test post 01 Eng",
            "content": "\nTest post",
            "tags": [],
            "lang": "en"
        }
    ]
}

How to connect with lunr.js

  1. Import/Fetch lunr-index.json
  2. Search

How to use this lunr-index.json witn lunr.js

npm install lunr

or in the Hugo template:

<script src="https://unpkg.com/lunr/lunr.js"></script>
const pagesStore = {} // need to map later title and uri Ex: {"/local-href": "post title"}
const getIndexData = async () => {
	let response = await fetch(`/search/lunr-index.json`)
	if (response.status != 200) {
		throw new Error("Server Error");
	}
	// read response stream as text
	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');

// get first found page title
const foundUri = searchResultRu[0].ref;
const foundPageTitle = pagesStore[foundUri];

Install

npm i hugo-lunr-ml

DownloadsWeekly Downloads

83

Version

3.1.0

License

MIT

Unpacked Size

24 kB

Total Files

14

Last publish

Collaborators

  • romankurnovskii