Advertisement
fagci

Epsilon notes markdown extra attributes fixed

Oct 14th, 2018
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const regExtra = /<(\w+)\s*(.*)>([^{>]*)\{([^}]+)\}(.*)/g,
  2.     regAttr = /([^= ]+)=['"]?([^"']+)['"]?/g;
  3. let body = document.body,
  4.     html = body.innerHTML;
  5. body.innerHTML = html.replace(regExtra, (s, tag, eAttr, bCont, attr, aCont) => {
  6.   if(['script','style'].indexOf(tag) !== -1) return s;
  7.   let attrMap = {}, newAttr;
  8.   eAttr.replace(regAttr, (a, aKey, aValue) => {
  9.     attrMap[aKey] = aValue;
  10.   });
  11.   normalizeAttributes = (a, aKey, aValue) => {
  12.     const existingValue = attrMap[aKey];
  13.     if(existingValue !== undefined && aKey === 'class') {
  14.       aValue = `${existingValue} ${aValue}`;
  15.     }
  16.     return `${aKey}="${aValue}"`;
  17.   };
  18.   newAttr = attr.split(" ").map(part => {
  19.     return part
  20.       .replace(/^\./, "class=")
  21.       .replace(/^#/, "id=")
  22.       .replace(regAttr, normalizeAttributes);
  23.   });
  24.   return `<${tag} ${newAttr.join(' ')}>${bCont} ${aCont}`;
  25. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement