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 34ea3cf226
commit 1c38c7010d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 MkSparkle from '@client/components/sparkle.vue';
import MkA from '@client/components/global/a.vue'; import MkA from '@client/components/global/a.vue';
import { host } from '@client/config'; import { host } from '@client/config';
import { fnNameList } from '@/mfm/fn-name-list';
export default defineComponent({ export default defineComponent({
props: { props: {
@ -46,7 +47,7 @@ export default defineComponent({
render() { render() {
if (this.text == null || this.text == '') return; 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) => { const validTime = (t: string | null | undefined) => {
if (t == null) return null; 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 * as mfm from 'mfm-js';
import { fnNameList } from '@/mfm/fn-name-list';
import { Note } from '@/models/entities/note'; import { Note } from '@/models/entities/note';
import { toHtml } from '../../../mfm/to-html'; import { toHtml } from '../../../mfm/to-html';
export default function(note: Note) { 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>'; if (html == null) html = '<p>.</p>';
return html; return html;

View file

@ -11,6 +11,7 @@ import { IIdentifier } from '../models/identifier';
import renderHashtag from './hashtag'; import renderHashtag from './hashtag';
import { DriveFiles, UserProfiles } from '@/models/index'; import { DriveFiles, UserProfiles } from '@/models/index';
import { getUserKeypair } from '@/misc/keypair-store'; import { getUserKeypair } from '@/misc/keypair-store';
import { fnNameList } from '@/mfm/fn-name-list';
export async function renderPerson(user: ILocalUser) { export async function renderPerson(user: ILocalUser) {
const id = `${config.url}/users/${user.id}`; const id = `${config.url}/users/${user.id}`;
@ -66,7 +67,7 @@ export async function renderPerson(user: ILocalUser) {
url: `${config.url}/@${user.username}`, url: `${config.url}/@${user.username}`,
preferredUsername: user.username, preferredUsername: user.username,
name: user.name, 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, icon: avatar ? renderImage(avatar) : null,
image: banner ? renderImage(banner) : null, image: banner ? renderImage(banner) : null,
tag, tag,

View file

@ -34,6 +34,7 @@ import { deliverToRelays } from '../relay';
import { Channel } from '@/models/entities/channel'; import { Channel } from '@/models/entities/channel';
import { normalizeForSearch } from '@/misc/normalize-for-search'; import { normalizeForSearch } from '@/misc/normalize-for-search';
import { getAntennas } from '@/misc/antenna-cache'; import { getAntennas } from '@/misc/antenna-cache';
import { fnNameList } from '@/mfm/fn-name-list';
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; 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 // Parse MFM if needed
if (!tags || !emojis || !mentionedUsers) { if (!tags || !emojis || !mentionedUsers) {
const tokens = data.text ? mfm.parse(data.text)! : []; const tokens = data.text ? mfm.parse(data.text, { fnNameList })! : [];
const cwTokens = data.cw ? mfm.parse(data.cw)! : []; const cwTokens = data.cw ? mfm.parse(data.cw, { fnNameList })! : [];
const choiceTokens = data.poll && data.poll.choices 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); const combinedTokens = tokens.concat(cwTokens).concat(choiceTokens);