Merge pull request #5514 from matrix-org/jryans/element-url-patterns

Recognise `*.element.io` links as Element permalinks
pull/21833/head
J. Ryan Stinnett 2020-12-21 14:49:58 +00:00 committed by GitHub
commit 62d1345790
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 10 deletions

View File

@ -163,7 +163,7 @@ const transformTags: IExtendedSanitizeOptions["transformTags"] = { // custom to
attribs.target = '_blank'; // by default attribs.target = '_blank'; // by default
const transformed = tryTransformPermalinkToLocalHref(attribs.href); const transformed = tryTransformPermalinkToLocalHref(attribs.href);
if (transformed !== attribs.href || attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN)) { if (transformed !== attribs.href || attribs.href.match(linkifyMatrix.ELEMENT_URL_PATTERN)) {
attribs.href = transformed; attribs.href = transformed;
delete attribs.target; delete attribs.target;
} }

View File

@ -183,12 +183,14 @@ const escapeRegExp = function(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}; };
// Recognise URLs from both our local vector and official vector as vector. // Recognise URLs from both our local and official Element deployments.
// anyone else really should be using matrix.to. // Anyone else really should be using matrix.to.
matrixLinkify.VECTOR_URL_PATTERN = "^(?:https?://)?(?:" matrixLinkify.ELEMENT_URL_PATTERN =
+ escapeRegExp(window.location.host + window.location.pathname) + "|" "^(?:https?://)?(?:" +
+ "(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/" escapeRegExp(window.location.host + window.location.pathname) + "|" +
+ ")(#.*)"; "(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/|" +
"(?:app|beta|staging|develop)\\.element\\.io/" +
")(#.*)";
matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?://)?(?:www\\.)?matrix\\.to/#/(([#@!+]).*)"; matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?://)?(?:www\\.)?matrix\\.to/#/(([#@!+]).*)";
matrixLinkify.MATRIXTO_MD_LINK_PATTERN = matrixLinkify.MATRIXTO_MD_LINK_PATTERN =
@ -253,7 +255,7 @@ matrixLinkify.options = {
target: function(href, type) { target: function(href, type) {
if (type === 'url') { if (type === 'url') {
const transformed = tryTransformPermalinkToLocalHref(href); const transformed = tryTransformPermalinkToLocalHref(href);
if (transformed !== href || href.match(matrixLinkify.VECTOR_URL_PATTERN)) { if (transformed !== href || href.match(matrixLinkify.ELEMENT_URL_PATTERN)) {
return null; return null;
} else { } else {
return '_blank'; return '_blank';

View File

@ -331,7 +331,7 @@ export function tryTransformPermalinkToLocalHref(permalink: string): string {
return permalink; return permalink;
} }
const m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN); const m = permalink.match(matrixLinkify.ELEMENT_URL_PATTERN);
if (m) { if (m) {
return m[1]; return m[1];
} }
@ -365,7 +365,7 @@ export function getPrimaryPermalinkEntity(permalink: string): string {
// If not a permalink, try the vector patterns. // If not a permalink, try the vector patterns.
if (!permalinkParts) { if (!permalinkParts) {
const m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN); const m = permalink.match(matrixLinkify.ELEMENT_URL_PATTERN);
if (m) { if (m) {
// A bit of a hack, but it gets the job done // A bit of a hack, but it gets the job done
const handler = new ElementPermalinkConstructor("http://localhost"); const handler = new ElementPermalinkConstructor("http://localhost");