MJPinfield
MJPinfield

Reputation: 796

Showdown markdown not replacing string

I am currently trying to add my own extension to showdown using the ghost blogging platform. I am trying to make it so someone can type map and then a uk postcode and have it rendered to a map, like so [map bh278bf]. I have made sure the maps.js extension has been added and works as I have tested it. However my Regex knowledge is practically non-existent. I have got the RegEx to work here in Regexr.com, but when I run it nothing happens, I have used the same codepen and it also doesn't work and I have no idea what to do. I need some assistance in identifying the string!

The Expression:

/(\[map )([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)(\])/igm

The extension (maps.js)

(function(){
    var maps = function(converter) {
        return [
            { 
                type: 'output', 
                filter: function(source) {
                    return source.replace(/(\[map )([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)(\])$/gim, function(match) {
                        return "<span>Map will go here</span>";
                    });
                }
            }
        ];
    };
    // Client-side export
    if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.prettify = maps; }
    // Server-side export
    if (typeof module !== 'undefined') module.exports = maps;
}());

Upvotes: 0

Views: 588

Answers (1)

pdeschen
pdeschen

Reputation: 1379

You were close but not there yet.

  1. Your regex is not valid and should read:

    /[map (GIR 0AA|A-PR-UWYZ ?[0-9][ABD-HJLNP-UW-Z]{2})]/ig

With i for case insensitiveness. See answer https://stackoverflow.com/a/29302162/475884

  1. The actual export is invalid where instead window.Showdown.extensions.prettify = maps you should have window.Showdown.extensions.maps = maps

Where you get

// https://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive

(function(){
    var maps = function(converter) {
        return [ { 
            type: 'lang', 
            filter: function(text) {
                return text.replace(/\[map (GIR 0AA|[A-PR-UWYZ]([A-HK-Y]([0-9][A-Z]?|[1-9][0-9])|[1-9]([0-9]|[A-HJKPSTUW])?) ?[0-9][ABD-HJLNP-UW-Z]{2})\]/ig, 'map');
            }
        }];
    };
    // Client-side export
    if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.maps = maps; }
    // Server-side export
    if (typeof module !== 'undefined') module.exports = maps;
}());

You may have a look at the following fsfiddle for details.

Upvotes: 1

Related Questions