UNPKG

3.73 kBMarkdownView Raw
1<h1 align="center">hugo-lunr-ml</h1>
2<p align="center">Package for multilingual (or not) hugo site</p>
3<p align="center">Generates <b>ready to use</b> by lunr.js `lunr-index.json` file.</p>
4
5<p align="center">
6 <img src="https://github.com/romankurnovskii/hugo-lunr-ml/raw/main/img/hugo-lunr-ml.png" alt="Hugo Lunr Multilanguage package">
7</p>
8
9[![NPM version][npm-image]][npm-url]
10![npm-typescript]
11[![License][github-license]][github-license-url]
12[![Install size][install-size]][install-size-url]
13
14## Installation
15
16Install the hugo-lunr-ml utility via [npm](https://www.npmjs.com/package/hugo-lunr-ml):
17
18```
19$ npm install hugo-lunr-ml
20```
21
22## Usage
23
24The easiest way to use hugo-lunr is via npm scripts:
25
26**package.json**
27```json
28 "scripts": {
29 "create-index": "hugo-lunr-ml",
30 "create-index-io": "hugo-lunr-ml -i "content/**" -o static/my-index.json"
31 },
32```
33
34### Options
35
36By default module will read the `content` directory of you and output the lunr index to `lunr-index.json`.
37
38```
39-i set input path to parse (default: content/**)
40-o set output index file path (default: /static/seacrh/index.json')
41-l set default language. Will use this code ( [.en, .ru etc] in the index.json(default: system language) )
42-ol set output lunr index file path (default: /static/seacrh/lunr-index.json')
43```
44
45### Execute
46
47```sh
48$ npm run create-index
49```
50
51
52**Example of result `lunr-index.json` file:**
53
54```json
55{
56 "ru": { ...some lunr staff... },
57 "en": { ...some lunr staff... },
58 "contentMap": {
59 "ru": { "/posts/post-sub01": "Test post 01 Ru" },
60 "en": { "/posts/post-sub01": "Test post 01 Eng" }
61 }
62}
63```
64
65**Example of result `index.json` file:**
66
67```json
68{
69 "ru": [
70 {
71 "uri": "/posts/post-1",
72 "title": "Test post 01 Ru",
73 "content": "\nTest post",
74 "tags": [],
75 "lang": "ru"
76 },
77 {
78 "uri": "/posts/post-2",
79 "title": "Test post 02 Ru",
80 "content": "\nTest post",
81 "tags": [],
82 "lang": "ru"
83 }
84 ],
85 "en": [
86 {
87 "uri": "/posts/post-1",
88 "title": "Test post 01 Eng",
89 "content": "\nTest post",
90 "tags": [],
91 "lang": "en"
92 }
93 ]
94}
95```
96
97## How to connect with [lunr.js](https://lunrjs.com/)
98
99
1001. Import/Fetch lunr-index.json
1012. Search
102
103How to use this `lunr-index.json` witn lunr.js
104
105```sh
106npm install lunr
107```
108
109or in the Hugo template:
110
111```html
112<script src="https://unpkg.com/lunr/lunr.js"></script>
113```
114
115```javascript
116const pagesStore = {} // need to map later title and uri Ex: {"/local-href": "post title"}
117const getIndexData = async () => {
118 let response = await fetch(`/search/lunr-index.json`)
119 if (response.status != 200) {
120 throw new Error("Server Error");
121 }
122 // read response stream as text
123 let textData = await response.text();
124 const idxData = JSON.parse(textData)
125 const lngIdx = idxData[languageMode]
126 const idx = lunr.Index.load(lngIdx)
127 pagesStore = idxData['contentMap'][languageMode]
128 return idx
129}
130
131const idx = await getIndexData()
132const results = idx.search('my search query');
133
134// get first found page title
135const foundUri = searchResultRu[0].ref;
136const foundPageTitle = pagesStore[foundUri];
137```
138
139
140
141
142[npm-url]: https://www.npmjs.com/package/hugo-lunr-ml
143[npm-image]: https://img.shields.io/npm/v/hugo-lunr-ml
144[github-license]: https://img.shields.io/github/license/romankurnovskii/hugo-lunr-ml
145[github-license-url]: https://github.com/romankurnovskii/hugo-lunr-ml/blob/main/LICENSE
146[npm-typescript]: https://img.shields.io/npm/types/hugo-lunr-ml
147[install-size]: https://packagephobia.com/badge?p=hugo-lunr-ml
148[install-size-url]: https://packagephobia.com/result?p=hugo-lunr-ml