Skip to content

Commit 3ba2088

Browse files
committed
Comprehensive README update
1 parent 14b619e commit 3ba2088

File tree

1 file changed

+160
-1
lines changed

1 file changed

+160
-1
lines changed

README.md

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ https://openzim.github.io/javascript-libzim/tests/test_large_file_access/.
1717
## Nightly and Release versions
1818

1919
WASM and ASM versions are built nightly from the binaries provided (nightly) by [kiwix-build](https://github.com/kiwix/kiwix-build). The artefacts are
20-
made available at https://download.openzim.org/nightly/ (if tests pass). Artefacts for PRs and pushes are attached to the respective workflow run.
20+
made available at https://download.openzim.org/nightly/ (if tests pass). Artefacts for PRs and pushes are attached to the respective workflow run. **Please note that currently, versions built form precompiled binaries lack the snippets support, because this support relies on a patch to the source code to override exceptions-based programme flow which cannot be handled well in WASM.** Therefore, to use the full functionality, it is currently necessary to compile from source using, e.g. `docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) docker-emscripten-libzim:v3 make`.
2121

2222
Released versions are published both in [Releases](https://github.com/openzim/javascript-libzim/releases) and at https://download.openzim.org/release/javascript-libzim/.
2323

@@ -28,6 +28,165 @@ to and received from the Worker via [`window.postMessage()`](https://developer.m
2828
You can change the File Systems and other parameters in the provided [Makefile](https://github.com/openzim/javascript-libzim/blob/main/Makefile) in
2929
this Repository. This recipe needs to be run in an Emscripten-configured system or a customized Emscripten container (see below).
3030

31+
## JavaScript API Bindings
32+
33+
This section documents the JavaScript API bindings that are available after loading the compiled W/ASM module. The bindings provide access to libzim's core functionality including archive loading, content access, and search capabilities.
34+
35+
### Archive Management
36+
37+
#### `Module.loadArchive(filename: string): void`
38+
Loads a ZIM archive for subsequent operations.
39+
```javascript
40+
Module.loadArchive("path/to/archive.zim");
41+
```
42+
43+
#### `Module.getArticleCount(): number`
44+
Returns the total number of articles in the loaded archive.
45+
```javascript
46+
const count = Module.getArticleCount();
47+
```
48+
49+
### Content Access
50+
51+
#### `Module.getEntryByPath(path: string): EntryWrapper | null`
52+
Retrieves a specific entry by its path in the ZIM archive.
53+
```javascript
54+
const entry = Module.getEntryByPath("A/Wikipedia");
55+
if (entry) {
56+
console.log(entry.getTitle());
57+
}
58+
```
59+
60+
### Entry Wrapper Class
61+
62+
The `EntryWrapper` class provides access to ZIM entries (articles, redirects, etc.):
63+
64+
- `getPath(): string` - Returns the entry's path
65+
- `getTitle(): string` - Returns the entry's title
66+
- `isRedirect(): boolean` - Returns true if the entry is a redirect
67+
- `getRedirectEntry(): EntryWrapper` - Returns the target entry for redirects
68+
- `getItem(follow: boolean): ItemWrapper` - Returns the item content
69+
70+
### Item Wrapper Class
71+
72+
The `ItemWrapper` class provides access to the actual content of entries:
73+
74+
- `getData(): BlobWrapper` - Returns the content as binary data
75+
- `getMimetype(): string` - Returns the MIME type of the content
76+
77+
### Blob Wrapper Class
78+
79+
The `BlobWrapper` class handles binary content:
80+
81+
- `getContent(): Uint8Array` - Returns the content as a typed array
82+
83+
### Search Functionality
84+
85+
#### Basic Full-Text Search
86+
87+
#### `Module.search(query: string, maxResults: number): vector<EntryWrapper>`
88+
Performs basic full-text search returning entry paths.
89+
```javascript
90+
const results = Module.search("quantum physics", 20);
91+
for (let i = 0; i < results.size(); i++) {
92+
const entry = results.get(i);
93+
console.log(entry.getTitle(), entry.getPath());
94+
}
95+
```
96+
97+
**Usage Example:** See [javascript_search_usage_example.js](javascript_search_usage_example.js) for comprehensive examples.
98+
99+
#### Enhanced Search with Snippets
100+
101+
#### `Module.searchWithSnippets(query: string, maxResults: number): vector<SearchIteratorWrapper>`
102+
Performs full-text search with content snippets and metadata.
103+
```javascript
104+
const results = Module.searchWithSnippets("quantum physics", 20);
105+
for (let i = 0; i < results.size(); i++) {
106+
const result = results.get(i);
107+
console.log(result.getTitle());
108+
console.log(result.getSnippet()); // Content excerpt with highlighted terms
109+
console.log("Score:", result.getScore());
110+
}
111+
```
112+
113+
**Implementation Details:** See [SEARCH_SNIPPETS_IMPLEMENTATION.md](SEARCH_SNIPPETS_IMPLEMENTATION.md) for technical details about snippet generation.
114+
115+
#### Search Iterator Wrapper Class
116+
117+
The `SearchIteratorWrapper` class provides rich search results with content snippets:
118+
119+
- `getPath(): string` - Returns the entry's path
120+
- `getTitle(): string` - Returns the entry's title
121+
- `getSnippet(): string` - Returns content excerpt with search term highlighting
122+
- `getScore(): number` - Returns search relevance score
123+
- `getWordCount(): number` - Returns word count of the article
124+
- `getEntry(): EntryWrapper` - Returns the full entry object
125+
126+
#### Language-Aware Search
127+
128+
#### `Module.searchWithLanguage(query: string, maxResults: number, language?: string): vector<EntryWrapper>`
129+
Performs search with optional language specification.
130+
```javascript
131+
const results = Module.searchWithLanguage("bonjour", 10, "fr");
132+
```
133+
134+
### Suggestion/Autocomplete Functionality
135+
136+
#### Simple Suggestion Function
137+
138+
#### `Module.suggest(query: string, maxResults: number): vector<EntryWrapper>`
139+
Quick title-based suggestions for autocomplete functionality.
140+
```javascript
141+
const suggestions = Module.suggest("wik", 8);
142+
for (let i = 0; i < suggestions.size(); i++) {
143+
const entry = suggestions.get(i);
144+
console.log(entry.getTitle());
145+
}
146+
```
147+
148+
#### Advanced Suggestion Classes
149+
150+
#### `Module.SuggestionSearcher` Class
151+
Advanced suggestion functionality with more control:
152+
153+
```javascript
154+
const searcher = new Module.SuggestionSearcher();
155+
const search = searcher.suggest("query");
156+
const matchCount = search.getEstimatedMatches();
157+
const results = search.getResults(0, 10);
158+
```
159+
160+
**SuggestionSearcher Methods:**
161+
- `suggest(query: string): SuggestionSearchWrapper` - Creates a suggestion search
162+
163+
**SuggestionSearchWrapper Methods:**
164+
- `getEstimatedMatches(): number` - Returns estimated total matches
165+
- `getResults(start: number, count: number): vector<EntryWrapper>` - Returns paginated results
166+
167+
**Usage Example:** See [javascript_suggestions_usage_example.js](javascript_suggestions_usage_example.js) for comprehensive examples.
168+
169+
### Vector Operations
170+
171+
All search and suggestion functions return Emscripten vectors with these methods:
172+
173+
- `size(): number` - Returns the number of results
174+
- `get(index: number): T` - Returns the item at the specified index
175+
176+
### Error Handling
177+
178+
All functions include proper error handling. Failed operations typically return:
179+
- `null` for single object returns (e.g., `getEntryByPath`)
180+
- Empty vectors for collection returns (e.g., `search`, `suggest`)
181+
- Empty strings for string returns (e.g., `getSnippet`)
182+
183+
### Complete Usage Examples
184+
185+
For comprehensive usage examples and patterns:
186+
- **Search functionality:** [javascript_search_usage_example.js](javascript_search_usage_example.js)
187+
- **Suggestion functionality:** [javascript_suggestions_usage_example.js](javascript_suggestions_usage_example.js)
188+
- **Search with snippets implementation:** [SEARCH_SNIPPETS_IMPLEMENTATION.md](SEARCH_SNIPPETS_IMPLEMENTATION.md)
189+
31190
## Steps to recompile from source with Docker
32191

33192
This is the easiest (and recommended) compilation method, because all required tools are configured in the Docker image. Ensure you have docker

0 commit comments

Comments
 (0)