From e9f8897fe28249642d47dd1ecf3e6a76b552ddf5 Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Thu, 20 Dec 2018 19:41:04 +0900 Subject: Refactor MFM Co-authored-by: syuilo syuilotan@yahoo.co.jp --- src/misc/extract-emojis.ts | 9 +++++++++ src/misc/extract-hashtags.ts | 9 +++++++++ src/misc/extract-mentions.ts | 23 +++++++---------------- 3 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 src/misc/extract-emojis.ts create mode 100644 src/misc/extract-hashtags.ts (limited to 'src/misc') diff --git a/src/misc/extract-emojis.ts b/src/misc/extract-emojis.ts new file mode 100644 index 0000000000..a7b949f4f7 --- /dev/null +++ b/src/misc/extract-emojis.ts @@ -0,0 +1,9 @@ +import { EmojiNode, MfmForest } from '../mfm/parser'; +import { preorderF } from '../prelude/tree'; +import { unique } from '../prelude/array'; + +export default function(mfmForest: MfmForest): string[] { + const emojiNodes = preorderF(mfmForest).filter(x => x.type === 'emoji') as EmojiNode[]; + const emojis = emojiNodes.filter(x => x.props.name && x.props.name.length <= 100).map(x => x.props.name); + return unique(emojis); +} diff --git a/src/misc/extract-hashtags.ts b/src/misc/extract-hashtags.ts new file mode 100644 index 0000000000..43eaa45909 --- /dev/null +++ b/src/misc/extract-hashtags.ts @@ -0,0 +1,9 @@ +import { HashtagNode, MfmForest } from '../mfm/parser'; +import { preorderF } from '../prelude/tree'; +import { unique } from '../prelude/array'; + +export default function(mfmForest: MfmForest): string[] { + const hashtagNodes = preorderF(mfmForest).filter(x => x.type === 'hashtag') as HashtagNode[]; + const hashtags = hashtagNodes.map(x => x.props.hashtag); + return unique(hashtags); +} diff --git a/src/misc/extract-mentions.ts b/src/misc/extract-mentions.ts index 1d844211c0..a53a25ffc4 100644 --- a/src/misc/extract-mentions.ts +++ b/src/misc/extract-mentions.ts @@ -1,19 +1,10 @@ -import parse from '../mfm/parse'; -import { Node, IMentionNode } from '../mfm/parser'; +// test is located in test/extract-mentions -export default function(tokens: ReturnType): IMentionNode['props'][] { - const mentions: IMentionNode['props'][] = []; +import { MentionNode, MfmForest } from '../mfm/parser'; +import { preorderF } from '../prelude/tree'; - const extract = (tokens: Node[]) => { - for (const x of tokens.filter(x => x.name === 'mention')) { - mentions.push(x.props); - } - for (const x of tokens.filter(x => x.children)) { - extract(x.children); - } - }; - - extract(tokens); - - return mentions; +export default function(mfmForest: MfmForest): MentionNode['props'][] { + // TODO: 重複を削除 + const mentionNodes = preorderF(mfmForest).filter(x => x.type === 'mention') as MentionNode[]; + return mentionNodes.map(x => x.props); } -- cgit v1.2.3-freya