diff --git a/packages/backend/src/misc/detect-language.ts b/packages/backend/src/misc/detect-language.ts new file mode 100644 index 0000000000..446f85878b --- /dev/null +++ b/packages/backend/src/misc/detect-language.ts @@ -0,0 +1,11 @@ +import { detect } from "tinyld"; +import * as mfm from "mfm-js"; + +export default function detectLanguage(text: string) { + const nodes = mfm.parse(text); + const filtered = mfm.extract(nodes, (node) => { + return node.type === "text" || node.type === "quote"; + }); + const purified = mfm.toString(filtered); + return detect(purified); +} diff --git a/packages/backend/src/remote/activitypub/renderer/note.ts b/packages/backend/src/remote/activitypub/renderer/note.ts index 4c7650e8cc..b840e2d6f1 100644 --- a/packages/backend/src/remote/activitypub/renderer/note.ts +++ b/packages/backend/src/remote/activitypub/renderer/note.ts @@ -1,12 +1,12 @@ import { In, IsNull } from "typeorm"; -import { detect as detectLanguage } from "tinyld"; import config from "@/config/index.js"; import type { Note, IMentionedRemoteUsers } from "@/models/entities/note.js"; import type { DriveFile } from "@/models/entities/drive-file.js"; import { DriveFiles, Notes, Users, Emojis, Polls } from "@/models/index.js"; import type { Emoji } from "@/models/entities/emoji.js"; import type { Poll } from "@/models/entities/poll.js"; -import toHtml from "../misc/get-note-html.js"; +import toHtml from "@/remote/activitypub/misc/get-note-html.js"; +import detectLanguage from "@/misc/detect-language.js"; import renderEmoji from "./emoji.js"; import renderMention from "./mention.js"; import renderHashtag from "./hashtag.js"; diff --git a/packages/backend/src/server/api/endpoints/notes/edit.ts b/packages/backend/src/server/api/endpoints/notes/edit.ts index 9add576708..e6d39c5249 100644 --- a/packages/backend/src/server/api/endpoints/notes/edit.ts +++ b/packages/backend/src/server/api/endpoints/notes/edit.ts @@ -35,8 +35,8 @@ import renderUpdate from "@/remote/activitypub/renderer/update.js"; import { deliverToRelays } from "@/services/relay.js"; // import { deliverQuestionUpdate } from "@/services/note/polls/update.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; -import { detect as detectLanguage } from "tinyld"; import { langmap } from "@/misc/langmap.js"; +import detectLanguage from "@/misc/detect-language.js"; export const meta = { tags: ["notes"], diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index dcd2428fe1..632cb960e2 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -64,11 +64,11 @@ import type { UserProfile } from "@/models/entities/user-profile.js"; import { db } from "@/db/postgre.js"; import { getActiveWebhooks } from "@/misc/webhook-cache.js"; import { shouldSilenceInstance } from "@/misc/should-block-instance.js"; -import meilisearch from "../../db/meilisearch.js"; +import meilisearch from "@/db/meilisearch.js"; import { redisClient } from "@/db/redis.js"; import { Mutex } from "redis-semaphore"; -import { detect as detectLanguage } from "tinyld"; import { langmap } from "@/misc/langmap.js"; +import detectLanguage from "@/misc/detect-language.js"; const mutedWordsCache = new Cache< { userId: UserProfile["userId"]; mutedWords: UserProfile["mutedWords"] }[]