Converge on permalink processing for HtmlUtils and linkify-matrix

This commit is contained in:
Travis Ralston 2019-09-30 20:17:54 -06:00
parent ff4eee5239
commit 2cb0b4903a
3 changed files with 45 additions and 38 deletions

View file

@ -20,6 +20,7 @@ import utils from 'matrix-js-sdk/lib/utils';
import SpecPermalinkConstructor, {baseUrl as matrixtoBaseUrl} from "./SpecPermalinkConstructor";
import PermalinkConstructor, {PermalinkParts} from "./PermalinkConstructor";
import RiotPermalinkConstructor from "./RiotPermalinkConstructor";
import * as matrixLinkify from "../../linkify-matrix";
const SdkConfig = require("../../SdkConfig");
@ -286,6 +287,40 @@ export function isPermalinkHost(host: string): boolean {
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 {
const riotPrefix = SdkConfig.get()['permalinkPrefix'];
if (riotPrefix && riotPrefix !== matrixtoBaseUrl) {