summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authorAya Morisawa <AyaMorisawa4869@gmail.com>2018-12-20 19:41:04 +0900
committerAya Morisawa <AyaMorisawa4869@gmail.com>2018-12-20 19:42:10 +0900
commite9f8897fe28249642d47dd1ecf3e6a76b552ddf5 (patch)
treec64445f671f782ae2a08e149400b1297f99e5eae /src/misc
parentFix overlap of birthday label on datepicker (#3697) (diff)
downloadsharkey-e9f8897fe28249642d47dd1ecf3e6a76b552ddf5.tar.gz
sharkey-e9f8897fe28249642d47dd1ecf3e6a76b552ddf5.tar.bz2
sharkey-e9f8897fe28249642d47dd1ecf3e6a76b552ddf5.zip
Refactor MFM
Co-authored-by: syuilo syuilotan@yahoo.co.jp
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/extract-emojis.ts9
-rw-r--r--src/misc/extract-hashtags.ts9
-rw-r--r--src/misc/extract-mentions.ts23
3 files changed, 25 insertions, 16 deletions
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<typeof parse>): 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);
}