From 1c38c7010d9a9604df979faa867cd8f9e7d825c9 Mon Sep 17 00:00:00 2001 From: marihachi Date: Mon, 1 Nov 2021 23:47:13 +0900 Subject: [PATCH] refactor: Introduce list of MFM Functions (#7882) * introduce list of MFM Functions * add note --- src/client/components/mfm.ts | 3 ++- src/mfm/fn-name-list.ts | 23 ++++++++++++++++++++ src/remote/activitypub/misc/get-note-html.ts | 3 ++- src/remote/activitypub/renderer/person.ts | 3 ++- src/services/note/create.ts | 7 +++--- 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/mfm/fn-name-list.ts diff --git a/src/client/components/mfm.ts b/src/client/components/mfm.ts index f3411cadc..ad6e711f6 100644 --- a/src/client/components/mfm.ts +++ b/src/client/components/mfm.ts @@ -11,6 +11,7 @@ import MkGoogle from '@client/components/google.vue'; import MkSparkle from '@client/components/sparkle.vue'; import MkA from '@client/components/global/a.vue'; import { host } from '@client/config'; +import { fnNameList } from '@/mfm/fn-name-list'; export default defineComponent({ props: { @@ -46,7 +47,7 @@ export default defineComponent({ render() { if (this.text == null || this.text == '') return; - const ast = (this.plain ? mfm.parsePlain : mfm.parse)(this.text); + const ast = (this.plain ? mfm.parsePlain : mfm.parse)(this.text, { fnNameList }); const validTime = (t: string | null | undefined) => { if (t == null) return null; diff --git a/src/mfm/fn-name-list.ts b/src/mfm/fn-name-list.ts new file mode 100644 index 000000000..1203bfffd --- /dev/null +++ b/src/mfm/fn-name-list.ts @@ -0,0 +1,23 @@ +// NOTE: client/components/autocomplete.vueにも関数のリスト(MFM_TAGS)があるので統合? + +const fnNameList = [ + 'tada', + 'jelly', + 'twitch', + 'shake', + 'spin', + 'jump', + 'bounce', + 'flip', + 'x2', + 'x3', + 'x4', + 'font', + 'blur', + 'rainbow', + 'sparkle', +]; + +export { + fnNameList +}; diff --git a/src/remote/activitypub/misc/get-note-html.ts b/src/remote/activitypub/misc/get-note-html.ts index 043335a5b..a24ec43a6 100644 --- a/src/remote/activitypub/misc/get-note-html.ts +++ b/src/remote/activitypub/misc/get-note-html.ts @@ -1,9 +1,10 @@ import * as mfm from 'mfm-js'; +import { fnNameList } from '@/mfm/fn-name-list'; import { Note } from '@/models/entities/note'; import { toHtml } from '../../../mfm/to-html'; export default function(note: Note) { - let html = note.text ? toHtml(mfm.parse(note.text), JSON.parse(note.mentionedRemoteUsers)) : null; + let html = note.text ? toHtml(mfm.parse(note.text, { fnNameList }), JSON.parse(note.mentionedRemoteUsers)) : null; if (html == null) html = '

.

'; return html; diff --git a/src/remote/activitypub/renderer/person.ts b/src/remote/activitypub/renderer/person.ts index f2a283a87..7e94abddf 100644 --- a/src/remote/activitypub/renderer/person.ts +++ b/src/remote/activitypub/renderer/person.ts @@ -11,6 +11,7 @@ import { IIdentifier } from '../models/identifier'; import renderHashtag from './hashtag'; import { DriveFiles, UserProfiles } from '@/models/index'; import { getUserKeypair } from '@/misc/keypair-store'; +import { fnNameList } from '@/mfm/fn-name-list'; export async function renderPerson(user: ILocalUser) { const id = `${config.url}/users/${user.id}`; @@ -66,7 +67,7 @@ export async function renderPerson(user: ILocalUser) { url: `${config.url}/@${user.username}`, preferredUsername: user.username, name: user.name, - summary: profile.description ? toHtml(mfm.parse(profile.description)) : null, + summary: profile.description ? toHtml(mfm.parse(profile.description, { fnNameList })) : null, icon: avatar ? renderImage(avatar) : null, image: banner ? renderImage(banner) : null, tag, diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 69d854ab1..98819c69a 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -34,6 +34,7 @@ import { deliverToRelays } from '../relay'; import { Channel } from '@/models/entities/channel'; import { normalizeForSearch } from '@/misc/normalize-for-search'; import { getAntennas } from '@/misc/antenna-cache'; +import { fnNameList } from '@/mfm/fn-name-list'; type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; @@ -182,10 +183,10 @@ export default async (user: { id: User['id']; username: User['username']; host: // Parse MFM if needed if (!tags || !emojis || !mentionedUsers) { - const tokens = data.text ? mfm.parse(data.text)! : []; - const cwTokens = data.cw ? mfm.parse(data.cw)! : []; + const tokens = data.text ? mfm.parse(data.text, { fnNameList })! : []; + const cwTokens = data.cw ? mfm.parse(data.cw, { fnNameList })! : []; const choiceTokens = data.poll && data.poll.choices - ? concat(data.poll.choices.map(choice => mfm.parse(choice)!)) + ? concat(data.poll.choices.map(choice => mfm.parse(choice, { fnNameList })!)) : []; const combinedTokens = tokens.concat(cwTokens).concat(choiceTokens);