fix(backend): parse5関係の型のimport方法を変更 (#14146)

This commit is contained in:
おさむのひと 2024-07-07 09:55:06 +09:00 committed by GitHub
parent 0ea88c07b4
commit fe852920c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,16 +7,18 @@ import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import * as parse5 from 'parse5'; import * as parse5 from 'parse5';
import { Window, XMLSerializer } from 'happy-dom'; import { Window, XMLSerializer } from 'happy-dom';
import * as TreeAdapter from 'parse5/dist/tree-adapters/default.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { intersperse } from '@/misc/prelude/array.js'; import { intersperse } from '@/misc/prelude/array.js';
import { normalizeForSearch } from '@/misc/normalize-for-search.js'; import { normalizeForSearch } from '@/misc/normalize-for-search.js';
import type { IMentionedRemoteUsers } from '@/models/Note.js'; import type { IMentionedRemoteUsers } from '@/models/Note.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import type { DefaultTreeAdapterMap } from 'parse5';
import type * as mfm from 'mfm-js'; import type * as mfm from 'mfm-js';
const treeAdapter = TreeAdapter.defaultTreeAdapter; const treeAdapter = parse5.defaultTreeAdapter;
type Node = DefaultTreeAdapterMap['node'];
type ChildNode = DefaultTreeAdapterMap['childNode'];
const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/; const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/;
const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/; const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/;
@ -46,7 +48,7 @@ export class MfmService {
return text.trim(); return text.trim();
function getText(node: TreeAdapter.Node): string { function getText(node: Node): string {
if (treeAdapter.isTextNode(node)) return node.value; if (treeAdapter.isTextNode(node)) return node.value;
if (!treeAdapter.isElementNode(node)) return ''; if (!treeAdapter.isElementNode(node)) return '';
if (node.nodeName === 'br') return '\n'; if (node.nodeName === 'br') return '\n';
@ -58,7 +60,7 @@ export class MfmService {
return ''; return '';
} }
function appendChildren(childNodes: TreeAdapter.ChildNode[]): void { function appendChildren(childNodes: ChildNode[]): void {
if (childNodes) { if (childNodes) {
for (const n of childNodes) { for (const n of childNodes) {
analyze(n); analyze(n);
@ -66,14 +68,16 @@ export class MfmService {
} }
} }
function analyze(node: TreeAdapter.Node) { function analyze(node: Node) {
if (treeAdapter.isTextNode(node)) { if (treeAdapter.isTextNode(node)) {
text += node.value; text += node.value;
return; return;
} }
// Skip comment or document type node // Skip comment or document type node
if (!treeAdapter.isElementNode(node)) return; if (!treeAdapter.isElementNode(node)) {
return;
}
switch (node.nodeName) { switch (node.nodeName) {
case 'br': { case 'br': {
@ -81,8 +85,7 @@ export class MfmService {
break; break;
} }
case 'a': case 'a': {
{
const txt = getText(node); const txt = getText(node);
const rel = node.attrs.find(x => x.name === 'rel'); const rel = node.attrs.find(x => x.name === 'rel');
const href = node.attrs.find(x => x.name === 'href'); const href = node.attrs.find(x => x.name === 'href');
@ -90,7 +93,7 @@ export class MfmService {
// ハッシュタグ // ハッシュタグ
if (normalizedHashtagNames && href && normalizedHashtagNames.has(normalizeForSearch(txt))) { if (normalizedHashtagNames && href && normalizedHashtagNames.has(normalizeForSearch(txt))) {
text += txt; text += txt;
// メンション // メンション
} else if (txt.startsWith('@') && !(rel && rel.value.startsWith('me '))) { } else if (txt.startsWith('@') && !(rel && rel.value.startsWith('me '))) {
const part = txt.split('@'); const part = txt.split('@');
@ -102,7 +105,7 @@ export class MfmService {
} else if (part.length === 3) { } else if (part.length === 3) {
text += txt; text += txt;
} }
// その他 // その他
} else { } else {
const generateLink = () => { const generateLink = () => {
if (!href && !txt) { if (!href && !txt) {
@ -130,8 +133,7 @@ export class MfmService {
break; break;
} }
case 'h1': case 'h1': {
{
text += '【'; text += '【';
appendChildren(node.childNodes); appendChildren(node.childNodes);
text += '】\n'; text += '】\n';
@ -139,16 +141,14 @@ export class MfmService {
} }
case 'b': case 'b':
case 'strong': case 'strong': {
{
text += '**'; text += '**';
appendChildren(node.childNodes); appendChildren(node.childNodes);
text += '**'; text += '**';
break; break;
} }
case 'small': case 'small': {
{
text += '<small>'; text += '<small>';
appendChildren(node.childNodes); appendChildren(node.childNodes);
text += '</small>'; text += '</small>';
@ -156,8 +156,7 @@ export class MfmService {
} }
case 's': case 's':
case 'del': case 'del': {
{
text += '~~'; text += '~~';
appendChildren(node.childNodes); appendChildren(node.childNodes);
text += '~~'; text += '~~';
@ -165,8 +164,7 @@ export class MfmService {
} }
case 'i': case 'i':
case 'em': case 'em': {
{
text += '<i>'; text += '<i>';
appendChildren(node.childNodes); appendChildren(node.childNodes);
text += '</i>'; text += '</i>';
@ -207,8 +205,7 @@ export class MfmService {
case 'h3': case 'h3':
case 'h4': case 'h4':
case 'h5': case 'h5':
case 'h6': case 'h6': {
{
text += '\n\n'; text += '\n\n';
appendChildren(node.childNodes); appendChildren(node.childNodes);
break; break;
@ -221,8 +218,7 @@ export class MfmService {
case 'article': case 'article':
case 'li': case 'li':
case 'dt': case 'dt':
case 'dd': case 'dd': {
{
text += '\n'; text += '\n';
appendChildren(node.childNodes); appendChildren(node.childNodes);
break; break;