linkifyjs icon indicating copy to clipboard operation
linkifyjs copied to clipboard

UNC path plugin

Open ekalchev opened this issue 6 years ago • 0 comments

I am trying to create plugin for creating links for UNC file path "\\server\path\directory". I need to know if I am at the right path.

I made the following modification to linkify.js to get this working

I defined a new token like this, because backslash was missing

var BACKSLASH = inheritsToken('\\');

Added a new symbol to state on S_START

S_START.on('\\', makeState(BACKSLASH));

and this is the plugin code

'use strict';

; (function (linkify) {
  var plugin = function () {
    'use strict';

    function hashtag(linkify) {
        var TT = linkify.scanner.TOKENS; // Text tokens
        var MultiToken = linkify.parser.TOKENS.Base; // Base Multi token class
        var S_START = linkify.parser.start;
        var State = linkify.parser.State;

        function BACKSLASH_BACKSLASH(value) {
            this.v = value;
        }

        linkify.inherits(MultiToken, BACKSLASH_BACKSLASH, {
            type: 'uncpath',
            isLink: true,
            toHref: function toHref() {
                return 'file://' + this.toString().substr(1);
            }
        });

        var S_BACKSLASH = S_START.jump(TT.BACKSLASH);
        var S_BACKSLASH_BACKSLASH = new State(BACKSLASH_BACKSLASH);
        S_BACKSLASH.on(TT.BACKSLASH, S_BACKSLASH_BACKSLASH);


        S_BACKSLASH_BACKSLASH.on(TT.DOMAIN, S_BACKSLASH_BACKSLASH);
        S_BACKSLASH_BACKSLASH.on(TT.TLD, S_BACKSLASH_BACKSLASH);
        S_BACKSLASH_BACKSLASH.on(TT.LOCALHOST, S_BACKSLASH_BACKSLASH);
        S_BACKSLASH_BACKSLASH.on(TT.BACKSLASH, S_BACKSLASH_BACKSLASH);
    }

    return hashtag;
}();

plugin(linkify);
})(linkify);

Can you tell me if I am missing something it appears to be working but may be I am not seeing something?

also I can see "asdasdasdasd\\asdasdasd\asdasdas" is also linkified. How can tell the parser to linkify the unc path only if \\ is prefixed with space, newline, tab and etc?

ekalchev avatar Apr 21 '18 20:04 ekalchev