Making WordPress.org

Changeset 8054


Ignore:
Timestamp:
01/09/2019 02:26:21 AM (6 years ago)
Author:
dd32
Message:

Trac: Autolink Gutenberg Github issues using the form GB-1234 or GB1234.

Fixes #3961.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/style/trac/wp-trac.js

    r8052 r8054  
    7878            wpTrac.autocomplete.init();
    7979            wpTrac.linkMentions();
     80            wpTrac.linkGutenbergIssues();
    8081
    8182            if ( ! $body.hasClass( 'plugins' ) ) {
     
    105106        },
    106107
    107         linkMentions: function() {
     108        linkMentions: function( selector ) {
    108109            // See https://github.com/regexps/mentions-regex/blob/master/index.js#L21
    109110            var mentionsRegEx = /(^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@)(?!\/))([a-zA-Z0-9/_\-.]{1,20})(?:\b(?!@|@)|$)/g,
    110111                mentionsInAttrRegEx = new RegExp( '="[^"]*?' + mentionsRegEx.source + '[\\s\\S]*?"' );
    111112
    112             $( 'div.change .comment, #ticket .description' ).each( function() {
     113            $( selector || 'div.change .comment, #ticket .description' ).each( function() {
    113114                var $comment = $( this ).html();
    114115
     
    134135
    135136                    // Restore mentions in HTML attributes.
     137                    if ( placeholders.length ) {
     138                        $comment = $comment.replace( '__PLACEHOLDER__', function() {
     139                            return placeholders.shift();
     140                        } );
     141                    }
     142
     143                    $( this ).html( $comment );
     144                }
     145            });
     146        },
     147
     148        linkGutenbergIssues: function( selector ) {
     149            var gutenRegEx = /\bGB[-]?(\d+)(<\/a>)?\b/gi,
     150                gutenInAttrRegEx = new RegExp( '="[^"]*?' + gutenRegEx.source + '[\\s\\S]*?"' );
     151
     152            $( selector || 'div.change .comment, #ticket .description' ).each( function() {
     153                var $comment = $( this ).html();
     154
     155                if ( gutenRegEx.test( $comment ) ) {
     156                    var placeholders = [];
     157
     158                    if ( gutenInAttrRegEx.test( $comment ) ) {
     159                        // Preserve matches in HTML attributes.
     160                        $comment = $comment.replace( gutenInAttrRegEx, function( match ) {
     161                            placeholders.push( match );
     162                            return '__PLACEHOLDER__';
     163                        } );
     164                    }
     165
     166                    $comment = $comment.replace( gutenRegEx, function( match, issueNumber, closing_a_present ) {
     167                        if ( closing_a_present ) {
     168                            // Already linked
     169                            return match;
     170                        }
     171
     172                        return '<a class="gutenberg-issue github ext-link" href="https://github.com/WordPress/Gutenberg/issues/' + issueNumber + '"><span class="icon">&#8203;</span>' + match + '</a>';
     173                    } );
     174
     175                    // Restore matches in HTML attributes.
    136176                    if ( placeholders.length ) {
    137177                        $comment = $comment.replace( '__PLACEHOLDER__', function() {
     
    174214                };
    175215            });
     216
     217            wpTrac.linkGutenbergIssues( '.ticketdraft .comment' );
     218            wpTrac.linkMentions( '.ticketdraft .comment' );
    176219        },
    177220
Note: See TracChangeset for help on using the changeset viewer.