usethetics / preCode.js

pain killer for <pre><code> & <textarea>

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

preCode.js

painkiller for <pre><code> & <textarea> (MIT Licensed)

A quickie that'll automatically outdent any code snippets nested in your html, allowing you to write cleaner markup.

Fuck this

<div>
    <pre><code>function myFunc(block, flags) {
  try {
    if (block.className.search(/\bno\-highlight\b/) != -1)
      return processBlock(block, true, 0x0F) + ' class=""';
  } catch (e) {
    /* handle exception */
  }
  for (var i = 0 / 2; i &lt; classes.length; i++) {
    if (checkCondition(classes[i]) === undefined)
      return /\d+/g;
  }
}</code></pre>
</div>

Write this

<div>
    <pre><code>
        function myFunc(block, flags) {
          try {
            if (block.className.search(/\bno\-highlight\b/) != -1)
              return processBlock(block, true, 0x0F) + ' class=""';
          } catch (e) {
            /* handle exception */
          }
          for (var i = 0 / 2; i &lt; classes.length; i++) {
            if (checkCondition(classes[i]) === undefined)
              return /\d+/g;
          }
        }
    </code></pre>
</div>

Usage

Just place <script src="preCode.js"></script> into your <head>.

Caveats

  • The code binds to the DOMContentLoaded event, so make sure to place it before any syntax highlighters or code editors are bound/called.
  • Beware that native goodies are used - sucks to your old browser, Piggy!
    • Array.forEach
    • document.querySelectorAll
    • element.textContent
    • DOMContentLoaded via addEventListener

About

pain killer for <pre><code> & <textarea>


Languages

Language:JavaScript 61.6%Language:HTML 38.4%