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:
parent
56a28923ca
commit
3a6331693a
9 changed files with 34 additions and 53 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
import * as mfm from 'mfm-js';
|
||||
import { unique } from '@/prelude/array';
|
||||
|
||||
export default function(nodes: mfm.MfmNode[]): string[] {
|
||||
const hashtagNodes = [] as mfm.MfmHashtag[];
|
||||
export function extractHashtags(nodes: mfm.MfmNode[]): string[] {
|
||||
const hashtags = [] as string[];
|
||||
|
||||
function scan(nodes: mfm.MfmNode[]) {
|
||||
for (const node of nodes) {
|
||||
if (node.type === 'hashtag') hashtagNodes.push(node);
|
||||
else if (node.children) scan(node.children);
|
||||
mfm.inspect(nodes, (node) => {
|
||||
if (node.type === 'hashtag') {
|
||||
hashtags.push(node.props.hashtag);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
scan(nodes);
|
||||
|
||||
const hashtags = hashtagNodes.map(x => x.props.hashtag);
|
||||
return unique(hashtags);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -6,29 +6,21 @@ import { unique } from '@/prelude/array';
|
|||
const removeHash = (x: string) => x.replace(/#[^#]*$/, '');
|
||||
|
||||
export function extractUrlFromMfm(nodes: mfm.MfmNode[], respectSilentFlag = true): string[] {
|
||||
const urlNodes = [] as (mfm.MfmUrl | mfm.MfmLink)[];
|
||||
let urls = [] as string[];
|
||||
|
||||
function scan(nodes: mfm.MfmNode[]) {
|
||||
for (const node of nodes) {
|
||||
if (node.type === 'url') {
|
||||
urlNodes.push(node);
|
||||
} else if (node.type === 'link') {
|
||||
if (!respectSilentFlag || !node.props.silent) {
|
||||
urlNodes.push(node);
|
||||
}
|
||||
} else if (node.children) {
|
||||
scan(node.children);
|
||||
}
|
||||
mfm.inspect(nodes, (node) => {
|
||||
if (node.type === 'url') {
|
||||
urls.push(node.props.url);
|
||||
} else if (node.type === 'link' && (!respectSilentFlag || !node.props.silent)) {
|
||||
urls.push(node.props.url);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
scan(nodes);
|
||||
|
||||
const urls = unique(urlNodes.map(x => x.props.url));
|
||||
urls = unique(urls);
|
||||
|
||||
return urls.reduce((array, url) => {
|
||||
const removed = removeHash(url);
|
||||
if (!array.map(x => removeHash(x)).includes(removed)) array.push(url);
|
||||
const urlWithoutHash = removeHash(url);
|
||||
if (!array.map(x => removeHash(x)).includes(urlWithoutHash)) array.push(url);
|
||||
return array;
|
||||
}, [] as string[]);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue