summaryrefslogtreecommitdiff
path: root/docs/_static/js/pagefind-wrapper.js
blob: a4acadf8684802086a51fae010679027825a106c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Version-scoped Pagefind loader. Derives its base from the current URL so the
 * same file works on production, canary, and /pr-<n>/ previews (§9). */
(function (window) {
  'use strict';

  function basePathFor(pathname) {
    var m = pathname.match(/^((\/pr-\d+)?\/[a-z]{2}(?:_[A-Z]{2})?\/[^/]+\/)/);
    if (!m) return null;
    return { base: m[1], prefix: m[2] || '' };
  }

  function prefixResultUrl(url, prefix) {
    return prefix ? prefix + url : url;
  }

  function assetUrlsFor(base) {
    return {
      css: base + 'pagefind/pagefind-ui.css',
      js: base + 'pagefind/pagefind-ui.js',
    };
  }

  function init() {
    var mount = document.getElementById('vyos-search');
    if (!mount) return;
    var ctx = basePathFor(window.location.pathname);
    if (!ctx) return;
    var assets = assetUrlsFor(ctx.base);
    /* Stylesheet first so the UI never renders unstyled. CSS load failure is
     * cosmetic only — no error handler on the link. */
    var l = document.createElement('link');
    l.rel = 'stylesheet';
    l.href = assets.css;
    document.head.appendChild(l);
    var s = document.createElement('script');
    s.src = assets.js;
    s.onerror = function () {
      /* Per-version Pagefind assets missing (index not built yet, preview
       * namespace, transient failure) — show a visible notice instead of
       * leaving #vyos-search a silent empty div. */
      var p = document.createElement('p');
      p.className = 'vyos-search-unavailable';
      p.textContent = 'Search is temporarily unavailable for this version.';
      mount.appendChild(p);
    };
    s.onload = function () {
      /* global PagefindUI */
      new window.PagefindUI({
        element: '#vyos-search',
        baseUrl: ctx.base,
        bundlePath: ctx.base + 'pagefind/',
        processResult: function (result) {
          result.url = prefixResultUrl(result.url, ctx.prefix);
          return result;
        },
      });
    };
    document.head.appendChild(s);
  }

  window.VyOSSearch = {
    basePathFor: basePathFor,
    prefixResultUrl: prefixResultUrl,
    assetUrlsFor: assetUrlsFor,
    init: init,
  };
  if (typeof document !== 'undefined' && document.addEventListener)
    document.addEventListener('DOMContentLoaded', init);
})(window);