From 2d40a15d2bc7356fdd82dd980cce202d6e9acdbc Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 7 Aug 2020 11:27:37 +0900 Subject: refactor: Extract well-known services --- src/mfm/to-html.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src/mfm') diff --git a/src/mfm/to-html.ts b/src/mfm/to-html.ts index 1292292154..9376889829 100644 --- a/src/mfm/to-html.ts +++ b/src/mfm/to-html.ts @@ -3,6 +3,7 @@ import config from '../config'; import { intersperse } from '../prelude/array'; import { MfmForest, MfmTree } from './prelude'; import { IMentionedRemoteUsers } from '../models/entities/note'; +import { wellKnownServices } from '../well-known-services'; export function toHtml(tokens: MfmForest | null, mentionedRemoteUsers: IMentionedRemoteUsers = []) { if (tokens == null) { @@ -126,18 +127,13 @@ export function toHtml(tokens: MfmForest | null, mentionedRemoteUsers: IMentione mention(token) { const a = doc.createElement('a'); const { username, host, acct } = token.node.props; - switch (host) { - case 'github.com': - a.href = `https://github.com/${username}`; - break; - case 'twitter.com': - a.href = `https://twitter.com/${username}`; - break; - default: - const remoteUserInfo = mentionedRemoteUsers.find(remoteUser => remoteUser.username === username && remoteUser.host === host); - a.href = remoteUserInfo ? (remoteUserInfo.url ? remoteUserInfo.url : remoteUserInfo.uri) : `${config.url}/${acct}`; - a.className = 'u-url mention'; - break; + const wellKnown = wellKnownServices.find(x => x[0] === host); + if (wellKnown) { + a.href = wellKnown[1](username); + } else { + const remoteUserInfo = mentionedRemoteUsers.find(remoteUser => remoteUser.username === username && remoteUser.host === host); + a.href = remoteUserInfo ? (remoteUserInfo.url ? remoteUserInfo.url : remoteUserInfo.uri) : `${config.url}/${acct}`; + a.className = 'u-url mention'; } a.textContent = acct; return a; -- cgit v1.2.3-freya