[feat] add translate replace regex

This commit is contained in:
kwaroran
2024-01-04 03:37:05 +09:00
parent 5f2275eb22
commit 4f77504d3f
4 changed files with 29 additions and 3 deletions

View File

@@ -1,10 +1,12 @@
import { get } from "svelte/store"
import { translatorPlugin } from "../plugins/plugins"
import { DataBase } from "../storage/database"
import { DataBase, type customscript } from "../storage/database"
import { globalFetch } from "../storage/globalApi"
import { alertError } from "../alert"
import { requestChatData } from "../process/request"
import { doingChat } from "../process"
import type { simpleCharacterArgument } from "../parser"
import { selectedCharID } from "../stores"
let cache={
origin: [''],
@@ -171,7 +173,7 @@ export function isExpTranslator(){
return db.translatorType === 'llm' || db.translatorType === 'deepl'
}
export async function translateHTML(html: string, reverse:boolean): Promise<string> {
export async function translateHTML(html: string, reverse:boolean, charArg:simpleCharacterArgument|string = ''): Promise<string> {
let db = get(DataBase)
let DoingChat = get(doingChat)
if(DoingChat){
@@ -233,6 +235,28 @@ export async function translateHTML(html: string, reverse:boolean): Promise<stri
// Remove the outer <body> tags
translatedHTML = translatedHTML.replace(/^<body[^>]*>|<\/body>$/g, '');
if(charArg !== ''){
let scripts:customscript[] = []
if(typeof(charArg) === 'string'){
const db = get(DataBase)
const charId = get(selectedCharID)
const char = db.characters[charId]
scripts = (db.globalscript ?? []).concat(char.customscript)
}
else{
scripts = (db.globalscript ?? []).concat(charArg.customscript)
}
for(const script of scripts){
if(script.type === 'edittrans'){
const reg = new RegExp(script.in, script.ableFlag ? script.flag : 'g')
let outScript = script.out.replaceAll("$n", "\n")
translatedHTML = translatedHTML.replace(reg, outScript)
}
}
}
// console.log(html)
// console.log(translatedHTML)
// Return the translated HTML, excluding the outer <body> tags if needed