refactor mfm extract (#7434)

* refactor extractCustomEmojisFromMfm()

* refactor extract-hashtags

* refactor extract-mentions

* refactor extract-hashtags

* refactor extract-url-from-mfm

* refactor extract-mentions
This commit is contained in:
marihachi 2021-04-10 17:50:18 +09:00 committed by GitHub
parent 56a28923ca
commit 3a6331693a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 53 deletions

View file

@ -2,17 +2,13 @@ import * as mfm from 'mfm-js';
import { unique } from '@/prelude/array';
export function extractCustomEmojisFromMfm(nodes: mfm.MfmNode[]): string[] {
const emojiNodes = [] as mfm.MfmEmojiCode[];
const emojis = [] as string[];
function scan(nodes: mfm.MfmNode[]) {
for (const node of nodes) {
if (node.type === 'emojiCode') emojiNodes.push(node);
else if (node.children) scan(node.children);
mfm.inspect(nodes, (node) => {
if (node.type === 'emojiCode' && node.props.name.length <= 100) {
emojis.push(node.props.name);
}
}
});
scan(nodes);
const emojis = emojiNodes.filter(x => x.props.name.length <= 100).map(x => x.props.name!);
return unique(emojis);
}