metadata.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  2. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  4. import { userAgents } from '@algolia/autocomplete-shared';
  5. export function getMetadata(_ref) {
  6. var _, _options$__autocomple, _options$__autocomple2, _options$__autocomple3;
  7. var plugins = _ref.plugins,
  8. options = _ref.options;
  9. var optionsKey = (_ = (((_options$__autocomple = options.__autocomplete_metadata) === null || _options$__autocomple === void 0 ? void 0 : _options$__autocomple.userAgents) || [])[0]) === null || _ === void 0 ? void 0 : _.segment;
  10. var extraOptions = optionsKey ? _defineProperty({}, optionsKey, Object.keys(((_options$__autocomple2 = options.__autocomplete_metadata) === null || _options$__autocomple2 === void 0 ? void 0 : _options$__autocomple2.options) || {})) : {};
  11. return {
  12. plugins: plugins.map(function (plugin) {
  13. return {
  14. name: plugin.name,
  15. options: Object.keys(plugin.__autocomplete_pluginOptions || [])
  16. };
  17. }),
  18. options: _objectSpread({
  19. 'autocomplete-core': Object.keys(options)
  20. }, extraOptions),
  21. ua: userAgents.concat(((_options$__autocomple3 = options.__autocomplete_metadata) === null || _options$__autocomple3 === void 0 ? void 0 : _options$__autocomple3.userAgents) || [])
  22. };
  23. }
  24. export function injectMetadata(_ref3) {
  25. var _environment$navigato;
  26. var metadata = _ref3.metadata,
  27. environment = _ref3.environment;
  28. var isMetadataEnabled = (_environment$navigato = environment.navigator) === null || _environment$navigato === void 0 ? void 0 : _environment$navigato.userAgent.includes('Algolia Crawler');
  29. if (isMetadataEnabled) {
  30. var metadataContainer = environment.document.createElement('meta');
  31. var headRef = environment.document.querySelector('head');
  32. metadataContainer.name = 'algolia:metadata';
  33. setTimeout(function () {
  34. metadataContainer.content = JSON.stringify(metadata);
  35. headRef.appendChild(metadataContainer);
  36. }, 0);
  37. }
  38. }