From 6d899518b35f696806ae1d3a61b25eeaece5446b Mon Sep 17 00:00:00 2001 From: naskya Date: Sun, 24 Sep 2023 17:58:21 +0900 Subject: [PATCH] chore: make language detection a reusable function in frontend as well --- packages/backend/src/misc/detect-language.ts | 2 +- packages/client/src/components/MkNote.vue | 11 +---------- packages/client/src/components/MkNoteSub.vue | 11 +---------- packages/client/src/scripts/detect-language.ts | 11 +++++++++++ 4 files changed, 14 insertions(+), 21 deletions(-) create mode 100644 packages/client/src/scripts/detect-language.ts diff --git a/packages/backend/src/misc/detect-language.ts b/packages/backend/src/misc/detect-language.ts index 446f85878b..6147247dee 100644 --- a/packages/backend/src/misc/detect-language.ts +++ b/packages/backend/src/misc/detect-language.ts @@ -1,7 +1,7 @@ import { detect } from "tinyld"; import * as mfm from "mfm-js"; -export default function detectLanguage(text: string) { +export default function detectLanguage(text: string): string { const nodes = mfm.parse(text); const filtered = mfm.extract(nodes, (node) => { return node.type === "text" || node.type === "quote"; diff --git a/packages/client/src/components/MkNote.vue b/packages/client/src/components/MkNote.vue index f790889c38..c9228bfda1 100644 --- a/packages/client/src/components/MkNote.vue +++ b/packages/client/src/components/MkNote.vue @@ -276,7 +276,6 @@ import { computed, inject, onMounted, ref } from "vue"; import * as mfm from "mfm-js"; import type { Ref } from "vue"; import type * as firefish from "firefish-js"; -import { detect as detectLanguage_ } from "tinyld"; import MkSubNoteContent from "./MkSubNoteContent.vue"; import MkNoteSub from "@/components/MkNoteSub.vue"; import XNoteHeader from "@/components/MkNoteHeader.vue"; @@ -287,6 +286,7 @@ import XStarButtonNoEmoji from "@/components/MkStarButtonNoEmoji.vue"; import XQuoteButton from "@/components/MkQuoteButton.vue"; import MkVisibility from "@/components/MkVisibility.vue"; import copyToClipboard from "@/scripts/copy-to-clipboard"; +import detectLanguage from "@/scripts/detect-language"; import { url } from "@/config"; import { pleaseLogin } from "@/scripts/please-login"; import { focusNext, focusPrev } from "@/scripts/focus"; @@ -372,15 +372,6 @@ const expandOnNoteClick = defaultStore.state.expandOnNoteClick; const lang = localStorage.getItem("lang"); const translateLang = localStorage.getItem("translateLang"); -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 detectLanguage_(purified); -} - const isForeignLanguage: boolean = defaultStore.state.detectPostLanguage && appearNote.value.text != null && diff --git a/packages/client/src/components/MkNoteSub.vue b/packages/client/src/components/MkNoteSub.vue index 5deee1f88b..73f45b2fa6 100644 --- a/packages/client/src/components/MkNoteSub.vue +++ b/packages/client/src/components/MkNoteSub.vue @@ -193,7 +193,6 @@ import { computed, inject, ref } from "vue"; import type { Ref } from "vue"; import type * as firefish from "firefish-js"; import * as mfm from "mfm-js"; -import { detect as detectLanguage_ } from "tinyld"; import XNoteHeader from "@/components/MkNoteHeader.vue"; import MkSubNoteContent from "@/components/MkSubNoteContent.vue"; import XReactionsViewer from "@/components/MkReactionsViewer.vue"; @@ -202,6 +201,7 @@ import XStarButtonNoEmoji from "@/components/MkStarButtonNoEmoji.vue"; import XRenoteButton from "@/components/MkRenoteButton.vue"; import XQuoteButton from "@/components/MkQuoteButton.vue"; import copyToClipboard from "@/scripts/copy-to-clipboard"; +import detectLanguage from "@/scripts/detect-language"; import { url } from "@/config"; import { pleaseLogin } from "@/scripts/please-login"; import { getNoteMenu } from "@/scripts/get-note-menu"; @@ -288,15 +288,6 @@ const expandOnNoteClick = defaultStore.state.expandOnNoteClick; const lang = localStorage.getItem("lang"); const translateLang = localStorage.getItem("translateLang"); -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 detectLanguage_(purified); -} - const isForeignLanguage: boolean = defaultStore.state.detectPostLanguage && appearNote.value.text != null && diff --git a/packages/client/src/scripts/detect-language.ts b/packages/client/src/scripts/detect-language.ts new file mode 100644 index 0000000000..6147247dee --- /dev/null +++ b/packages/client/src/scripts/detect-language.ts @@ -0,0 +1,11 @@ +import { detect } from "tinyld"; +import * as mfm from "mfm-js"; + +export default function detectLanguage(text: string): 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); +}