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 |
|
16 | Install 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 |
|
24 | The 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 |
|
36 | By 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 |
|
100 | 1. Import/Fetch lunr-index.json
|
101 | 2. Search
|
102 |
|
103 | How to use this `.json` witn lunr.js
|
104 |
|
105 | [How I use this index](https://romankurnovskii.com/en/posts/hugo-add-search-lunr-popup/#connect-searchresult-forms-with-lunrjs-search)
|
106 |
|
107 | ```sh
|
108 | npm install lunr
|
109 | ```
|
110 |
|
111 | or in the Hugo template:
|
112 |
|
113 | ```html
|
114 | <script src="https://unpkg.com/lunr/lunr.js"></script>
|
115 | ```
|
116 |
|
117 | ```javascript
|
118 | const pagesStore = {} // need to map later title and uri
|
119 | const getIndexData = async () => {
|
120 | let response = await fetch(`/search/lunr-index.json`)
|
121 | if (response.status != 200) {
|
122 | throw new Error("Server Error");
|
123 | }
|
124 | // read response stream as text
|
125 | let text_data = await response.text();
|
126 | const idxData = JSON.parse(text_data)
|
127 | const lngIdx = idxData[languageMode]
|
128 | const idx = lunr.Index.load(lngIdx)
|
129 | pagesStore = idxData['contentMap'][languageMode]
|
130 | return idx
|
131 | }
|
132 |
|
133 |
|
134 | const idx = await getIndexData()
|
135 | const results = idx.search('my search query');
|
136 |
|
137 | ```
|
138 |
|
139 |
|
140 | ## Notes
|
141 |
|
142 | ### 2.0.1
|
143 |
|
144 | Creates stringified lunr-index. Now no need to create index every time on search request. Just need to fetch lunr-index.
|
145 | Index generation for 100.000 pages took 2min *once* during build. Search of popular query takes < 0.3sec
|
146 |
|
147 |
|
148 | [npm-url]: https://www.npmjs.com/package/hugo-lunr-ml
|
149 | [npm-image]: https://img.shields.io/npm/v/hugo-lunr-ml
|
150 | [github-license]: https://img.shields.io/github/license/romankurnovskii/hugo-lunr-ml
|
151 | [github-license-url]: https://github.com/romankurnovskii/hugo-lunr-ml/blob/main/LICENSE
|
152 | [npm-typescript]: https://img.shields.io/npm/types/hugo-lunr-ml
|
153 | [install-size]: https://packagephobia.com/badge?p=hugo-lunr-ml
|
154 | [install-size-url]: https://packagephobia.com/result?p=hugo-lunr-ml
|
155 |
|
\ | No newline at end of file |