Skip to content

Instantly share code, notes, and snippets.

@josephdburdick
Forked from hkfoster/smarten.js
Created July 7, 2016 20:32
Show Gist options
  • Select an option

  • Save josephdburdick/cc573e33f9b6d073c70e325102bd1d3e to your computer and use it in GitHub Desktop.

Select an option

Save josephdburdick/cc573e33f9b6d073c70e325102bd1d3e to your computer and use it in GitHub Desktop.

Revisions

  1. @hkfoster hkfoster created this gist Jan 11, 2014.
    29 changes: 29 additions & 0 deletions smarten.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // Make punctuation smarter
    jQuery.fn.smarten = (function() {

    function smartenNode(node) {
    if (node.nodeType === 3) {
    node.data = node.data
    .replace(/(^|[-\u2014/(\[{"\s])'/g, "$1\u2018") // Opening singles
    .replace(/'/g, "\u2019") // Closing singles & apostrophes
    .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, "$1\u201c") // Opening doubles
    .replace(/"/g, "\u201d") // Closing doubles
    .replace(/--/g, "\u2014") // Em dashes
    .replace(/\.{3}/g, "\u2026"); // Ellipsis
    } else if (node.nodeType === 1) {
    if (node = node.firstChild) do {
    smartenNode(node);
    } while (node = node.nextSibling);
    }
    }

    return function() {
    return this.each(function(){
    smartenNode(this);
    });
    };

    }());

    // Instantiation
    $('body').smarten();