Converge on permalink processing for HtmlUtils and linkify-matrix
This commit is contained in:
parent
ff4eee5239
commit
2cb0b4903a
3 changed files with 45 additions and 38 deletions
|
@ -2,6 +2,7 @@
|
||||||
Copyright 2015, 2016 OpenMarket Ltd
|
Copyright 2015, 2016 OpenMarket Ltd
|
||||||
Copyright 2017, 2018 New Vector Ltd
|
Copyright 2017, 2018 New Vector Ltd
|
||||||
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
||||||
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -33,6 +34,7 @@ import url from 'url';
|
||||||
|
|
||||||
import EMOJIBASE from 'emojibase-data/en/compact.json';
|
import EMOJIBASE from 'emojibase-data/en/compact.json';
|
||||||
import EMOJIBASE_REGEX from 'emojibase-regex';
|
import EMOJIBASE_REGEX from 'emojibase-regex';
|
||||||
|
import {tryTransformPermalinkToLocalHref} from "./utils/permalinks/RoomPermalinkCreator";
|
||||||
|
|
||||||
linkifyMatrix(linkify);
|
linkifyMatrix(linkify);
|
||||||
|
|
||||||
|
@ -158,30 +160,10 @@ const transformTags = { // custom to matrix
|
||||||
if (attribs.href) {
|
if (attribs.href) {
|
||||||
attribs.target = '_blank'; // by default
|
attribs.target = '_blank'; // by default
|
||||||
|
|
||||||
let m;
|
const transformed = tryTransformPermalinkToLocalHref(attribs.href);
|
||||||
// FIXME: horrible duplication with linkify-matrix
|
if (transformed !== attribs.href || attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN)) {
|
||||||
m = attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN);
|
attribs.href = transformed;
|
||||||
if (m) {
|
|
||||||
attribs.href = m[1];
|
|
||||||
delete attribs.target;
|
delete attribs.target;
|
||||||
} else {
|
|
||||||
m = attribs.href.match(linkifyMatrix.MATRIXTO_URL_PATTERN);
|
|
||||||
if (m) {
|
|
||||||
const entity = m[1];
|
|
||||||
switch (entity[0]) {
|
|
||||||
case '@':
|
|
||||||
attribs.href = '#/user/' + entity;
|
|
||||||
break;
|
|
||||||
case '+':
|
|
||||||
attribs.href = '#/group/' + entity;
|
|
||||||
break;
|
|
||||||
case '#':
|
|
||||||
case '!':
|
|
||||||
attribs.href = '#/room/' + entity;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
delete attribs.target;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
attribs.rel = 'noopener'; // https://mathiasbynens.github.io/rel-noopener/
|
attribs.rel = 'noopener'; // https://mathiasbynens.github.io/rel-noopener/
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2015, 2016 OpenMarket Ltd
|
Copyright 2015, 2016 OpenMarket Ltd
|
||||||
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -15,6 +16,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {baseUrl} from "./utils/permalinks/SpecPermalinkConstructor";
|
import {baseUrl} from "./utils/permalinks/SpecPermalinkConstructor";
|
||||||
|
import {tryTransformPermalinkToLocalHref} from "./utils/permalinks/RoomPermalinkCreator";
|
||||||
|
|
||||||
function matrixLinkify(linkify) {
|
function matrixLinkify(linkify) {
|
||||||
// Text tokens
|
// Text tokens
|
||||||
|
@ -225,20 +227,8 @@ matrixLinkify.options = {
|
||||||
case 'roomalias':
|
case 'roomalias':
|
||||||
case 'userid':
|
case 'userid':
|
||||||
case 'groupid':
|
case 'groupid':
|
||||||
return matrixLinkify.MATRIXTO_BASE_URL + '/#/' + href;
|
|
||||||
default: {
|
default: {
|
||||||
// FIXME: horrible duplication with HtmlUtils' transform tags
|
return tryTransformPermalinkToLocalHref(href);
|
||||||
let m = href.match(matrixLinkify.VECTOR_URL_PATTERN);
|
|
||||||
if (m) {
|
|
||||||
return m[1];
|
|
||||||
}
|
|
||||||
m = href.match(matrixLinkify.MATRIXTO_URL_PATTERN);
|
|
||||||
if (m) {
|
|
||||||
const entity = m[1];
|
|
||||||
if (matrixToEntityMap[entity[0]]) return matrixToEntityMap[entity[0]] + entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return href;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -249,8 +239,8 @@ matrixLinkify.options = {
|
||||||
|
|
||||||
target: function(href, type) {
|
target: function(href, type) {
|
||||||
if (type === 'url') {
|
if (type === 'url') {
|
||||||
if (href.match(matrixLinkify.VECTOR_URL_PATTERN) ||
|
const transformed = tryTransformPermalinkToLocalHref(href);
|
||||||
href.match(matrixLinkify.MATRIXTO_URL_PATTERN)) {
|
if (transformed !== href || href.match(matrixLinkify.VECTOR_URL_PATTERN)) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return '_blank';
|
return '_blank';
|
||||||
|
|
|
@ -20,6 +20,7 @@ import utils from 'matrix-js-sdk/lib/utils';
|
||||||
import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor";
|
import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor";
|
||||||
import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
|
import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
|
||||||
import RiotPermalinkConstructor from "./RiotPermalinkConstructor";
|
import RiotPermalinkConstructor from "./RiotPermalinkConstructor";
|
||||||
|
import * as matrixLinkify from "../../linkify-matrix";
|
||||||
|
|
||||||
const SdkConfig = require("../../SdkConfig");
|
const SdkConfig = require("../../SdkConfig");
|
||||||
|
|
||||||
|
@ -286,6 +287,40 @@ export function isPermalinkHost(host: string): boolean {
|
||||||
return getPermalinkConstructor().isPermalinkHost(host);
|
return getPermalinkConstructor().isPermalinkHost(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms a permalink (or possible permalink) into a local URL if possible. If
|
||||||
|
* the given permalink is found to not be a permalink, it'll be returned unaltered.
|
||||||
|
*/
|
||||||
|
export function tryTransformPermalinkToLocalHref(permalink: string): string {
|
||||||
|
if (!permalink.startsWith("http:") && !permalink.startsWith("https:")) {
|
||||||
|
return permalink;
|
||||||
|
}
|
||||||
|
|
||||||
|
let m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN);
|
||||||
|
if (m) {
|
||||||
|
return m[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
// A bit of a hack to convert permalinks of unknown origin to Riot links
|
||||||
|
try {
|
||||||
|
const permalinkParts = parsePermalink(permalink);
|
||||||
|
if (permalinkParts) {
|
||||||
|
if (permalinkParts.roomIdOrAlias) {
|
||||||
|
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : '';
|
||||||
|
permalink = `#/room/${permalinkParts.roomIdOrAlias}${eventIdPart}`;
|
||||||
|
} else if (permalinkParts.groupId) {
|
||||||
|
permalink = `#/group/${permalinkParts.groupId}`;
|
||||||
|
} else if (permalinkParts.userId) {
|
||||||
|
permalink = `#/user/${permalinkParts.userId}`;
|
||||||
|
} // else not a valid permalink for our purposes - do not handle
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Not an href we need to care about
|
||||||
|
}
|
||||||
|
|
||||||
|
return permalink;
|
||||||
|
}
|
||||||
|
|
||||||
function getPermalinkConstructor(): PermalinkConstructor {
|
function getPermalinkConstructor(): PermalinkConstructor {
|
||||||
const riotPrefix = SdkConfig.get()['permalinkPrefix'];
|
const riotPrefix = SdkConfig.get()['permalinkPrefix'];
|
||||||
if (riotPrefix && riotPrefix !== matrixtoBaseUrl) {
|
if (riotPrefix && riotPrefix !== matrixtoBaseUrl) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue