From 4f693a1ff5f51e876ed94d717d4903e89f782ccd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 16 Jun 2018 08:27:17 +0100 Subject: [PATCH 1/2] implement group links in matrixLinkify:MATRIXTO. Simplify if/else w/ map Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/linkify-matrix.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index 6bbea77733..328cb98888 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -169,11 +169,18 @@ matrixLinkify.VECTOR_URL_PATTERN = "^(?:https?:\/\/)?(?:" + "(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/" + ")(#.*)"; -matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?:\/\/)?(?:www\\.)?matrix\\.to/#/((#|@|!).*)"; +matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?:\/\/)?(?:www\\.)?matrix\\.to/#/([#@!+].*)"; matrixLinkify.MATRIXTO_MD_LINK_PATTERN = - '\\[([^\\]]*)\\]\\((?:https?:\/\/)?(?:www\\.)?matrix\\.to/#/((#|@|!)[^\\)]*)\\)'; + '\\[([^\\]]*)\\]\\((?:https?:\/\/)?(?:www\\.)?matrix\\.to/#/([#@!+][^\\)]*)\\)'; matrixLinkify.MATRIXTO_BASE_URL= baseUrl; +const matrixToEntityMap = { + '@': '#/user/', + '#': '#/room/', + '!': '#/room/', + '+': '#/group/', +}; + matrixLinkify.options = { events: function(href, type) { switch (type) { @@ -204,24 +211,20 @@ matrixLinkify.options = { case 'userid': case 'groupid': return matrixLinkify.MATRIXTO_BASE_URL + '/#/' + href; - default: - var m; + default: { // FIXME: horrible duplication with HtmlUtils' transform tags - m = href.match(matrixLinkify.VECTOR_URL_PATTERN); + 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 (entity[0] === '@') { - return '#/user/' + entity; - } else if (entity[0] === '#' || entity[0] === '!') { - return '#/room/' + entity; - } + if (matrixToEntityMap[entity[0]]) return matrixToEntityMap[entity[0]] + entity; } return href; + } } }, From 3ebec92ac592e0c10f37f1f47fa09989536319d8 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 16 Jun 2018 08:27:47 +0100 Subject: [PATCH 2/2] replace hardcoded `matrix.to` with reference to const in matrix-to for easier changing Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/messages/TextualBody.js | 3 ++- src/matrix-to.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/views/messages/TextualBody.js b/src/components/views/messages/TextualBody.js index 018754411c..60377a47d7 100644 --- a/src/components/views/messages/TextualBody.js +++ b/src/components/views/messages/TextualBody.js @@ -36,6 +36,7 @@ import * as ContextualMenu from '../../structures/ContextualMenu'; import SettingsStore from "../../../settings/SettingsStore"; import PushProcessor from 'matrix-js-sdk/lib/pushprocessor'; import ReplyThread from "../elements/ReplyThread"; +import {host as matrixtoHost} from '../../../matrix-to'; linkifyMatrix(linkify); @@ -304,7 +305,7 @@ module.exports = React.createClass({ // never preview matrix.to links (if anything we should give a smart // preview of the room/user they point to: nobody needs to be reminded // what the matrix.to site looks like). - if (host == 'matrix.to') return false; + if (host === matrixtoHost) return false; if (node.textContent.toLowerCase().trim().startsWith(host.toLowerCase())) { // it's a "foo.pl" style link diff --git a/src/matrix-to.js b/src/matrix-to.js index 72fb3c38fc..90b0a66090 100644 --- a/src/matrix-to.js +++ b/src/matrix-to.js @@ -14,7 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -export const baseUrl = "https://matrix.to"; +export const host = "matrix.to"; +export const baseUrl = `https://${host}`; export function makeEventPermalink(roomId, eventId) { return `${baseUrl}/#/${roomId}/${eventId}`;