[fix] parsing

This commit is contained in:
kwaroran
2023-11-14 14:19:32 +09:00
parent abd6ae0402
commit 4c4b70a039
2 changed files with 58 additions and 14 deletions

View File

@@ -2,7 +2,7 @@
import { ArrowLeft, ArrowRight, EditIcon, LanguagesIcon, RefreshCcwIcon, TrashIcon, CopyIcon } from "lucide-svelte"; import { ArrowLeft, ArrowRight, EditIcon, LanguagesIcon, RefreshCcwIcon, TrashIcon, CopyIcon } from "lucide-svelte";
import { ParseMarkdown, type simpleCharacterArgument } from "../../ts/parser"; import { ParseMarkdown, type simpleCharacterArgument } from "../../ts/parser";
import AutoresizeArea from "../UI/GUI/TextAreaResizable.svelte"; import AutoresizeArea from "../UI/GUI/TextAreaResizable.svelte";
import { alertConfirm } from "../../ts/alert"; import { alertConfirm, alertError } from "../../ts/alert";
import { language } from "../../lang"; import { language } from "../../lang";
import { DataBase, type groupChat } from "../../ts/storage/database"; import { DataBase, type groupChat } from "../../ts/storage/database";
import { CurrentChat, selectedCharID } from "../../ts/stores"; import { CurrentChat, selectedCharID } from "../../ts/stores";
@@ -20,13 +20,16 @@
export let onReroll = () => {} export let onReroll = () => {}
export let unReroll = () => {} export let unReroll = () => {}
export let character:simpleCharacterArgument|string|null = null export let character:simpleCharacterArgument|string|null = null
let translating = (!!$DataBase.autoTranslate) let translating = false
try {
translating = get(DataBase).autoTranslate
} catch (error) {}
let editMode = false let editMode = false
let statusMessage:string = '' let statusMessage:string = ''
export let altGreeting = false export let altGreeting = false
let msgDisplay = '' let msgDisplay = ''
let translated = false let translated = get(DataBase).autoTranslate
async function rm(){ async function rm(){
const rm = $DataBase.askRemoval ? await alertConfirm(language.removeChat) : true const rm = $DataBase.askRemoval ? await alertConfirm(language.removeChat) : true
if(rm){ if(rm){
@@ -55,7 +58,7 @@
$CurrentChat.message = msg $CurrentChat.message = msg
} }
async function displaya(message:string){ function displaya(message:string){
msgDisplay = risuChatParser(message, {chara: name, chatID: idx, rmVar: true}) msgDisplay = risuChatParser(message, {chara: name, chatID: idx, rmVar: true})
} }
@@ -71,14 +74,19 @@
let lastCharArg:string|simpleCharacterArgument = null let lastCharArg:string|simpleCharacterArgument = null
let lastChatId = -10 let lastChatId = -10
const markParsing = async (data: string, charArg?: string | simpleCharacterArgument, mode?: "normal" | "back", chatID?: number, translateText?:boolean) => { const markParsing = async (data: string, charArg?: string | simpleCharacterArgument, mode?: "normal" | "back", chatID?: number, translateText?:boolean, tries?:number) => {
if((!isEqual(lastCharArg, charArg)) || (chatID !== lastChatId)){ try {
if((!isEqual(lastCharArg, charArg)) || (chatID !== lastChatId)){
lastParsed = '' lastParsed = ''
lastCharArg = charArg lastCharArg = charArg
lastChatId = chatID lastChatId = chatID
translating = (!!$DataBase.autoTranslate) translateText = false
translateText = translating try {
translated = false translated = get(DataBase).autoTranslate
if(translated){
translateText = true
}
} catch (error) {}
} }
if(translateText){ if(translateText){
const marked = await ParseMarkdown(data, charArg, mode, chatID) const marked = await ParseMarkdown(data, charArg, mode, chatID)
@@ -95,6 +103,14 @@
lastCharArg = charArg lastCharArg = charArg
return marked return marked
} }
} catch (error) {
//retry
if(tries > 2){
alertError(`Error while parsing chat message: ${error}`)
return data
}
return await markParsing(data, charArg, mode, chatID, translateText, (tries ?? 0) + 1)
}
} }
$: displaya(message) $: displaya(message)

View File

@@ -162,29 +162,57 @@ async function jaTrans(text:string) {
export async function translateHTML(html: string, reverse:boolean): Promise<string> { export async function translateHTML(html: string, reverse:boolean): Promise<string> {
const dom = new DOMParser().parseFromString(html, 'text/html'); const dom = new DOMParser().parseFromString(html, 'text/html');
console.log(html)
let promises: Promise<void>[] = [];
async function translateNodeText(node:Node) {
if(node.textContent.trim().length !== 0){
node.textContent = await translate(node.textContent || '', reverse);
}
}
// Recursive function to translate all text nodes // Recursive function to translate all text nodes
async function translateNode(node: Node): Promise<void> { async function translateNode(node: Node, parent?: Node): Promise<void> {
if (node.nodeType === Node.TEXT_NODE) { if (node.nodeType === Node.TEXT_NODE) {
// Translate the text content of the node // Translate the text content of the node
if(node.textContent){ if(node.textContent && parent){
node.textContent = await translate(node.textContent || '', reverse); const parentName = parent.nodeName.toLowerCase();
if(parentName === 'script' || parentName === 'style'){
return
}
if(promises.length > 10){
await Promise.all(promises)
promises = []
}
promises.push(translateNodeText(node))
} }
} else if(node.nodeType === Node.ELEMENT_NODE) { } else if(node.nodeType === Node.ELEMENT_NODE) {
// Translate child nodes // Translate child nodes
//skip if it's a script or style tag
if(node.nodeName.toLowerCase() === 'script' || node.nodeName.toLowerCase() === 'style'){
return
}
for (const child of Array.from(node.childNodes)) { for (const child of Array.from(node.childNodes)) {
await translateNode(child); await translateNode(child, node);
} }
} }
} }
// Start translation from the body element // Start translation from the body element
await translateNode(dom.body); await translateNode(dom.body);
await Promise.all(promises)
// Serialize the DOM back to HTML // Serialize the DOM back to HTML
const serializer = new XMLSerializer(); const serializer = new XMLSerializer();
const translatedHTML = serializer.serializeToString(dom.body); let translatedHTML = serializer.serializeToString(dom);
// Remove the outer <body> tags
translatedHTML = translatedHTML.replace(/^<body[^>]*>|<\/body>$/g, '');
// console.log(html)
// console.log(translatedHTML)
// Return the translated HTML, excluding the outer <body> tags if needed // Return the translated HTML, excluding the outer <body> tags if needed
return translatedHTML return translatedHTML
} }