Too big of a change for now, I think we should do this when/if we move to GH package registry (we would have to I think).
54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
/**
|
|
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
|
|
* @license MIT
|
|
*/
|
|
|
|
import { Terminal, ILinkMatcherOptions, ITerminalAddon } from 'xterm';
|
|
|
|
const protocolClause = '(https?:\\/\\/)';
|
|
const domainCharacterSet = '[\\da-z\\.-]+';
|
|
const negatedDomainCharacterSet = '[^\\da-z\\.-]+';
|
|
const domainBodyClause = '(' + domainCharacterSet + ')';
|
|
const tldClause = '([a-z\\.]{2,6})';
|
|
const ipClause = '((\\d{1,3}\\.){3}\\d{1,3})';
|
|
const localHostClause = '(localhost)';
|
|
const portClause = '(:\\d{1,5})';
|
|
const hostClause = '((' + domainBodyClause + '\\.' + tldClause + ')|' + ipClause + '|' + localHostClause + ')' + portClause + '?';
|
|
const pathCharacterSet = '(\\/[\\/\\w\\.\\-%~:+]*)*([^:"\'\\s])';
|
|
const pathClause = '(' + pathCharacterSet + ')?';
|
|
const queryStringHashFragmentCharacterSet = '[0-9\\w\\[\\]\\(\\)\\/\\?\\!#@$%&\'*+,:;~\\=\\.\\-]*';
|
|
const queryStringClause = '(\\?' + queryStringHashFragmentCharacterSet + ')?';
|
|
const hashFragmentClause = '(#' + queryStringHashFragmentCharacterSet + ')?';
|
|
const negatedPathCharacterSet = '[^\\/\\w\\.\\-%]+';
|
|
const bodyClause = hostClause + pathClause + queryStringClause + hashFragmentClause;
|
|
const start = '(?:^|' + negatedDomainCharacterSet + ')(';
|
|
const end = ')($|' + negatedPathCharacterSet + ')';
|
|
const strictUrlRegex = new RegExp(start + protocolClause + bodyClause + end);
|
|
|
|
function handleLink(event: MouseEvent, uri: string): void {
|
|
window.open(uri, '_blank');
|
|
}
|
|
|
|
export class WebLinksAddon implements ITerminalAddon {
|
|
private _linkMatcherId: number | undefined;
|
|
private _terminal: Terminal | undefined;
|
|
|
|
constructor(
|
|
private _handler: (event: MouseEvent, uri: string) => void = handleLink,
|
|
private _options: ILinkMatcherOptions = {}
|
|
) {
|
|
this._options.matchIndex = 1;
|
|
}
|
|
|
|
public activate(terminal: Terminal): void {
|
|
this._terminal = terminal;
|
|
this._linkMatcherId = this._terminal.registerLinkMatcher(strictUrlRegex, this._handler, this._options);
|
|
}
|
|
|
|
public dispose(): void {
|
|
if (this._linkMatcherId !== undefined && this._terminal !== undefined) {
|
|
this._terminal.deregisterLinkMatcher(this._linkMatcherId);
|
|
}
|
|
}
|
|
}
|