CloudCannon / pagefind

Static low-bandwidth search at scale
https://pagefind.app
MIT License
3.22k stars 97 forks source link

Error in documentation for the bundle path option #631

Open hankertrix opened 3 weeks ago

hankertrix commented 3 weeks ago

The documentation for the Pagefind options object on https://pagefind.app is wrong. Specifically, the bundlePath option doesn’t exist. Here’s the link to the page:

https://pagefind.app/docs/search-config/#bundle-path

The WASM generated JavaScript code in pagefind.js shows that the bundlePath option doesn’t exist, and a basePath option exists instead. Here is the relevant code from pagefind.js:


var PagefindInstance = class {
        constructor(opts = {}) {
            this.version = pagefind_version;
            this.backend = wasm_bindgen;
            this.decoder = new TextDecoder("utf-8");
            this.wasm = null;
            this.basePath = opts.basePath || "/pagefind/";
            this.primary = opts.primary || false;
            if (this.primary && !opts.basePath) {
                this.initPrimary()
            }
            if (/[^\/]$/.test(this.basePath)) {
                this.basePath = `${this.basePath}/`
            }
            if (window?.location?.origin && this.basePath.startsWith(window.location.origin)) {
                this.basePath = this.basePath.replace(window.location.origin, "")
            }
            this.baseUrl = opts.baseUrl || this.defaultBaseUrl();
            if (!/^(\/|https?:\/\/)/.test(this.baseUrl)) {
                this.baseUrl = `/${this.baseUrl}`
            }
            this.indexWeight = opts.indexWeight ?? 1;
            this.excerptLength = opts.excerptLength ?? 30;
            this.mergeFilter = opts.mergeFilter ?? {};
            this.ranking = opts.ranking;
            this.highlightParam = opts.highlightParam ?? null;
            this.loaded_chunks = {};
            this.loaded_filters = {};
            this.loaded_fragments = {};
            this.raw_ptr = null;
            this.searchMeta = null;
            this.languages = null
        }
        initPrimary() {
            let derivedBasePath = import.meta.url.match(/^(.*\/)pagefind.js.*$/)?.[1];
            if (derivedBasePath) {
                this.basePath = derivedBasePath
            } else {
                console.warn(["Pagefind couldn't determine the base of the bundle from the import path. Falling back to the default.", "Set a basePath option when initialising Pagefind to ignore this message."].join("\n"))
            }
        }
        defaultBaseUrl() {
            let default_base = this.basePath.match(/^(.*\/)_?pagefind/)?.[1];
            return default_base || "/"
        }
        async options(options2) {
            const opts = ["basePath", "baseUrl", "indexWeight", "excerptLength", "mergeFilter", "highlightParam", "ranking"];
            for (const [k, v] of Object.entries(options2)) {
                if (k === "mergeFilter") {
                    let filters2 = this.stringifyFilters(v);
                    let ptr = await this.getPtr();
                    this.raw_ptr = this.backend.add_synthetic_filter(ptr, filters2)
                } else if (k === "ranking") {
                    await this.set_ranking(options2.ranking)
                } else if (opts.includes(k)) {
                    if (k === "basePath" && typeof v === "string") this.basePath = v;
                    if (k === "baseUrl" && typeof v === "string") this.baseUrl = v;
                    if (k === "indexWeight" && typeof v === "number") this.indexWeight = v;
                    if (k === "excerptLength" && typeof v === "number") this.excerptLength = v;
                    if (k === "mergeFilter" && typeof v === "object") this.mergeFilter = v;
                    if (k === "highlightParam" && typeof v === "string") this.highlightParam = v
                } else {
                    console.warn(`Unknown Pagefind option ${k}. Allowed options: [${opts.join(", ")}]`)
                }
            }
        }
        decompress(data, file = "unknown file") {
                if (this.decoder.decode(data.slice(0, 12)) === "pagefind_dcd") {
                    return data.slice(12)
                }