* Don't require auth for .ping * Finally introducing loggers * Remove file list caching from disk_space_primitives I don't think this is required anymore with the sync model * Simplification of space primitives and more tests * More reliable online status * Add .zed settings * Remove selfUpdate argument from `writeFile` no longer used - Add `sync` config schema and document sync options - Move sync config handling to client boot logic - Move BootConfig type to ui_types.ts - Remove sync env vars from server - Update documentation for sync and architecture * Improve sync logic and proxy routing for full sync state * Add BoxProxy for late-bound client and improve Lua boot - Add isMobile syscall for editor * Add concurrency to sync process and log indexing duration * Better service worker lifecycle management
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import type {
|
|
IndexTreeEvent,
|
|
PageCreatingContent,
|
|
PageCreatingEvent,
|
|
} from "@silverbulletmd/silverbullet/type/event";
|
|
import { renderToText } from "@silverbulletmd/silverbullet/lib/tree";
|
|
import { editor, system } from "@silverbulletmd/silverbullet/syscalls";
|
|
import { ftsIndexPage, ftsSearch } from "./engine.ts";
|
|
import { PromiseQueue } from "../../lib/async.ts";
|
|
|
|
const searchPrefix = "search:";
|
|
|
|
// Search indexing is prone to concurrency issues, so we queue all write operations
|
|
const promiseQueue = new PromiseQueue();
|
|
|
|
export async function indexPage({ name, tree }: IndexTreeEvent) {
|
|
if (!await system.getConfig("index.search.enable", true)) {
|
|
return;
|
|
}
|
|
const text = renderToText(tree);
|
|
|
|
return promiseQueue.runInQueue(async () => {
|
|
// console.log("Now FTS indexing", name);
|
|
// await engine.deleteDocument(name);
|
|
await ftsIndexPage(name, text);
|
|
});
|
|
}
|
|
|
|
export async function searchCommand() {
|
|
const phrase = await editor.prompt("Search for: ");
|
|
if (phrase) {
|
|
await editor.navigate(`${searchPrefix}${phrase}`);
|
|
}
|
|
}
|
|
|
|
export async function readFileSearch(
|
|
{ name }: PageCreatingEvent,
|
|
): Promise<PageCreatingContent | undefined> {
|
|
if (!name.startsWith(searchPrefix)) {
|
|
return;
|
|
}
|
|
const phrase = name.substring(searchPrefix.length);
|
|
const results = await ftsSearch(phrase);
|
|
const text = `# Search results for "${phrase}"\n${
|
|
results
|
|
.map((r) => `* [[${r.id}]] (score ${r.score})`)
|
|
.join("\n")
|
|
}
|
|
`;
|
|
|
|
return {
|
|
text,
|
|
perm: "ro",
|
|
};
|
|
}
|