paricafe/packages/frontend/src/scripts/detect-language.ts
2024-10-17 23:43:41 +08:00

18 lines
494 B
TypeScript

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { detect } from 'tinyld/heavy';
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);
if (detect(purified) === '') return 'en';
return detect(purified);
}