docs-searchbar.js

docs-searchbar.js

Meilisearch文档搜索栏前端集成方案

docs-searchbar.js是一个基于Meilisearch的文档搜索前端解决方案。它提供了简单易用的搜索栏界面,支持自定义样式,并可与Meilisearch搜索引擎无缝集成。该项目既可通过ES模块引入,也可直接在HTML中使用,并提供了暗黑模式等丰富配置选项,能够快速为文档网站添加高效的搜索功能。

Meilisearch搜索引擎文档搜索前端SDK开源软件Github开源项目
<p align="center"> <img src="https://github.com/meilisearch/integration-guides/blob/main/assets/logos/logo.svg" alt="Meilisearch" width="200" height="200" /> </p> <h1 align="center">DEPRECATED - docs-searchbar.js</h1>

🚨 DEPRECATION WARNING 🚨

Dear Community,

We'd like to share some updates regarding the future maintenance of this repository:

Our team is small, and our availability will be reduced in the upcoming times. As such, we decided to deprecate this repository. We invite you into using Tauri's meilisearch-docsearch instead of this one.

We still accept bug fixes from the community but no more enhancements.

Seeking immediate support? Please join us on our Discord channel.


<h4 align="center"> <a href="https://github.com/meilisearch/meilisearch">Meilisearch</a> | <a href="https://www.meilisearch.com/cloud?utm_campaign=oss&utm_source=github&utm_medium=docs-searchbar.js">Meilisearch Cloud</a> | <a href="https://www.meilisearch.com/docs">Documentation</a> | <a href="https://discord.meilisearch.com">Discord</a> | <a href="https://roadmap.meilisearch.com/tabs/1-under-consideration">Roadmap</a> | <a href="https://www.meilisearch.com">Website</a> | <a href="https://www.meilisearch.com/docs/faq">FAQ</a> </h4> <p align="center"> <a href="https://www.npmjs.com/package/docs-searchbar.js"><img src="https://img.shields.io/npm/v/docs-searchbar.js.svg" alt="NPM version"></a> <a href="https://github.com/meilisearch/docs-searchbar.js/actions"><img src="https://github.com/meilisearch/docs-searchbar.js/workflows/Tests/badge.svg" alt="Test"></a> <a href="https://github.com/meilisearch/docs-searchbar.js/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-informational" alt="License"></a> <a href="https://ms-bors.herokuapp.com/repositories/11"><img src="https://bors.tech/images/badge_small.svg" alt="Bors enabled"></a> </p>

docs-searchbar.js is a front-end SDK for Meilisearch providing a search bar for your documentation.

docs-searchbar.js comes with a css template. The default styling of this library fits a documentation search bar, but you can customize it.

To make it work, You need to have your documentation's content in a Meilisearch instance. If not already the case, you can achieve this using docs-scraper.

Meilisearch is an open-source search engine. Discover what Meilisearch is!

docs-searchbar.js example

💡 If you use VuePress for your website, you should check out our VuePress plugin for Meilisearch.

Table of Contents <!-- omit in toc -->

⚡ Supercharge your Meilisearch experience

Say goodbye to server deployment and manual updates with Meilisearch Cloud. Get started with a 14-day free trial! No credit card required.

🔧 Installation

With npm:

We only guarantee that the package works with node >= 12 and node < 15.

# With NPM npm install docs-searchbar.js # With Yarn yarn add docs-searchbar.js

In your HTML:

Add the following script into your HTML file:

<script src="https://cdn.jsdelivr.net/npm/docs-searchbar.js@latest/dist/cdn/docs-searchbar.min.js"></script>

🏃‍♀️ Run Meilisearch <!-- omit in toc -->

There are many easy ways to download and run a Meilisearch instance.

For example, using the curl command in your Terminal:

# Install Meilisearch curl -L https://install.meilisearch.com | sh # Launch Meilisearch ./meilisearch --master-key=masterKey

NB: you can also download Meilisearch from Homebrew or APT or even run it using Docker.

Index your data <!-- omit in toc -->

The goal of this library is to provide a front-end search bar into your own documentation. To make that possible, you need to gather your website content in advance, and index it in a Meilisearch instance.

Luckily, we provide all the tools that you need, and can help you through the whole process, if you follow this guide 🚀

Note: If you want to try out docs-searchbar.js as a first introduction, try out our playground.

Use your own scraper <!-- omit in toc -->

We recommend using the docs-scraper tool to scrape your website, but this is not mandatory.

If you already have your own scraper but you still want to use Meilisearch and docs-searchbar.js, check out this discussion.

🎬 Getting Started

ES module <!-- omit in toc -->

Add an input tag with the attribute id="search-bar-input in one of your HTML file.

<input type="search" id="search-bar-input" />

Then, import docs-searchbar.js and run the docsSearchBar function. For more explaination of the required parameters, see next section.

import docsSearchBar from 'docs-searchbar.js' docsSearchBar({ hostUrl: 'https://mymeilisearch.com', apiKey: 'XXX', indexUid: 'docs', inputSelector: '#search-bar-input', })

HTML <!-- omit in toc -->

Add the following code to one of your HTML files.

<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docs-searchbar.js@latest/dist/cdn/docs-searchbar.min.css" /> </head> <body> <input type="search" id="search-bar-input" /> <script src="https://cdn.jsdelivr.net/npm/docs-searchbar.js@latest/dist/cdn/docs-searchbar.min.js"></script> <script> docsSearchBar({ hostUrl: 'https://mymeilisearch.com', apiKey: 'XXX', indexUid: 'docs', inputSelector: '#search-bar-input', debug: true, // Set debug to true if you want to inspect the dropdown }) </script> </body> </html>

The hostUrl and the apiKey (optional) fields are the credentials of your Meilisearch instance.<br> indexUid is the index identifier in your Meilisearch instance in which your website content is stored.<br> inputSelector is the id attribute of the HTML search input tag. As an alternative the dom element can be supplied with inputElement directly.

Your documentation content is not indexed yet? Check out this tutorial.

WARNING: We recommend providing the Meilisearch public key, which is enough to perform search requests.<br> Read more about Meilisearch authentication.

Styling

docs-searchbar.js comes with a css template. It has to be added in your project in the following way:

In an ES+ environment:

import 'docs-searchbar.js/dist/cdn/docs-searchbar.css'

In a HTML file, the link tag should be added in your header:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docs-searchbar.js@latest/dist/cdn/docs-searchbar.min.css" />

🎨 Customization

The default behavior of this library fits perfectly for a documentation search bar, but you might need some customizations.

Optional parameters <!-- omit in toc -->

When calling the docsSearchBar method, you can add optional fields:

queryHook <!-- omit in toc -->

queryHook takes a callback function as value. This function will be called on every keystroke to transform the typed keywords before querying Meilisearch. By default, it does not do anything, but it is the perfect place for you to add some preprocessing or custom functionality.

transformData <!-- omit in toc -->

transformData takes a callback function as value. This function will be called on every hit before displaying them. By default, it does not do anything, but it lets you add any post-processing around the data you received from Meilisearch.

handleSelected <!-- omit in toc -->

handleSelected takes a callback function a value. This function is called when a suggestion is selected (either from a click or a keystroke). By default, it displays anchor links to the results page. Here is an example to override this behavior:

docsSearchBar({ // ... handleSelected: function (input, event, suggestion, datasetNumber, context) { // Prevents the default behavior on click and rather opens the suggestion // in a new tab. if (context.selectionMethod === 'click') { input.setVal('') const windowReference = window.open(suggestion.url, '_blank') windowReference.focus() } }, })

Note that, by default, you can already open a new tab thanks to the CMD/CTRL + Click action.

The function is called with the following arguments:

  • input: a reference to the search input element. It comes with the .open(), .close(), .getVal() and .setVal() methods.

  • event: the actual event triggering the selection.

  • suggestion: the object representing the current selection. It contains a .url key representing the destination.

  • datasetNumber: this should always be equal to 1 as docs-searchbar.js is searching into one dataset at a time. You can ignore this attribute.

  • context: additional information about the selection. Contains a .selectionMethod key that can be either click, enterKey, tabKey or blur, depending on how the suggestion was selected.

meilisearchOptions <!-- omit in toc -->

You can forward search parameters to the Meilisearch API by using the meilisearchOptions key. Checkout out the Meilisearch documentation about search parameters.

For example, you might want to increase the number of results displayed in the dropdown:

docsSearchBar({ meilisearchOptions: { limit: 10, }, })

enableDarkMode <!-- omit in toc -->

Allows you to display the searchbar in dark mode. It is useful if your website has dark mode support and you also want the searchbar to appear in a dark version. You can always edit the style of the searchbar to match the style of your website. When the option enableDarkMode is set to auto, the searchbar automatically sets the mode to the system mode.

enableDarkMode has three possible states:

  • false: enforce light mode.
  • true: enforce dark mode.
  • auto: system mode (light or dark).

Example:

docsSearchBar({ ... enableDarkMode: 'auto' })

Dark mode with enableDarkMode set to auto and system mode set to dark:

docs-searchbar with dark mode

enhancedSearchInput <!-- omit in toc -->

When set to true, a theme is applied to the search box to improve its appearance. It adds the .searchbox class which can be used to further customise the search box.

Example:

docsSearchBar({ ... enhancedSearchInput: true })
More Examples <!-- omit in toc -->

Here is a basic HTML file used in the playground of this repository.

As a more concrete example, you can check out the configuration applied in the Meilisearch plugin for VuePress.

Styling <!-- omit in toc -->

/* Main dropdown wrapper */ .meilisearch-autocomplete .dsb-dropdown-menu { width: 500px; } /* Main category */ .meilisearch-autocomplete .docs-searchbar-suggestion--category-header { color: darkgray; border: 1px solid gray; } /* Category */ .meilisearch-autocomplete .docs-searchbar-suggestion--subcategory-column { color: gray; } /* Title */ .meilisearch-autocomplete .docs-searchbar-suggestion--title { font-weight: bold; color: black; } /* Description */ .meilisearch-autocomplete .docs-searchbar-suggestion--text { font-size: 0.8rem; color: gray; } /* Highlighted text */ .meilisearch-autocomplete .docs-searchbar-suggestion--highlight { color: blue; }

TIPS: When inspecting the dropdown markup with your browser tools, you should add debug: true to your docsSearchBar call to prevent it from closing on inspection.

More Examples <!-- omit in toc -->

Here is the CSS customization applied in the Meilisearch plugin for VuePress.

🤖 Compatibility with Meilisearch

This package guarantees compatibility with version v1.x of Meilisearch, but some features may not be present. Please check the issues for more info.

⚙️ Development Workflow and Contributing

Any new contribution is more than welcome in this project!

If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!

🥇 Credits

Based on Algolia DocSearch repository from this commit.<br> Due to a lot of future changes in this repository compared to the original one, we don't maintain it as an official fork.

<hr>

Meilisearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

openai-agents-python

openai-agents-python

OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。

openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。

下拉加载更多