9
src/misc/extract-emojis.ts
Normal file
9
src/misc/extract-emojis.ts
Normal file
@ -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);
|
||||
}
|
9
src/misc/extract-hashtags.ts
Normal file
9
src/misc/extract-hashtags.ts
Normal file
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user