From e1eaf08df8ec63ac9f85a6a16b0adab16745b4df Mon Sep 17 00:00:00 2001 From: sub-hub Date: Mon, 6 May 2024 13:30:38 +0900 Subject: [PATCH 1/5] Fix: now combineTranslation will Apply markdown --- src/lang/ko.ts | 1 + src/ts/translator/translator.ts | 8 +++--- src/ts/util.ts | 46 ++++++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/lang/ko.ts b/src/lang/ko.ts index 509b4aa5..8d86369e 100644 --- a/src/lang/ko.ts +++ b/src/lang/ko.ts @@ -437,4 +437,5 @@ export const languageKorean = { modules: "모듈", useAdvancedEditor: "고급 에디터 사용", hanuraiMemory: "하느라이메모리", + combineTranslation: "결합 번역", } \ No newline at end of file diff --git a/src/ts/translator/translator.ts b/src/ts/translator/translator.ts index 3be3050e..2464422a 100644 --- a/src/ts/translator/translator.ts +++ b/src/ts/translator/translator.ts @@ -8,7 +8,7 @@ import { doingChat } from "../process" import type { simpleCharacterArgument } from "../parser" import { selectedCharID } from "../stores" import { getModuleRegexScripts } from "../process/modules" -import { getNodetextWithNewline, sleep } from "../util" +import { getNodetextToSentence, sleep, applyMarkdownToNode } from "../util" import { processScriptFull } from "../process/scripts" let cache={ @@ -318,7 +318,7 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl node.textContent = translated; return; } - + const { data: processedTranslated } = await processScriptFull( alwaysExistChar, translated, @@ -338,6 +338,7 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl ); newNode.innerHTML = processedTranslated; node.parentNode.replaceChild(newNode, node); + applyMarkdownToNode(newNode); } } @@ -374,7 +375,7 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl blacklist.includes(child.nodeName.toLowerCase()) ); if (!hasBlacklistChild && (node as Element)?.getAttribute('translate') !== 'no'){ - const text = getNodetextWithNewline(node); + const text = getNodetextToSentence(node); const sentences = text.split("\n"); if (sentences.length > 1) { // Multiple sentences seperated by
tags @@ -389,6 +390,7 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl } } else { // Single sentence + node.innerHTML = sentences[0]; await translateNodeText(node, true); } return; diff --git a/src/ts/util.ts b/src/ts/util.ts index c3a596f3..1b16d57d 100644 --- a/src/ts/util.ts +++ b/src/ts/util.ts @@ -8,6 +8,16 @@ import { basename } from "@tauri-apps/api/path" import { createBlankChar, getCharImage } from "./characters" import { appWindow } from '@tauri-apps/api/window'; import { isTauri } from "./storage/globalApi" +import { Marked } from "marked" + +const mconverted = new Marked({ + gfm: true, + breaks: true, + silent: true, + tokenizer: { + + } +}) export interface Messagec extends Message{ index: number @@ -525,18 +535,18 @@ export function appendLastPath(url, lastPath) { } /** - * Retrieves the text content of a given Node object, including line breaks represented by
elements. + * Converts the text content of a given Node object, including HTML elements, into a plain text sentence. * * @param {Node} node - The Node object from which the text content will be extracted. - * @returns {string} The text content of the Node, with line breaks represented by newline characters ('\n'). + * @returns {string} The plain text sentence representing the content of the Node object. * * @example * const div = document.createElement('div'); - * div.innerHTML = 'Hello
World'; - * const text = getNodetextWithNewline(div); - * console.log(text); // Output: "Hello\nWorld" + * div.innerHTML = 'Hello
WorldDeleted'; + * const sentence = getNodetextToSentence(div); + * console.log(sentence); // Output: "Hello\nWorld~Deleted~" */ -export function getNodetextWithNewline(node: Node) { +export function getNodetextToSentence(node: Node): string { let result = ''; for (const child of node.childNodes) { if (child.nodeType === Node.TEXT_NODE) { @@ -544,10 +554,32 @@ export function getNodetextWithNewline(node: Node) { } else if (child.nodeType === Node.ELEMENT_NODE) { if (child.nodeName === 'BR') { result += '\n'; + } else if (child.nodeName === 'DEL') { + result += '~' + getNodetextToSentence(child) + '~'; } else { - result += getNodetextWithNewline(child); + result += getNodetextToSentence(child); } } } return result; +} + +export function applyMarkdownToNode(node: Node) { + if (node.nodeType === Node.TEXT_NODE) { + const text = node.textContent; + if (text) { + let markdown = mconverted.parseInline(text); + markdown = mconverted.parseInline(markdown) + if (markdown !== text) { + const span = document.createElement('span'); + span.innerHTML = markdown; + (node as Element)?.replaceWith(span); + return + } + } + } else { + for (const child of node.childNodes) { + applyMarkdownToNode(child); + } + } } \ No newline at end of file From 8f9f075cdbde79073de7dc7e493e687c3e31385e Mon Sep 17 00:00:00 2001 From: sub-hub Date: Mon, 6 May 2024 14:04:00 +0900 Subject: [PATCH 2/5] Fix: Markdown Nodes Inherit inline styles --- src/ts/util.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ts/util.ts b/src/ts/util.ts index 1b16d57d..5f44c7bd 100644 --- a/src/ts/util.ts +++ b/src/ts/util.ts @@ -573,6 +573,12 @@ export function applyMarkdownToNode(node: Node) { if (markdown !== text) { const span = document.createElement('span'); span.innerHTML = markdown; + + // inherit inline style from the parent node + const parentStyle = (node.parentNode as HTMLElement).style; + for(let i=0;i Date: Mon, 6 May 2024 16:41:55 +0900 Subject: [PATCH 3/5] Update util.ts --- src/ts/util.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ts/util.ts b/src/ts/util.ts index 5f44c7bd..d3ef3dc2 100644 --- a/src/ts/util.ts +++ b/src/ts/util.ts @@ -569,7 +569,6 @@ export function applyMarkdownToNode(node: Node) { const text = node.textContent; if (text) { let markdown = mconverted.parseInline(text); - markdown = mconverted.parseInline(markdown) if (markdown !== text) { const span = document.createElement('span'); span.innerHTML = markdown; @@ -588,4 +587,4 @@ export function applyMarkdownToNode(node: Node) { applyMarkdownToNode(child); } } -} \ No newline at end of file +} From 9b3571beeb25e01d26d241cd4c0e0abf0a3160fe Mon Sep 17 00:00:00 2001 From: sub-hub Date: Mon, 6 May 2024 17:10:26 +0900 Subject: [PATCH 4/5] Added markdown null check and reapply markdown in all cases parent null check applyMarkdownToNode --- src/ts/translator/translator.ts | 7 +------ src/ts/util.ts | 10 ++++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/ts/translator/translator.ts b/src/ts/translator/translator.ts index 2464422a..9c8672d7 100644 --- a/src/ts/translator/translator.ts +++ b/src/ts/translator/translator.ts @@ -316,6 +316,7 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl let translated = await translate(node.textContent || "", reverse); if (!reprocessDisplayScript) { node.textContent = translated; + applyMarkdownToNode(node); return; } @@ -325,12 +326,6 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl "editdisplay", chatID ); - - // If the translation is the same, don't replace the node - if (translated == processedTranslated) { - node.textContent = processedTranslated; - return; - } // Replace the old node with the new one const newNode = document.createElement( diff --git a/src/ts/util.ts b/src/ts/util.ts index d3ef3dc2..ca3dd7f0 100644 --- a/src/ts/util.ts +++ b/src/ts/util.ts @@ -574,10 +574,12 @@ export function applyMarkdownToNode(node: Node) { span.innerHTML = markdown; // inherit inline style from the parent node - const parentStyle = (node.parentNode as HTMLElement).style; - for(let i=0;i Date: Mon, 6 May 2024 17:14:27 +0900 Subject: [PATCH 5/5] Fixed a mistake in translateHTML. --- src/ts/translator/translator.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ts/translator/translator.ts b/src/ts/translator/translator.ts index 9c8672d7..aaa1bb25 100644 --- a/src/ts/translator/translator.ts +++ b/src/ts/translator/translator.ts @@ -316,7 +316,6 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl let translated = await translate(node.textContent || "", reverse); if (!reprocessDisplayScript) { node.textContent = translated; - applyMarkdownToNode(node); return; } @@ -326,6 +325,12 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl "editdisplay", chatID ); + // If the translation is the same, don't replace the node + if (translated == processedTranslated) { + node.textContent = processedTranslated; + applyMarkdownToNode(node) + return; + } // Replace the old node with the new one const newNode = document.createElement(