Wednesday, December 7, 2011

Markdown textarea


// ==UserScript==
// @name            Markdown textarea
// @namespace       http://userscripts.org/scripts/show/91369
// @description     Add Markdown convert button to textarea
// @version         0.9
// @author          mozillazg
// @updateURL       https://userscripts.org/scripts/source/91369.meta.js
// @include         *
// ==/UserScript==

(function () {
    var button, div;
    var textareas = document.getElementsByTagName('textarea');
    if (textareas && textareas.length) {
        for (var i = 0; i < textareas.length; i++) {
            var textarea = textareas[i];
            var textarea_style = getComputedStyle(textarea, "" );
            if (textarea_style.display != "none" 
                 && textarea_style.visibility != "hidden" 
                 && textarea.getAttribute("readonly") == null 
                 && textarea.getAttribute("disabled") == null) {
                div = document.createElement('div');
                button = document.createElement('button');
                button.setAttribute('type', 'button')
                button.innerHTML = 'Markdown';
                button.addEventListener('click', function(e){
                    var converter = new Showdown.converter();
                    e.target.parentNode.firstChild.value = converter.makeHtml(e.target.parentNode.firstChild.value);
                }, false);
                textareas[i].parentNode.insertBefore(div, textareas[i]);
                div.appendChild(textareas[i]);
                div.appendChild(document.createElement('br'));
                div.appendChild(button);
            }
        }
    }


    //
    // showdown.js -- A javascript port of Markdown.
    //
    // Copyright (c) 2007 John Fraser.
    //
    // Original Markdown Copyright (c) 2004-2005 John Gruber
    //   <http://daringfireball.net/projects/markdown/>
    //
    // Redistributable under a BSD-style open source license.
    // See license.txt for more information.
    //
    // The full source distribution is at:
    //
    //    A A L
    //    T C A
    //    T K B
    //
    //   <http://www.attacklab.net/>
    //
    
    //
    // Wherever possible, Showdown is a straight, line-by-line port
    // of the Perl version of Markdown.
    //
    // This is not a normal parser design; it's basically just a
    // series of string substitutions.  It's hard to read and
    // maintain this way,  but keeping Showdown close to the original
    // design makes it easier to port new features.
    //
    // More importantly, Showdown behaves like markdown.pl in most
    // edge cases.  So web applications can do client-side preview
    // in Javascript, and then build identical HTML on the server.
    //
    // This port needs the new RegExp functionality of ECMA 262,
    // 3rd Edition (i.e. Javascript 1.5).  Most modern web browsers
    // should do fine.  Even with the new regular expression features,
    // We do a lot of work to emulate Perl's regex functionality

0 comments:

Post a Comment