refactor: Introduce list of MFM Functions (#7882)

* introduce list of MFM Functions

* add note
This commit is contained in:
marihachi 2021-11-01 23:47:13 +09:00 committed by GitHub
parent 4d85407ea4
commit 1c91945bf5
5 changed files with 33 additions and 6 deletions

View file

@ -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;

23
src/mfm/fn-name-list.ts Normal file
View file

@ -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
};

View file

@ -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 = '<p>.</p>';
return html;

View file

@ -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,

View file

@ -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);