summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2020-08-07 11:27:37 +0900
committersyuilo <syuilotan@yahoo.co.jp>2020-08-07 11:27:37 +0900
commit2d40a15d2bc7356fdd82dd980cce202d6e9acdbc (patch)
treeb52847941047ab0731b0ad8ae3e750884863cda3 /src
parentenhance(api): アクセストークンを作成する際、createdAtをlastUs... (diff)
downloadsharkey-2d40a15d2bc7356fdd82dd980cce202d6e9acdbc.tar.gz
sharkey-2d40a15d2bc7356fdd82dd980cce202d6e9acdbc.tar.bz2
sharkey-2d40a15d2bc7356fdd82dd980cce202d6e9acdbc.zip
refactor: Extract well-known services
Diffstat (limited to 'src')
-rw-r--r--src/client/components/mention.vue12
-rw-r--r--src/mfm/to-html.ts20
-rw-r--r--src/well-known-services.ts4
3 files changed, 18 insertions, 18 deletions
diff --git a/src/client/components/mention.vue b/src/client/components/mention.vue
index 0303e0e5b5..dd68aab146 100644
--- a/src/client/components/mention.vue
+++ b/src/client/components/mention.vue
@@ -18,6 +18,7 @@
import Vue from 'vue';
import { toUnicode } from 'punycode';
import { host as localHost } from '../config';
+import { wellKnownServices } from '../../well-known-services';
export default Vue.extend({
props: {
@@ -37,12 +38,11 @@ export default Vue.extend({
},
computed: {
url(): string {
- switch (this.host) {
- case 'twitter.com':
- case 'github.com':
- return `https://${this.host}/${this.username}`;
- default:
- return `/${this.canonical}`;
+ const wellKnown = wellKnownServices.find(x => x[0] === this.host);
+ if (wellKnown) {
+ return wellKnown[1](this.username);
+ } else {
+ return `/${this.canonical}`;
}
},
canonical(): string {
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;
diff --git a/src/well-known-services.ts b/src/well-known-services.ts
new file mode 100644
index 0000000000..f47c540516
--- /dev/null
+++ b/src/well-known-services.ts
@@ -0,0 +1,4 @@
+export const wellKnownServices = [
+ ['twitter.com', username => `https://twitter.com/${username}`],
+ ['github.com', username => `https://github.com/${username}`],
+] as [string, (username: string) => string][];