import { useMemo } from "../preact/hooks.ts"; import { toTitleLc } from "../scrapbox-userscript-std/dom.ts"; import type { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts"; declare const scrapbox: Scrapbox; export const useSearch = (text: string, maxCandidates = 10): string[] => useMemo(() => { const textLc = toTitleLc(text); return scrapbox.Project.pages .flatMap((page) => { const index = page.titleLc.indexOf(textLc); if (index < 0) return []; return [{ index, ...page }]; }) .sort((a,b) => { // 1. 一致した箇所が早い順 const diff = a.index - b.index; if (diff !== 0) return diff; // 2. 文字列が短い順 const ldiff = a.title.length - b.title.length if (ldiff !== 0 ) return ldiff; // 3. つながっているリンク順 if (a.exists !== b.exists) a.exists ? -1 : 1; // 4. 更新日時が新しい順 return b.updated - a.updated; }) .slice(0, maxCandidates) .map((page) => page.title) }, [text, maxCandidates, scrapbox.Project.pages]);