chore: make language detection a reusable function in frontend as well

This commit is contained in:
naskya 2023-09-24 17:58:21 +09:00
parent 87c54575c1
commit 6d899518b3
No known key found for this signature in database
GPG Key ID: 164DFF24E2D40139
4 changed files with 14 additions and 21 deletions

View File

@ -1,7 +1,7 @@
import { detect } from "tinyld"; import { detect } from "tinyld";
import * as mfm from "mfm-js"; 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 nodes = mfm.parse(text);
const filtered = mfm.extract(nodes, (node) => { const filtered = mfm.extract(nodes, (node) => {
return node.type === "text" || node.type === "quote"; return node.type === "text" || node.type === "quote";

View File

@ -276,7 +276,6 @@ import { computed, inject, onMounted, ref } from "vue";
import * as mfm from "mfm-js"; import * as mfm from "mfm-js";
import type { Ref } from "vue"; import type { Ref } from "vue";
import type * as firefish from "firefish-js"; import type * as firefish from "firefish-js";
import { detect as detectLanguage_ } from "tinyld";
import MkSubNoteContent from "./MkSubNoteContent.vue"; import MkSubNoteContent from "./MkSubNoteContent.vue";
import MkNoteSub from "@/components/MkNoteSub.vue"; import MkNoteSub from "@/components/MkNoteSub.vue";
import XNoteHeader from "@/components/MkNoteHeader.vue"; import XNoteHeader from "@/components/MkNoteHeader.vue";
@ -287,6 +286,7 @@ import XStarButtonNoEmoji from "@/components/MkStarButtonNoEmoji.vue";
import XQuoteButton from "@/components/MkQuoteButton.vue"; import XQuoteButton from "@/components/MkQuoteButton.vue";
import MkVisibility from "@/components/MkVisibility.vue"; import MkVisibility from "@/components/MkVisibility.vue";
import copyToClipboard from "@/scripts/copy-to-clipboard"; import copyToClipboard from "@/scripts/copy-to-clipboard";
import detectLanguage from "@/scripts/detect-language";
import { url } from "@/config"; import { url } from "@/config";
import { pleaseLogin } from "@/scripts/please-login"; import { pleaseLogin } from "@/scripts/please-login";
import { focusNext, focusPrev } from "@/scripts/focus"; import { focusNext, focusPrev } from "@/scripts/focus";
@ -372,15 +372,6 @@ const expandOnNoteClick = defaultStore.state.expandOnNoteClick;
const lang = localStorage.getItem("lang"); const lang = localStorage.getItem("lang");
const translateLang = localStorage.getItem("translateLang"); 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 = const isForeignLanguage: boolean =
defaultStore.state.detectPostLanguage && defaultStore.state.detectPostLanguage &&
appearNote.value.text != null && appearNote.value.text != null &&

View File

@ -193,7 +193,6 @@ import { computed, inject, ref } from "vue";
import type { Ref } from "vue"; import type { Ref } from "vue";
import type * as firefish from "firefish-js"; import type * as firefish from "firefish-js";
import * as mfm from "mfm-js"; import * as mfm from "mfm-js";
import { detect as detectLanguage_ } from "tinyld";
import XNoteHeader from "@/components/MkNoteHeader.vue"; import XNoteHeader from "@/components/MkNoteHeader.vue";
import MkSubNoteContent from "@/components/MkSubNoteContent.vue"; import MkSubNoteContent from "@/components/MkSubNoteContent.vue";
import XReactionsViewer from "@/components/MkReactionsViewer.vue"; import XReactionsViewer from "@/components/MkReactionsViewer.vue";
@ -202,6 +201,7 @@ import XStarButtonNoEmoji from "@/components/MkStarButtonNoEmoji.vue";
import XRenoteButton from "@/components/MkRenoteButton.vue"; import XRenoteButton from "@/components/MkRenoteButton.vue";
import XQuoteButton from "@/components/MkQuoteButton.vue"; import XQuoteButton from "@/components/MkQuoteButton.vue";
import copyToClipboard from "@/scripts/copy-to-clipboard"; import copyToClipboard from "@/scripts/copy-to-clipboard";
import detectLanguage from "@/scripts/detect-language";
import { url } from "@/config"; import { url } from "@/config";
import { pleaseLogin } from "@/scripts/please-login"; import { pleaseLogin } from "@/scripts/please-login";
import { getNoteMenu } from "@/scripts/get-note-menu"; import { getNoteMenu } from "@/scripts/get-note-menu";
@ -288,15 +288,6 @@ const expandOnNoteClick = defaultStore.state.expandOnNoteClick;
const lang = localStorage.getItem("lang"); const lang = localStorage.getItem("lang");
const translateLang = localStorage.getItem("translateLang"); 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 = const isForeignLanguage: boolean =
defaultStore.state.detectPostLanguage && defaultStore.state.detectPostLanguage &&
appearNote.value.text != null && appearNote.value.text != null &&

View File

@ -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);
}