fogbugz-markdown

Automatically reinterpret comments as markdown.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name           fogbugz-markdown
// @version        1.4
// @namespace      ohnopub.net
// @description    Automatically reinterpret comments as markdown.
// @include        http://*.fogbugz.com/*
// @include        https://*.fogbugz.com/*
// @require https://code.jquery.com/jquery-1.10.2.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.1/marked.min.js
// ==/UserScript==

this.jQuery = jQuery.noConflict(true);
(function (jQuery) {
    /* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions */
    /* Also http://imgur.com/MzceH02 */
    var stringEscapeForRegExp = function (string){
        return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    }

    jQuery(document).ready(function () {
        var markedOptions = {
            renderer: new marked.Renderer()
        };

        var markedRendererLink = markedOptions.renderer.link;
        var linkBugRegExp = new RegExp('^([a-z0-9]{1,12}://' + stringEscapeForRegExp(window.location.hostname) + '/)?/?(default\\.asp)?\\?([0-9]+)$');
        markedOptions.renderer.link = function (href, title, text) {
            /* Add back the onmouseover="b1(ix, this);" to each bug link */
            var matches = linkBugRegExp.exec(href);
            if (matches)
                return jQuery('<span/>').append(jQuery('<a/>', {'class': 'markdown-bugref', href: matches[0], onmouseover: 'b1(' + JSON.stringify(matches[3]|0) + ', this);', text: text, title: title})).html();
            return markedRendererLink.apply(this, arguments);
        };

        jQuery('.bugevent.detailed .body, .pseudobugevent.detailed .body').each(function (i, elem) {
            var obj = jQuery(elem);
            /*
             * Detect preformatted (rich text) by text nodes with only
             * whitespace followed by an element containing text nodes
             * (empty or not, e.g. <p></p>).
             */
            var c = obj.contents();
            while (c.length && c[0].data && /^\s*$/.test(c[0].data))
                c.pop();
            /*
             * We have skipped the whitespace text nodes, now to see
             * if the first element is empty or not. Rich text is
             * mostly in <p/> elements and would have <span/>s
             * probably for formatting. Plain text only has <br/> if
             * anything. In plain text, we might thus encounter an
             * empty element *or* a non-empty text node but in rich
             * text that can never happen. Thus, if it’s not a text
             * node and not an empty element, assume it’s preformatted
             * rich text and abort.
             */
            if (c.length && !c[0].data && !jQuery(c[0]).is(':empty'))
                return;

            /*
             * Make buglinks into something that will survive the
             * markdown.
             */
            obj.find('a').each(function (i, elem) {
                var obj = jQuery(elem);
                if (linkBugRegExp.exec(obj.attr('href')))
                    obj.text('[' + obj.text() + '](' + obj.attr('href') + ')');
            });
            obj.html(marked(obj.text(), markedOptions));
        });

        /* style blockquote */
        jQuery('head').append(
            jQuery('<style/>', {
                text: 'blockquote {margin-left: 0; border-left: 0.25em solid #aaaaaa; border-left-color: rgba(127, 127, 127, 0.5); padding-left: 1.75em;}',
                type: 'text/css'
            }));
    });
})(jQuery);