rust-search-extension/README.md

140 lines
4.7 KiB
Markdown
Raw Normal View History

2018-11-27 10:55:52 +00:00
![](assets/rustacean.gif)
2018-10-31 11:40:46 +00:00
2018-11-27 03:34:35 +00:00
# Rust Search Extension
2018-11-29 15:56:40 +00:00
![Chrome Web Store](https://img.shields.io/chrome-web-store/v/ennpfpdlaclocpomkiablnmbppdnlhoh.svg)
![Mozilla Add-on](https://img.shields.io/amo/v/rust-search-extension?color=%2320123A)
2019-12-23 06:59:14 +00:00
[![rust-doc](https://img.shields.io/badge/stable-1.40.0-yellow.svg)](https://doc.rust-lang.org/1.40.0/std/)
2018-11-27 03:34:35 +00:00
[![license-mit](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Folyd/rust-search-extension/blob/master/LICENSE-MIT)
[![license-apache](https://img.shields.io/badge/license-Apache-yellow.svg)](https://github.com/Folyd/rust-search-extension/blob/master/LICENSE-APACHE)
2019-11-01 13:49:21 +00:00
![Build Status](https://github.com/folyd/rust-search-extension/workflows/build/badge.svg)
2018-11-27 03:34:35 +00:00
2019-11-08 03:06:49 +00:00
🦀 A handy browser extension to search crates and docs in the address bar (omnibox).
2018-11-27 03:34:35 +00:00
2020-01-03 12:30:59 +00:00
[https://rust-search-extension.now.sh/](https://rust-search-extension.now.sh/)
2019-12-03 10:00:53 +00:00
2018-11-29 15:56:40 +00:00
### Installation
- [Chrome Web Store](https://chrome.google.com/webstore/detail/rust-search-extension/ennpfpdlaclocpomkiablnmbppdnlhoh)
2018-11-29 15:56:40 +00:00
- [Firefox](https://addons.mozilla.org/en-US/firefox/addon/rust-search-extension/)
2018-11-29 15:56:40 +00:00
2018-11-27 03:34:35 +00:00
### Features
- Search Primitive Types and Modules
- Search Structs, Traits and Enums
- Search Functions, Methods and Macros
- Search crates on https://crates.io
2019-10-29 08:39:09 +00:00
- Search [Compiler Error Index](https://doc.rust-lang.org/error-index.html) with error code
2019-10-31 14:27:22 +00:00
- Offline mode, search local Rust docs (`rustup docs --std`) (Some limitation on Firefox, see [Caveats](#caveats))
- Both Chrome and Firefox are supported
2018-11-27 03:34:35 +00:00
### Usages
2018-11-27 10:55:52 +00:00
Input keyword **rs** in the address bar, press `Tab` or `Space` to activate the search bar. Then enter any word
you want to search, the extension will response the related search results instantly.
2018-11-27 03:34:35 +00:00
![demonstration.gif](assets/demonstration.gif)
2020-01-03 12:30:59 +00:00
### [How the extension works](https://rust-search-extension.now.sh/how-it-works/)
2018-11-27 03:34:35 +00:00
2018-11-27 10:55:52 +00:00
#### 1. Build *search index*
2018-11-27 03:34:35 +00:00
The raw *search index* was generated by **rustc-doc**. The most important function is `build_index()` which
2019-10-29 08:39:09 +00:00
located at [librustdoc/html/render/cache.rs](https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/cache.rs).
2018-11-27 03:34:35 +00:00
2018-11-27 10:55:52 +00:00
The final *search index* is an object array like this:
2018-11-27 03:34:35 +00:00
```js
var searchIndex = [
{
crate: "std",
2018-11-27 10:55:52 +00:00
desc: "A hash map implemented with linear probing and Robin Hood bucket stealing.",
name: "HashMap",
2018-11-27 03:34:35 +00:00
parent: undefined,
2018-11-27 10:55:52 +00:00
path: "std::collections",
ty: 3,
2018-11-27 03:34:35 +00:00
type: null
},
{
crate: "std",
desc: "Applies function to the elements of iterator and returns the first non-none result.",
name: "find_map",
parent: {ty: 8, name: "Iterator"},
path: "std::iter",
ty: 11,
type: [["self"],["f"]]
},
2018-11-27 03:34:35 +00:00
...
];
2018-11-27 03:34:35 +00:00
```
2018-11-27 10:55:52 +00:00
#### 2. Build *search words array* based on *search index*
2018-11-27 03:34:35 +00:00
The *search words array* is the list of search words to query against which build from the raw *search index*.
It's just a plain name array of every Rust structs, traits, enums, functions, methods, macros, etc.
```js
var searchWords = [
"result",
"option",
"hashmap",
"hashset",
"clone",
"copy",
2018-11-27 10:55:52 +00:00
"display",
2019-10-29 08:39:09 +00:00
"debug",
2018-11-27 03:34:35 +00:00
...
];
2018-11-27 03:34:35 +00:00
```
2018-11-27 12:00:01 +00:00
#### 3. Search keyword in *search words array*
2018-11-27 03:34:35 +00:00
2018-11-27 12:00:01 +00:00
Using *Levenshtein distance* algorithm to search matched words, the max Levenshtein distance is 2.
```js
// Max levenshtein distance.
var MAX_LEV_DISTANCE = 2;
```
#### 4. Sort *search result*
Sort search result according to *Levenshtein distance*, lexicographical and others metrics.
2018-11-27 03:34:35 +00:00
2018-11-27 10:55:52 +00:00
#### 5. Transform *search result* to *suggestion result* to show in the address bar
2018-11-27 12:00:01 +00:00
Mapping the *search word* and *search index* to generate the *search result*, build Rust doc link for each result item.
```js
var suggestResults = [
{
content: "https://doc.rust-lang.org/std/ops/trait.Deref.html",
description: "std::ops::<match>Deref</match> - <dim>Used for immutable dereferencing operations, like `*v`.</dim>"
2018-11-27 12:00:01 +00:00
},
{
content: "https://doc.rust-lang.org/std/ops/trait.Deref.html#tymethod.deref",
description: "std::ops::Deref::<match>deref</match> - <dim>Dereferences the value.</dim>"
2019-10-29 08:39:09 +00:00
},
2018-11-27 12:00:01 +00:00
...
];
```
2019-10-31 14:27:22 +00:00
### Caveats
#### 1. Why local `file:` rust doc not work properly on Firefox?
For security reasons, in Firefox, `file:` URLs is an unprivileged URL, accessing to those unprivileged URLs are prohibited.
See the [MDN documentation](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/create) for more detail.
#### 2. Any workaround to support offline mode on Firefox?
2019-12-03 10:00:53 +00:00
Sure. A good choice is use http server! For example using python **http.server** module:
2019-10-31 14:27:22 +00:00
```sh
$ cd your-rust-doc-directory
2019-11-01 13:35:43 +00:00
$ python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
2019-10-31 14:27:22 +00:00
```
2019-11-01 13:35:43 +00:00
Then set `http://0.0.0.0:8000` as your local doc path.