From dbea4bcd2751f7975bfefe99434fd330e66fcea4 Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Sat, 23 Nov 2024 16:10:28 +0900 Subject: [PATCH] =?UTF-8?q?feat(aiscript):=20MFM=E3=82=92=E8=80=83?= =?UTF-8?q?=E6=85=AE=E3=81=97=E3=81=A6=E3=83=86=E3=82=AD=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=92=E6=93=8D=E4=BD=9C=E3=81=99=E3=82=8B=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/frontend/src/scripts/aiscript/api.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/frontend/src/scripts/aiscript/api.ts b/packages/frontend/src/scripts/aiscript/api.ts index 46aed49330..ddecd7eea5 100644 --- a/packages/frontend/src/scripts/aiscript/api.ts +++ b/packages/frontend/src/scripts/aiscript/api.ts @@ -5,12 +5,15 @@ import { utils, values } from '@syuilo/aiscript'; import * as Misskey from 'misskey-js'; +import { parse as parseMfm, toString as mfmAstToString } from 'mfm-js'; +import type { MfmNode } from 'mfm-js'; import * as os from '@/os.js'; import { misskeyApi } from '@/scripts/misskey-api.js'; import { $i } from '@/account.js'; import { miLocalStorage } from '@/local-storage.js'; import { customEmojis } from '@/custom-emojis.js'; import { url, lang } from '@@/js/config.js'; +import { deepClone } from '../clone.js'; export function aiScriptReadline(q: string): Promise { return new Promise(ok => { @@ -89,5 +92,31 @@ export function createAiScriptEnv(opts) { utils.assertString(text); return values.STR(Misskey.nyaize(text.value)); }), + 'Mk:replaceMfm': values.FN_NATIVE(async ([text, fn], opts) => { + utils.assertString(text); + utils.assertFunction(fn); + + const ast = parseMfm(text.value); + + async function replaceText(ast: MfmNode[]) { + utils.assertFunction(fn); + + return await Promise.all(ast.map(async (node) => { + const out = deepClone(node); + if (out.type === 'text') { + const res = (await opts.topCall(fn, [values.STR(out.props.text)])); + utils.assertString(res); + out.props.text = res.value; + } else if (out.type !== 'plain' && 'children' in out && out.children != null && out.children.length > 0) { + out.children = await replaceText(out.children); + } + return out; + })); + } + + const afterAst = await replaceText(ast); + + return values.STR(mfmAstToString(afterAst)); + }), }; }