mirror of
https://github.com/misskey-dev/misskey.git
synced 2024-12-28 16:38:23 +01:00
feat(aiscript): MFMを考慮してテキストを操作する関数を追加
This commit is contained in:
parent
04b221409c
commit
dbea4bcd27
1 changed files with 29 additions and 0 deletions
|
@ -5,12 +5,15 @@
|
||||||
|
|
||||||
import { utils, values } from '@syuilo/aiscript';
|
import { utils, values } from '@syuilo/aiscript';
|
||||||
import * as Misskey from 'misskey-js';
|
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 * as os from '@/os.js';
|
||||||
import { misskeyApi } from '@/scripts/misskey-api.js';
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
||||||
import { $i } from '@/account.js';
|
import { $i } from '@/account.js';
|
||||||
import { miLocalStorage } from '@/local-storage.js';
|
import { miLocalStorage } from '@/local-storage.js';
|
||||||
import { customEmojis } from '@/custom-emojis.js';
|
import { customEmojis } from '@/custom-emojis.js';
|
||||||
import { url, lang } from '@@/js/config.js';
|
import { url, lang } from '@@/js/config.js';
|
||||||
|
import { deepClone } from '../clone.js';
|
||||||
|
|
||||||
export function aiScriptReadline(q: string): Promise<string> {
|
export function aiScriptReadline(q: string): Promise<string> {
|
||||||
return new Promise(ok => {
|
return new Promise(ok => {
|
||||||
|
@ -89,5 +92,31 @@ export function createAiScriptEnv(opts) {
|
||||||
utils.assertString(text);
|
utils.assertString(text);
|
||||||
return values.STR(Misskey.nyaize(text.value));
|
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));
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue