From 1ec3338d2e0a0286a86d766bce3edc87e8c9a158 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sun, 11 Apr 2021 00:18:29 +0900 Subject: [PATCH] update mfm.js (#7435) * use mfm.js 0.14.0 * use mfm.extract * use mfm.extract * use mfm.extract --- package.json | 2 +- src/misc/extract-custom-emojis-from-mfm.ts | 10 +++------- src/misc/extract-hashtags.ts | 11 +++-------- src/misc/extract-mentions.ts | 9 ++------- src/misc/extract-url-from-mfm.ts | 13 +++---------- 5 files changed, 12 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 954a8cc62..aaa3a4376 100644 --- a/package.json +++ b/package.json @@ -181,7 +181,7 @@ "markdown-it": "12.0.4", "markdown-it-anchor": "7.1.0", "matter-js": "0.16.1", - "mfm-js": "0.12.0", + "mfm-js": "0.14.0", "mocha": "8.3.2", "moji": "0.5.1", "ms": "2.1.3", diff --git a/src/misc/extract-custom-emojis-from-mfm.ts b/src/misc/extract-custom-emojis-from-mfm.ts index ef2cb1fd6..b29ce281b 100644 --- a/src/misc/extract-custom-emojis-from-mfm.ts +++ b/src/misc/extract-custom-emojis-from-mfm.ts @@ -2,13 +2,9 @@ import * as mfm from 'mfm-js'; import { unique } from '@/prelude/array'; export function extractCustomEmojisFromMfm(nodes: mfm.MfmNode[]): string[] { - const emojis = [] as string[]; - - mfm.inspect(nodes, (node) => { - if (node.type === 'emojiCode' && node.props.name.length <= 100) { - emojis.push(node.props.name); - } + const emojiNodes = mfm.extract(nodes, (node) => { + return (node.type === 'emojiCode' && node.props.name.length <= 100); }); - return unique(emojis); + return unique(emojiNodes.map(x => x.props.name)); } diff --git a/src/misc/extract-hashtags.ts b/src/misc/extract-hashtags.ts index f89b699a9..b0a74df21 100644 --- a/src/misc/extract-hashtags.ts +++ b/src/misc/extract-hashtags.ts @@ -2,13 +2,8 @@ import * as mfm from 'mfm-js'; import { unique } from '@/prelude/array'; export function extractHashtags(nodes: mfm.MfmNode[]): string[] { - const hashtags = [] as string[]; + const hashtagNodes = mfm.extract(nodes, (node) => node.type === 'hashtag'); + const hashtags = unique(hashtagNodes.map(x => x.props.hashtag)); - mfm.inspect(nodes, (node) => { - if (node.type === 'hashtag') { - hashtags.push(node.props.hashtag); - } - }); - - return unique(hashtags); + return hashtags; } diff --git a/src/misc/extract-mentions.ts b/src/misc/extract-mentions.ts index a0e2fb6d5..cc19b161a 100644 --- a/src/misc/extract-mentions.ts +++ b/src/misc/extract-mentions.ts @@ -4,13 +4,8 @@ import * as mfm from 'mfm-js'; export function extractMentions(nodes: mfm.MfmNode[]): mfm.MfmMention['props'][] { // TODO: 重複を削除 - const mentions = [] as mfm.MfmMention['props'][]; - - mfm.inspect(nodes, (node) => { - if (node.type === 'mention') { - mentions.push(node.props); - } - }); + const mentionNodes = mfm.extract(nodes, (node) => node.type === 'mention'); + const mentions = mentionNodes.map(x => x.props); return mentions; } diff --git a/src/misc/extract-url-from-mfm.ts b/src/misc/extract-url-from-mfm.ts index ec3b019ea..e6d5d0104 100644 --- a/src/misc/extract-url-from-mfm.ts +++ b/src/misc/extract-url-from-mfm.ts @@ -6,17 +6,10 @@ import { unique } from '@/prelude/array'; const removeHash = (x: string) => x.replace(/#[^#]*$/, ''); export function extractUrlFromMfm(nodes: mfm.MfmNode[], respectSilentFlag = true): string[] { - let urls = [] as string[]; - - mfm.inspect(nodes, (node) => { - if (node.type === 'url') { - urls.push(node.props.url); - } else if (node.type === 'link' && (!respectSilentFlag || !node.props.silent)) { - urls.push(node.props.url); - } + const urlNodes = mfm.extract(nodes, (node) => { + return (node.type === 'url') || (node.type === 'link' && (!respectSilentFlag || !node.props.silent)); }); - - urls = unique(urls); + const urls: string[] = unique(urlNodes.map(x => x.props.url)); return urls.reduce((array, url) => { const urlWithoutHash = removeHash(url);