Skip to content

Commit

Permalink
Fix(Backend): MfmService.toHtml を JSDOM に戻す
Browse files Browse the repository at this point in the history
Co-authored-by: riku6460 <[email protected]>
  • Loading branch information
mattyatea and riku6460 committed Jul 20, 2024
1 parent a059034 commit 1502bc6
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions packages/backend/src/core/MfmService.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project , Type4ny-project
* SPDX-FileCopyrightText: syuilo and misskey-project, Type4ny-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common';
import * as parse5 from 'parse5';
import { JSDOM } from 'jsdom';
import serialize from 'w3c-xmlserializer';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { intersperse } from '@/misc/prelude/array.js';
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
import type { IMentionedRemoteUsers } from '@/models/Note.js';
import { bindThis } from '@/decorators.js';
import type { DefaultTreeAdapterMap } from 'parse5';
import * as TreeAdapter from '../../node_modules/parse5/dist/tree-adapters/default.js';
import type * as mfm from 'mfm-js';

const treeAdapter = parse5.defaultTreeAdapter;
type Node = DefaultTreeAdapterMap['node'];
type ChildNode = DefaultTreeAdapterMap['childNode'];
const treeAdapter = TreeAdapter.defaultTreeAdapter;

const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/;
const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/;
Expand Down Expand Up @@ -49,7 +46,7 @@ export class MfmService {

return text.trim();

function getText(node: Node): string {
function getText(node: TreeAdapter.Node): string {
if (treeAdapter.isTextNode(node)) return node.value;
if (!treeAdapter.isElementNode(node)) return '';
if (node.nodeName === 'br') return '\n';
Expand All @@ -61,32 +58,31 @@ export class MfmService {
return '';
}

function appendChildren(childNodes: ChildNode[]): void {
function appendChildren(childNodes: TreeAdapter.ChildNode[]): void {
if (childNodes) {
for (const n of childNodes) {
analyze(n);
}
}
}

function analyze(node: Node) {
function analyze(node: TreeAdapter.Node) {
if (treeAdapter.isTextNode(node)) {
text += node.value;
return;
}

// Skip comment or document type node
if (!treeAdapter.isElementNode(node)) {
return;
}
if (!treeAdapter.isElementNode(node)) return;

switch (node.nodeName) {
case 'br': {
text += '\n';
break;
}

case 'a': {
case 'a':
{
const txt = getText(node);
const rel = node.attrs.find(x => x.name === 'rel');
const href = node.attrs.find(x => x.name === 'href');
Expand Down Expand Up @@ -134,38 +130,43 @@ export class MfmService {
break;
}

case 'h1': {
case 'h1':
{
text += '【';
appendChildren(node.childNodes);
text += '】\n';
break;
}

case 'b':
case 'strong': {
case 'strong':
{
text += '**';
appendChildren(node.childNodes);
text += '**';
break;
}

case 'small': {
case 'small':
{
text += '<small>';
appendChildren(node.childNodes);
text += '</small>';
break;
}

case 's':
case 'del': {
case 'del':
{
text += '~~';
appendChildren(node.childNodes);
text += '~~';
break;
}

case 'i':
case 'em': {
case 'em':
{
text += '<i>';
appendChildren(node.childNodes);
text += '</i>';
Expand Down Expand Up @@ -206,7 +207,8 @@ export class MfmService {
case 'h3':
case 'h4':
case 'h5':
case 'h6': {
case 'h6':
{
text += '\n\n';
appendChildren(node.childNodes);
break;
Expand All @@ -219,7 +221,8 @@ export class MfmService {
case 'article':
case 'li':
case 'dt':
case 'dd': {
case 'dd':
{
text += '\n';
appendChildren(node.childNodes);
break;
Expand All @@ -240,9 +243,9 @@ export class MfmService {
return null;
}

const { window } = new JSDOM() as unknown as { window: Window };
const fragment = JSDOM.fragment('');

const doc = window.document;
const doc = fragment.ownerDocument;

const body = doc.createElement('p');

Expand Down Expand Up @@ -458,6 +461,6 @@ export class MfmService {

appendChildren(nodes, body);

return serialize(body);
return body.outerHTML;
}
}

0 comments on commit 1502bc6

Please sign in to comment.