Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Fix room alias references in topics #4176

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/HtmlUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import ReplyThread from "./components/views/elements/ReplyThread";

import React from 'react';
import sanitizeHtml from 'sanitize-html';
import highlight from 'highlight.js';
import * as linkify from 'linkifyjs';
import linkifyMatrix from './linkify-matrix';
import _linkifyElement from 'linkifyjs/element';
Expand Down Expand Up @@ -467,11 +466,12 @@ export function bodyToHtml(content, highlights, opts={}) {
/**
* Linkifies the given string. This is a wrapper around 'linkifyjs/string'.
*
* @param {string} str
* @returns {string}
* @param {string} str string to linkify
* @param {object} [options] Options for linkifyString. Default: linkifyMatrix.options
* @returns {string} Linkified string
*/
export function linkifyString(str) {
return _linkifyString(str);
export function linkifyString(str, options = linkifyMatrix.options) {
return _linkifyString(str, options);
}

/**
Expand All @@ -489,10 +489,11 @@ export function linkifyElement(element, options = linkifyMatrix.options) {
* Linkify the given string and sanitize the HTML afterwards.
*
* @param {string} dirtyHtml The HTML string to sanitize and linkify
* @param {object} [options] Options for linkifyString. Default: linkifyMatrix.options
* @returns {string}
*/
export function linkifyAndSanitizeHtml(dirtyHtml) {
return sanitizeHtml(linkifyString(dirtyHtml), sanitizeHtmlParams);
export function linkifyAndSanitizeHtml(dirtyHtml, options = linkifyMatrix.options) {
return sanitizeHtml(linkifyString(dirtyHtml, options), sanitizeHtmlParams);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/linkify-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
*/

import {baseUrl} from "./utils/permalinks/SpecPermalinkConstructor";
import {tryTransformPermalinkToLocalHref} from "./utils/permalinks/Permalinks";
import {tryTransformEntityToPermalink, tryTransformPermalinkToLocalHref} from "./utils/permalinks/Permalinks";

function matrixLinkify(linkify) {
// Text tokens
Expand Down Expand Up @@ -221,7 +221,7 @@ matrixLinkify.options = {
case 'userid':
case 'groupid':
default: {
return tryTransformPermalinkToLocalHref(href);
return tryTransformEntityToPermalink(href);
}
}
},
Expand Down
19 changes: 19 additions & 0 deletions src/utils/permalinks/Permalinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,25 @@ export function isPermalinkHost(host: string): boolean {
return getPermalinkConstructor().isPermalinkHost(host);
}

/**
* Transforms an entity (permalink, room alias, user ID, etc) into a local URL
* if possible. If the given entity is not found to be valid enough to be converted
* then a null value will be returned.
* @param {string} entity The entity to transform.
* @returns {string|null} The transformed permalink or null if unable.
*/
export function tryTransformEntityToPermalink(entity: string): string {
if (!entity) return null;

// Check to see if it is a bare entity for starters
if (entity[0] === '#' || entity[0] === '!') return makeRoomPermalink(entity);
if (entity[0] === '@') return makeUserPermalink(entity);
if (entity[0] === '+') return makeGroupPermalink(entity);

// Then try and merge it into a permalink
return tryTransformPermalinkToLocalHref(entity);
}

/**
* 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.
Expand Down