refactor: Extract well-known services
This commit is contained in:
parent
2bdcd22ad4
commit
2d40a15d2b
@ -18,6 +18,7 @@
|
|||||||
import Vue from 'vue';
|
import Vue from 'vue';
|
||||||
import { toUnicode } from 'punycode';
|
import { toUnicode } from 'punycode';
|
||||||
import { host as localHost } from '../config';
|
import { host as localHost } from '../config';
|
||||||
|
import { wellKnownServices } from '../../well-known-services';
|
||||||
|
|
||||||
export default Vue.extend({
|
export default Vue.extend({
|
||||||
props: {
|
props: {
|
||||||
@ -37,12 +38,11 @@ export default Vue.extend({
|
|||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
url(): string {
|
url(): string {
|
||||||
switch (this.host) {
|
const wellKnown = wellKnownServices.find(x => x[0] === this.host);
|
||||||
case 'twitter.com':
|
if (wellKnown) {
|
||||||
case 'github.com':
|
return wellKnown[1](this.username);
|
||||||
return `https://${this.host}/${this.username}`;
|
} else {
|
||||||
default:
|
return `/${this.canonical}`;
|
||||||
return `/${this.canonical}`;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
canonical(): string {
|
canonical(): string {
|
||||||
|
@ -3,6 +3,7 @@ import config from '../config';
|
|||||||
import { intersperse } from '../prelude/array';
|
import { intersperse } from '../prelude/array';
|
||||||
import { MfmForest, MfmTree } from './prelude';
|
import { MfmForest, MfmTree } from './prelude';
|
||||||
import { IMentionedRemoteUsers } from '../models/entities/note';
|
import { IMentionedRemoteUsers } from '../models/entities/note';
|
||||||
|
import { wellKnownServices } from '../well-known-services';
|
||||||
|
|
||||||
export function toHtml(tokens: MfmForest | null, mentionedRemoteUsers: IMentionedRemoteUsers = []) {
|
export function toHtml(tokens: MfmForest | null, mentionedRemoteUsers: IMentionedRemoteUsers = []) {
|
||||||
if (tokens == null) {
|
if (tokens == null) {
|
||||||
@ -126,18 +127,13 @@ export function toHtml(tokens: MfmForest | null, mentionedRemoteUsers: IMentione
|
|||||||
mention(token) {
|
mention(token) {
|
||||||
const a = doc.createElement('a');
|
const a = doc.createElement('a');
|
||||||
const { username, host, acct } = token.node.props;
|
const { username, host, acct } = token.node.props;
|
||||||
switch (host) {
|
const wellKnown = wellKnownServices.find(x => x[0] === host);
|
||||||
case 'github.com':
|
if (wellKnown) {
|
||||||
a.href = `https://github.com/${username}`;
|
a.href = wellKnown[1](username);
|
||||||
break;
|
} else {
|
||||||
case 'twitter.com':
|
const remoteUserInfo = mentionedRemoteUsers.find(remoteUser => remoteUser.username === username && remoteUser.host === host);
|
||||||
a.href = `https://twitter.com/${username}`;
|
a.href = remoteUserInfo ? (remoteUserInfo.url ? remoteUserInfo.url : remoteUserInfo.uri) : `${config.url}/${acct}`;
|
||||||
break;
|
a.className = 'u-url mention';
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
a.textContent = acct;
|
a.textContent = acct;
|
||||||
return a;
|
return a;
|
||||||
|
4
src/well-known-services.ts
Normal file
4
src/well-known-services.ts
Normal file
@ -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][];
|
Loading…
Reference in New Issue
Block a user