utils.js 1.1 KB

123456789101112131415161718192021222324
  1. module.exports = {
  2. extractId: function(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. addDataAttr: function(options, tag) {
  11. for (var opt in tag.dataset) {
  12. if (tag.dataset.hasOwnProperty(opt)) {
  13. if (opt === "env" || opt === "dumpLineNumbers" || opt === "rootpath" || opt === "errorReporting") {
  14. options[opt] = tag.dataset[opt];
  15. } else {
  16. try {
  17. options[opt] = JSON.parse(tag.dataset[opt]);
  18. }
  19. catch(_) {}
  20. }
  21. }
  22. }
  23. }
  24. };