utils.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. /** @param {string} href */
  2. export function extractId(href) {
  3. return href.replace(/^[a-z-]+:\/+?[^/]+/, '') // Remove protocol & domain
  4. .replace(/[?&]livereload=\w+/, '') // Remove LiveReload cachebuster
  5. .replace(/^\//, '') // Remove root /
  6. .replace(/\.[a-zA-Z]+$/, '') // Remove simple extension
  7. .replace(/[^.\w-]+/g, '-') // Replace illegal characters
  8. .replace(/\./g, ':'); // Replace dots with colons(for valid id)
  9. }
  10. /**
  11. * @param {Record<string, *>} options
  12. * @param {HTMLElement | null} tag
  13. */
  14. export function addDataAttr(options, tag) {
  15. if (!tag) {return;} // in case of tag is null or undefined
  16. for (const opt in tag.dataset) {
  17. if (Object.prototype.hasOwnProperty.call(tag.dataset, opt)) {
  18. if (opt === 'env' || opt === 'dumpLineNumbers' || opt === 'rootpath' || opt === 'errorReporting') {
  19. options[opt] = tag.dataset[opt];
  20. } else {
  21. try {
  22. options[opt] = JSON.parse(tag.dataset[opt]);
  23. }
  24. catch (_) {}
  25. }
  26. }
  27. }
  28. }