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,18 +2,15 @@
import * as mfm from 'mfm-js';
export default function(nodes: mfm.MfmNode[]): mfm.MfmMention['props'][] {
export function extractMentions(nodes: mfm.MfmNode[]): mfm.MfmMention['props'][] {
// TODO: 重複を削除
const mentionNodes = [] as mfm.MfmMention[];
const mentions = [] as mfm.MfmMention['props'][];
function scan(nodes: mfm.MfmNode[]) {
for (const node of nodes) {
if (node.type === 'mention') mentionNodes.push(node);
else if (node.children) scan(node.children);
mfm.inspect(nodes, (node) => {
if (node.type === 'mention') {
mentions.push(node.props);
}
}
});
scan(nodes);
return mentionNodes.map(x => x.props);
return mentions;
}