https://github.com/rodneylab/searchlite
Rust WASM tool to manipulate HTML searching for an input search term
https://github.com/rodneylab/searchlite
aho-corasick html5ever rust search wasm
Last synced: 17 days ago
JSON representation
Rust WASM tool to manipulate HTML searching for an input search term
- Host: GitHub
- URL: https://github.com/rodneylab/searchlite
- Owner: rodneylab
- License: bsd-3-clause
- Created: 2023-03-16T12:57:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-29T03:49:07.000Z (about 3 years ago)
- Last Synced: 2025-02-09T03:06:49.489Z (over 1 year ago)
- Topics: aho-corasick, html5ever, rust, search, wasm
- Language: Rust
- Homepage:
- Size: 87.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

searchlite
Rust WASM tool to manipulate HTML searching for an input search term. Searchlite will wrap matching text elements in a pair of HTML `` tags. Browsers will highlight these matches by default.
Module can be used within a web app or in serverless middleware to highlight search terms in an HTML response.
Uses `html5ever` and `aho-corasick` Rust crates under the hood.
## Compile WASM
1. Clone the project and change into the project directory. Then run these
commands:
```shell
cargo install wasm-pack # skip if you already have it installed
wasm-pack build --target web
```
2. Copy the generated `pkg` folder into your JavaScript or TypeScript project.
3. Import and use the code in one of your project source files (expected output
is as shown in previous section):
- Generate highlighted HTML
`highlight_search_terms` takes two arguments: the input HTML and the search term. Separate multiple search terms with a space (e.g. `"apple pear"`).
```typescript
import init, { highlight_search_terms as highlight } from "pkg/searchlite.js";
await init();
// alternative if top level await is not available
(async () => {
await init();
})();
const highlightedHtml = highlight(
"
Heading
Nobody likes maple in their apple flavoured Snapple. APPLE
",
"apple",
);
```
Output (`highlightedHTML`):
```html
Heading
Nobody likes maple in their apple flavoured
Snapple. APPLE
```
Note the `id` added to the first search match. You can use this to scroll the first match into view.

## 🗺️ Roadmap
No firm course laid in.
- Possibly add stemmer, though this might be better handled externally. A stemmer will match on related words; film and filming for an input of films as an example. The [Porter Algorithm](https://tartarus.org/martin/PorterStemmer/def.txt) is often used for this.
- Possibly add a utility function to generate a match snippet for use in result pages that show matches across various documents.
## ☎️ Reach Out
Feel free to jump into the
[Rodney Lab matrix chat room](https://matrix.to/#/%23rodney:matrix.org).