Skip to content

Commit 3f8914c

Browse files
committed
chore: add 12-10-adding-searchbox-to-blog-site.md
1 parent 37ff4c5 commit 3f8914c

15 files changed

+179
-0
lines changed
Loading
49.1 KB
Loading
98.2 KB
Loading
63.3 KB
Loading
Loading
43.6 KB
Loading
Loading
52.4 KB
Loading
57.7 KB
Loading
27.9 KB
Loading
67.6 KB
Loading
Loading
Loading
37.4 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
title: "Adding Searchbox to Hugo!"
3+
meta_title: "Adding Searchbox to Hugo Blog!"
4+
description: "How to add a searchbox to your hugoblog site."
5+
date: 2024-12-10T05:00:00Z
6+
image: "/images/posts/2024/adding-searchbox-to-blog-site.png"
7+
categories: ["Welcome", "Blog"]
8+
author: "Medcl"
9+
tags: ["search", "searchbox", "hugo"]
10+
draft: false
11+
---
12+
13+
## What is INFINI Pizza?
14+
INFINI Pizza is an upcoming search engine developed by INFINI Labs, written in Rust (soon to be fully open-sourced). The basic search capabilities have already been completed, and based on the core engine of INFINI Pizza, a WASM version of the ultra-lightweight kernel is provided. This can be easily embedded into various application systems, such as websites, especially static sites or small blog systems.
15+
16+
Currently, The website of INFINI Pizza have integrated INFINI Pizza for WebAssembly. The specific search results are shown in the image below:
17+
18+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-1.png)
19+
20+
## How to use INFINI Pizza for WebAssembly?
21+
Visit the website ([http://pizza.rs](http://pizza.rs/)), and by pressing the shortcut key 's', you can bring up the search box and experience the search capabilities provided by INFINI Pizza. Notably, during the search process, all of your actions are executed locally in the browser. Unlike traditional search implementations, where each query requires an interaction with a backend search server, INFINI Pizza for WebAssembly operates entirely offline. Even if you're disconnected from the internet, you can still enjoy a seamless search experience.
22+
23+
Without further ado, let's dive into how you can use INFINI Pizza for WebAssembly on your own site.
24+
25+
First, INFINI Pizza for WebAssembly is open source. You can find the GitHub repository here: https://github.com/infinilabs/pizza-wasm. The compiled WASM package is available for direct download here: https://github.com/infinilabs/pizza-wasm/tree/main/pkg.
26+
27+
```shell
28+
➜ wasm git:(main) ✗ du -sh pkg/*
29+
4.0K pkg/README.md
30+
4.0K pkg/package.json
31+
4.0K pkg/pizza_wasm.d.ts
32+
4.0K pkg/pizza_wasm.js
33+
12K pkg/pizza_wasm_bg.js
34+
580K pkg/pizza_wasm_bg.wasm
35+
4.0K pkg/pizza_wasm_bg.wasm.d.ts
36+
256K pkg/pizza_wasm_bg.wasm.gz
37+
```
38+
39+
You'll notice that the WASM package is just over 500 KB, and after Gzip compression, it’s reduced to just over 200 KB, making it quite lightweight. You may have thinking there are some JavaScript based static search solution maybe more smaller, but please note they are not full featured search engine, and I will explain this when INFINI Pizza officially reveal sometime later.
40+
41+
Pizza-WASM is a WebAssembly interface wrapper for the core engine of INFINI Pizza, exposing only a few simple access interfaces. These are sufficient for current frontend search applications. You can find a very basic example of how to call the WASM methods in the following directory: https://github.com/infinilabs/pizza-wasm/tree/main/web.
42+
43+
However, just having the Pizza WASM isn't enough. If we want to add a search box to an existing static site, we also need to consider where the data comes from and how the results are displayed. To address this, we've wrapped up a project called Pizza-searchbox, which further simplifies usage. This project is also open source, and you can find it on GitHub here: https://github.com/infinilabs/pizza-searchbox.
44+
45+
Since the example project has already uploaded the compiled code and samples, we can directly download the source code and preview the functionality locally.
46+
47+
```shell
48+
➜ /tmp git clone https://github.com/infinilabs/pizza-searchbox.git
49+
Cloning into 'pizza-searchbox'...
50+
remote: Enumerating objects: 174, done.
51+
remote: Counting objects: 100% (174/174), done.
52+
remote: Compressing objects: 100% (112/112), done.
53+
remote: Total 174 (delta 86), reused 147 (delta 59), pack-reused 0 (from 0)
54+
Receiving objects: 100% (174/174), 941.94 KiB | 1.20 MiB/s, done.
55+
Resolving deltas: 100% (86/86), done.
56+
➜ /tmp cd pizza-searchbox/example/dist
57+
➜ dist git:(main) python3 -m http.server 8083
58+
59+
Serving HTTP on :: port 8083 (http://[::]:8083/) ...
60+
```
61+
62+
Open your browser and visit: [http://localhost:8083](http://localhost:8083/), as shown below:
63+
64+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-2.gif)
65+
66+
Observe the network requests in the browser, and you'll see that it loads the sample index.json data:
67+
68+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-3.png)
69+
70+
In practice, if it's our own static website or blog, simply ensuring that this file (index.json) with the appropriate format exists in the root directory of the site is enough to quickly integrate the search functionality you see into your own website. OK, with the functionality verified, let's start integrating it into our site.
71+
72+
The official website of Pizza/INFINI Labs is statically generated using Hugo, and the index.json file does not need to be manually created. First, we need to enable Hugo to generate content in JSON format, which is a built-in capability of Hugo. We'll need to modify the configuration of the Hugo project:
73+
74+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-4.png)
75+
76+
77+
Add a JSON output option to the outputs parameter, and then define the JSON output format template in the theme's template files:
78+
79+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-5.png)
80+
81+
82+
The text content is as follows for easy copying and pasting. Save the file with the name index.json:
83+
84+
```shell
85+
{{- $index := slice -}}
86+
{{- range where .Site.RegularPages.ByDate.Reverse "Type" "not in" (slice "page" "json") -}}
87+
{{- $index = $index | append (dict "title" (.Title | plainify) "url" .Permalink "tags" .Params.tags "category" .Params.category "subcategory" .Params.subcategory "summary" (.Params.Summary | markdownify | plainify) "content" (.Content | markdownify | plainify)) -}}
88+
{{- end -}}
89+
{{- $index | jsonify -}}
90+
```
91+
OK, next, we need to add the tags we’ve used above to the metadata of each article or blog post on the site:
92+
93+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-6.png)
94+
95+
96+
OK, start the Hugo site:
97+
98+
```shell
99+
hugo server --forceSyncStatic --minify --theme book
100+
101+
102+
| EN
103+
-------------------+------
104+
Pages | 181
105+
Paginator pages | 5
106+
Non-page files | 0
107+
Static files | 110
108+
Processed images | 0
109+
Aliases | 52
110+
Sitemaps | 1
111+
Cleaned | 0
112+
113+
Built in 323 ms
114+
Watching for changes in /Users/medcl/Documents/rust/pizza/website/{assets,content.en,static,themes}
115+
Watching for config changes in /Users/medcl/Documents/rust/pizza/website/config.yaml
116+
Environment: "development"
117+
Serving pages from memory
118+
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
119+
Web Server is available at //localhost:1313/ (bind address 127.0.0.1)
120+
Press Ctrl+C to stop
121+
```
122+
123+
Open the Hugo site address and try accessing http://localhost:1313/index.json. You should be able to access the JSON file:
124+
125+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-7.png)
126+
127+
## Integrate the search widget to Hugo
128+
129+
At this point, the data preparation is complete. Next, we'll integrate the frontend search widget.
130+
131+
Do you remember the resource files we downloaded from Pizza-searchbox earlier? We mainly use the three files in the assets folder:
132+
133+
```shell
134+
/tmp/pizza-searchbox/example/dist
135+
➜ dist git:(main) tree
136+
.
137+
├── assets
138+
├── index-C1z1vz3D.css
139+
├── index-D_gOo737.js
140+
└── pizza_wasm_bg-BRCuviY_.wasm
141+
├── index.html
142+
└── index.json
143+
144+
1 directory, 5 files
145+
➜ dist git:(main)
146+
```
147+
148+
Open the index.html file, and you will see the following content:
149+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-8.png)
150+
151+
In order to integrate with the searchbox, we just need to add the three highlighted sections of code into our own static website.
152+
153+
Copy the files from the assets directory to our Hugo site, in the following location:
154+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-9.png)
155+
156+
Then, modify the Hugo theme templates by adding a segment of code to the html-head.html file in the header template of all pages to load our CSS stylesheet:
157+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-10.png)
158+
159+
Next, continue modifying the Hugo theme template files by adding a segment of code to the footer template of all pages to load the JS script files:
160+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-11.png)
161+
162+
Next, insert a `docsearch` tag in the appropriate place within the page template to place the search box, as shown in the image:
163+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-12.png)
164+
165+
> the tag `docsearch` may be renamed to `searchbox` in the future
166+
167+
With that, the task is complete!
168+
169+
Open your browser to see the final result:
170+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-13.png)
171+
172+
Finally, to summarize, with the help of the 3 lines of code and copy 3 files from INFINI Pizza searchbox, you can add a lightweight offline search functionality to your static site in just 5 minutes. Give it a try!
173+
174+
175+
## References
176+
177+
* [https://pizza.rs](https://pizza.rs/)
178+
* [https://github.com/infinilabs/pizza-wasm](https://github.com/infinilabs/pizza-wasm)
179+
* [https://github.com/infinilabs/pizza-searchbox](https://github.com/infinilabs/pizza-searchbox)

0 commit comments

Comments
 (0)