Welcome to mirror list, hosted at ThFree Co, Russian Federation.

search_helpers.js « search « frontend « content - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09d5afbf88a2fd4df4dd1e5f7e8806e3b4e52f53 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
 * Shared functions for Lunr and Google search.
 */

/**
 * Check URL parameters for search parameters.
 *
 * We support "q" for the query string as it's a Google standard,
 * and also "query" as it has been long-documented in the
 * GitLab handbook as a Docs search parameter.
 *
 * See https://about.gitlab.com/handbook/tools-and-tips/searching/
 *
 * @returns
 *  An object containing query parameters.
 */
export const getSearchParamsFromURL = () => {
  const searchParams = new URLSearchParams(window.location.search);
  return {
    qParam: searchParams.get('q') || searchParams.get('query') || '',
    pageParam: searchParams.get('page') || '',
    filterParam: searchParams.get('filters') || '',
  };
};

/**
 * Update URL parameters.
 *
 * This allows users to retrace their steps after a search.
 *
 * @param params Object
 *   Key/value pairs with the param name and value.
 *   Values can be strings or arrays.
 */
export const updateURLParams = (params) => {
  const queryString = Object.entries(params)
    .filter(([, value]) => value !== '' && !(Array.isArray(value) && value.length === 0))
    .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
    .join('&');
  window.history.pushState(null, '', `${window.location.pathname}?${queryString}`);
};

/**
 * Search filters.
 *
 * Option properties:
 *   - text: Used for checkbox labels
 *   - value: References values in the "docs-site-section" metatag, which is included each search result.
 *   - id: Machine-friendly version of the text, used for analytics and URL params.
 */
export const SEARCH_FILTERS = [
  {
    title: 'Filter by',
    options: [
      {
        text: 'Tutorials',
        value: 'Tutorials',
        id: 'tutorials',
      },
      {
        text: 'Installation docs',
        value: 'Install,Subscribe',
        id: 'install',
      },
      {
        text: 'Administration docs',
        value: 'Administer,Subscribe',
        id: 'administer',
      },
      {
        text: 'User docs',
        value: 'Subscribe,Use GitLab',
        id: 'user',
      },
      {
        text: 'Extension and API docs',
        value: 'Extend',
        id: 'extend',
      },
      {
        text: 'Contributor docs',
        value: 'Contribute',
        id: 'contribute',
      },
      {
        text: 'Solution docs',
        value: 'Solutions',
        id: 'solutions',
      },
    ],
  },
];

/**
 * Convert between filter values and filter IDs.
 *
 * @param Array arr
 *   Selected filters to convert.
 * @param Boolean isToID
 *   true to convert to IDs, false to convert to values
 *
 * @returns Array
 */
export const convertFilterValues = (arr, isToID) => {
  const convertedArr = arr.map((item) => {
    for (const filter of SEARCH_FILTERS) {
      for (const option of filter.options) {
        if ((isToID && option.value === item) || (!isToID && option.id === item)) {
          return isToID ? option.id : option.value;
        }
      }
    }
    return null;
  });
  return convertedArr.filter((item) => item !== null);
};

/**
 * Keyboard shortcuts.
 */
export const activateKeyboardShortcut = () => {
  document.addEventListener('keydown', (e) => {
    // Focus on the search form with the forward slash and S keys.
    const shortcutKeys = ['/', 's'];
    if (!shortcutKeys.includes(e.key) || e.ctrlKey || e.metaKey) return;
    if (/^(?:input|textarea|select|button)$/i.test(e.target.tagName)) return;
    e.preventDefault();
    document.querySelector('input[type="search"]').focus();
  });
};

/**
 * Find the highest-level scrollable header that contains a given string.
 *
 * We need a regex here to match only if there are word boundaries or slashes.
 * For example:
 *  - "to" should match in "login to gitlab" but not "repository"
 *  - "AI" should match in "AI/ML" but not "FAIL"
 */
export const findHighestHeader = (query) => {
  const headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6');

  const regex = new RegExp(`(?<=^|\\s|\\/)${query}s?(?=$|\\s|\\/)`, 'gi');
  const matches = Array.from(headings).filter((heading) => heading.textContent.match(regex));

  if (matches.length) {
    return matches.sort((a, b) => a.tagName.localeCompare(b.tagName))[0];
  }

  return null;
};

/**
 * If a search query is in the URL parameters and a heading, scroll to it.
 */
export const scrollToQuery = () => {
  const { qParam } = getSearchParamsFromURL();
  if (!qParam) return;

  const header = findHighestHeader(qParam);
  if (header && header.tagName !== 'H1') {
    header.scrollIntoView({ behavior: 'smooth' });
  }
};

/**
 * Generate a query string to be appended to search result links.
 *
 * This is used to limit which pages we run scrollToQuery() on.
 */
export const searchResultQueryParam = (query, link) => {
  const pages = new Set(['/ee/ci/yaml/']);

  // Check if the search result is included in the pages set.
  let linkPath = '';
  try {
    const url = new URL(link); // Google results are full URLs
    linkPath = url.pathname;
  } catch {
    linkPath = `/${link.replace('/index.html', '/')}`; // Lunr results are just paths
  }

  if (pages.has(linkPath)) return `?query=${query}`;
  return '';
};