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
|
@ -61,7 +61,7 @@ import XNotePreview from './note-preview.vue';
|
||||||
import * as mfm from 'mfm-js';
|
import * as mfm from 'mfm-js';
|
||||||
import { host, url } from '@client/config';
|
import { host, url } from '@client/config';
|
||||||
import { erase, unique } from '../../prelude/array';
|
import { erase, unique } from '../../prelude/array';
|
||||||
import extractMentions from '@/misc/extract-mentions';
|
import { extractMentions } from '@/misc/extract-mentions';
|
||||||
import getAcct from '@/misc/acct/render';
|
import getAcct from '@/misc/acct/render';
|
||||||
import { formatTimeString } from '@/misc/format-time-string';
|
import { formatTimeString } from '@/misc/format-time-string';
|
||||||
import { Autocomplete } from '@client/scripts/autocomplete';
|
import { Autocomplete } from '@client/scripts/autocomplete';
|
||||||
|
|
|
@ -56,7 +56,7 @@ import { toASCII } from 'punycode/';
|
||||||
import * as mfm from 'mfm-js';
|
import * as mfm from 'mfm-js';
|
||||||
import { host, url } from '@client/config';
|
import { host, url } from '@client/config';
|
||||||
import { erase, unique } from '../../../prelude/array';
|
import { erase, unique } from '../../../prelude/array';
|
||||||
import extractMentions from '@/misc/extract-mentions';
|
import { extractMentions } from '@/misc/extract-mentions';
|
||||||
import getAcct from '@/misc/acct/render';
|
import getAcct from '@/misc/acct/render';
|
||||||
import { formatTimeString } from '@/misc/format-time-string';
|
import { formatTimeString } from '@/misc/format-time-string';
|
||||||
import { Autocomplete } from '@client/scripts/autocomplete';
|
import { Autocomplete } from '@client/scripts/autocomplete';
|
||||||
|
|
|
@ -2,17 +2,13 @@ import * as mfm from 'mfm-js';
|
||||||
import { unique } from '@/prelude/array';
|
import { unique } from '@/prelude/array';
|
||||||
|
|
||||||
export function extractCustomEmojisFromMfm(nodes: mfm.MfmNode[]): string[] {
|
export function extractCustomEmojisFromMfm(nodes: mfm.MfmNode[]): string[] {
|
||||||
const emojiNodes = [] as mfm.MfmEmojiCode[];
|
const emojis = [] as string[];
|
||||||
|
|
||||||
function scan(nodes: mfm.MfmNode[]) {
|
mfm.inspect(nodes, (node) => {
|
||||||
for (const node of nodes) {
|
if (node.type === 'emojiCode' && node.props.name.length <= 100) {
|
||||||
if (node.type === 'emojiCode') emojiNodes.push(node);
|
emojis.push(node.props.name);
|
||||||
else if (node.children) scan(node.children);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
scan(nodes);
|
|
||||||
|
|
||||||
const emojis = emojiNodes.filter(x => x.props.name.length <= 100).map(x => x.props.name!);
|
|
||||||
return unique(emojis);
|
return unique(emojis);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,14 @@
|
||||||
import * as mfm from 'mfm-js';
|
import * as mfm from 'mfm-js';
|
||||||
import { unique } from '@/prelude/array';
|
import { unique } from '@/prelude/array';
|
||||||
|
|
||||||
export default function(nodes: mfm.MfmNode[]): string[] {
|
export function extractHashtags(nodes: mfm.MfmNode[]): string[] {
|
||||||
const hashtagNodes = [] as mfm.MfmHashtag[];
|
const hashtags = [] as string[];
|
||||||
|
|
||||||
function scan(nodes: mfm.MfmNode[]) {
|
mfm.inspect(nodes, (node) => {
|
||||||
for (const node of nodes) {
|
if (node.type === 'hashtag') {
|
||||||
if (node.type === 'hashtag') hashtagNodes.push(node);
|
hashtags.push(node.props.hashtag);
|
||||||
else if (node.children) scan(node.children);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
scan(nodes);
|
|
||||||
|
|
||||||
const hashtags = hashtagNodes.map(x => x.props.hashtag);
|
|
||||||
return unique(hashtags);
|
return unique(hashtags);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,18 +2,15 @@
|
||||||
|
|
||||||
import * as mfm from 'mfm-js';
|
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: 重複を削除
|
// TODO: 重複を削除
|
||||||
const mentionNodes = [] as mfm.MfmMention[];
|
const mentions = [] as mfm.MfmMention['props'][];
|
||||||
|
|
||||||
function scan(nodes: mfm.MfmNode[]) {
|
mfm.inspect(nodes, (node) => {
|
||||||
for (const node of nodes) {
|
if (node.type === 'mention') {
|
||||||
if (node.type === 'mention') mentionNodes.push(node);
|
mentions.push(node.props);
|
||||||
else if (node.children) scan(node.children);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
scan(nodes);
|
return mentions;
|
||||||
|
|
||||||
return mentionNodes.map(x => x.props);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,29 +6,21 @@ import { unique } from '@/prelude/array';
|
||||||
const removeHash = (x: string) => x.replace(/#[^#]*$/, '');
|
const removeHash = (x: string) => x.replace(/#[^#]*$/, '');
|
||||||
|
|
||||||
export function extractUrlFromMfm(nodes: mfm.MfmNode[], respectSilentFlag = true): string[] {
|
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[]) {
|
mfm.inspect(nodes, (node) => {
|
||||||
for (const node of nodes) {
|
|
||||||
if (node.type === 'url') {
|
if (node.type === 'url') {
|
||||||
urlNodes.push(node);
|
urls.push(node.props.url);
|
||||||
} else if (node.type === 'link') {
|
} else if (node.type === 'link' && (!respectSilentFlag || !node.props.silent)) {
|
||||||
if (!respectSilentFlag || !node.props.silent) {
|
urls.push(node.props.url);
|
||||||
urlNodes.push(node);
|
|
||||||
}
|
|
||||||
} else if (node.children) {
|
|
||||||
scan(node.children);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
scan(nodes);
|
urls = unique(urls);
|
||||||
|
|
||||||
const urls = unique(urlNodes.map(x => x.props.url));
|
|
||||||
|
|
||||||
return urls.reduce((array, url) => {
|
return urls.reduce((array, url) => {
|
||||||
const removed = removeHash(url);
|
const urlWithoutHash = removeHash(url);
|
||||||
if (!array.map(x => removeHash(x)).includes(removed)) array.push(url);
|
if (!array.map(x => removeHash(x)).includes(urlWithoutHash)) array.push(url);
|
||||||
return array;
|
return array;
|
||||||
}, [] as string[]);
|
}, [] as string[]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import acceptAllFollowRequests from '../../../../services/following/requests/acc
|
||||||
import { publishToFollowers } from '../../../../services/i/update';
|
import { publishToFollowers } from '../../../../services/i/update';
|
||||||
import define from '../../define';
|
import define from '../../define';
|
||||||
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm';
|
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm';
|
||||||
import extractHashtags from '@/misc/extract-hashtags';
|
import { extractHashtags } from '@/misc/extract-hashtags';
|
||||||
import * as langmap from 'langmap';
|
import * as langmap from 'langmap';
|
||||||
import { updateUsertags } from '../../../../services/update-hashtag';
|
import { updateUsertags } from '../../../../services/update-hashtag';
|
||||||
import { ApiError } from '../../error';
|
import { ApiError } from '../../error';
|
||||||
|
|
|
@ -12,9 +12,9 @@ import { updateHashtags } from '../update-hashtag';
|
||||||
import { concat } from '../../prelude/array';
|
import { concat } from '../../prelude/array';
|
||||||
import insertNoteUnread from './unread';
|
import insertNoteUnread from './unread';
|
||||||
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
||||||
import extractMentions from '@/misc/extract-mentions';
|
import { extractMentions } from '@/misc/extract-mentions';
|
||||||
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm';
|
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm';
|
||||||
import extractHashtags from '@/misc/extract-hashtags';
|
import { extractHashtags } from '@/misc/extract-hashtags';
|
||||||
import { Note, IMentionedRemoteUsers } from '../../models/entities/note';
|
import { Note, IMentionedRemoteUsers } from '../../models/entities/note';
|
||||||
import { Mutings, Users, NoteWatchings, Notes, Instances, UserProfiles, Antennas, Followings, MutedNotes, Channels, ChannelFollowings } from '../../models';
|
import { Mutings, Users, NoteWatchings, Notes, Instances, UserProfiles, Antennas, Followings, MutedNotes, Channels, ChannelFollowings } from '../../models';
|
||||||
import { DriveFile } from '../../models/entities/drive-file';
|
import { DriveFile } from '../../models/entities/drive-file';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as assert from 'assert';
|
import * as assert from 'assert';
|
||||||
|
|
||||||
import extractMentions from '../src/misc/extract-mentions';
|
import { extractMentions } from '../src/misc/extract-mentions';
|
||||||
import { parse } from 'mfm-js';
|
import { parse } from 'mfm-js';
|
||||||
|
|
||||||
describe('Extract mentions', () => {
|
describe('Extract mentions', () => {
|
||||||
|
|
Loading…
Reference in a new issue