From 51614ea95d02f6416b475f9e3177eb67e269bbde Mon Sep 17 00:00:00 2001 From: Kilian Singer Date: Thu, 21 Aug 2025 09:37:43 +0200 Subject: [PATCH] new lines are now preserved in mzta-menus.js also newlines are normalized to \n in chase \r\n (like on windows platforms) are present. some LLMs work better with proper paragraph breaks. --- js/mzta-menus.js | 18 +++++++++--------- js/mzta-utils.js | 13 ++++++++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/js/mzta-menus.js b/js/mzta-menus.js index 01e94a8d..d11cba0e 100644 --- a/js/mzta-menus.js +++ b/js/mzta-menus.js @@ -19,7 +19,7 @@ // Some original methods are derived from https://github.com/ali-raheem/Aify/blob/cfadf52f576b7be3720b5b73af7c8d3129c054da/plugin/html/actions.js import { getPrompts } from './mzta-prompts.js'; -import { getLanguageDisplayName, getMenuContextCompose, getMenuContextDisplay, i18nConditionalGet, getMailSubject, getTagsList, extractJsonObject, convertNewlinesToBr } from './mzta-utils.js' +import { getLanguageDisplayName, getMenuContextCompose, getMenuContextDisplay, i18nConditionalGet, getMailSubject, getTagsList, extractJsonObject, convertNewlinesToBr, cleanupNewlines } from './mzta-utils.js' import { taPromptUtils } from './mzta-utils-prompt.js'; import { taLogger } from './mzta-logger.js'; import { placeholdersUtils } from './mzta-placeholders.js'; @@ -81,11 +81,11 @@ export class mzta_Menus { const getMailBody = async (tabs, do_autoselect = false) => { //const tabs = await browser.tabs.query({ active: true, currentWindow: true }); return {tabId: tabs[0].id, - selection: convertNewlinesToBr(await browser.tabs.sendMessage(tabs[0].id, { command: "getSelectedText" })), + selection: cleanupNewlines(await browser.tabs.sendMessage(tabs[0].id, { command: "getSelectedText" })), selection_html: convertNewlinesToBr(await browser.tabs.sendMessage(tabs[0].id, { command: "getSelectedHtml" })), - text: convertNewlinesToBr(await browser.tabs.sendMessage(tabs[0].id, { command: "getTextOnly" })), + text: "hello\n" + cleanupNewlines(await browser.tabs.sendMessage(tabs[0].id, { command: "getTextOnly" })), html: convertNewlinesToBr(await browser.tabs.sendMessage(tabs[0].id, { command: "getFullHtml" })), - only_typed_text: convertNewlinesToBr(await browser.tabs.sendMessage(tabs[0].id, { command: "getOnlyTypedText", do_autoselect: do_autoselect })), + only_typed_text: cleanupNewlines(await browser.tabs.sendMessage(tabs[0].id, { command: "getOnlyTypedText", do_autoselect: do_autoselect })), only_quoted_text: convertNewlinesToBr(await browser.tabs.sendMessage(tabs[0].id, { command: "getOnlyQuotedText" })) }; }; @@ -109,17 +109,17 @@ export class mzta_Menus { let selection_html = msg_text.selection_html; let only_typed_text = ''; let only_quoted_text = ''; - only_typed_text = msg_text.only_typed_text.replace(/\s+/g, ' ').trim(); - selection_text = msg_text.selection.replace(/\s+/g, ' ').trim(); + only_typed_text = msg_text.only_typed_text.replace(/[ \t]+/g, ' ').trim(); + selection_text = msg_text.selection.replace(/[ \t]+/g, ' ').trim(); if(selection_text === ''){ if(placeholdersUtils.hasPlaceholder(curr_prompt.text, "mail_typed_text")){ selection_text = only_typed_text; } } - only_quoted_text = msg_text.only_quoted_text.replace(/\s+/g, ' ').trim(); + only_quoted_text = msg_text.only_quoted_text.replace(/[ \t]+/g, ' ').trim(); curr_prompt.selection_text = selection_text; curr_prompt.selection_html = selection_html; - body_text = msg_text.text.replace(/\s+/g, ' ').trim(); + body_text = msg_text.text.replace(/[ \t]+/g, ' ').trim(); curr_prompt.body_text = body_text; //open chatgpt window //console.log("Click menu item..."); @@ -143,7 +143,7 @@ export class mzta_Menus { curr_message = curr_messages; break; } - + fullPrompt = await taPromptUtils.preparePrompt(curr_prompt, curr_message, chatgpt_lang, selection_text, selection_html, body_text, await getMailSubject(tabs[0]), msg_text, only_typed_text, only_quoted_text, tags_full_list); switch(curr_prompt.id){ diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 323f87c4..b58070f0 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -213,15 +213,26 @@ export function htmlBodyToPlainText(htmlString) { const textContent = doc.body.textContent || ""; // Trim whitespace return textContent + .replace(/\r\n/g, '\n') .replace(/[ \t]+\n/g, '\n') .replace(/\n{2,}/g, '\n') .replace(/[ \t]+/g, ' ') .replace(/ /gi,"") .trim(); } + +export function cleanupNewlines(text) { + return text + .replace(/\r\n/g, '\n') + .replace(/[ \t]+\n/g, '\n') + .replace(/\n{2,}/g, '\n') + .replace(/[ \t]+/g, ' ') + .replace(/ /gi,' ') + .trim(); +} export function convertNewlinesToBr(text) { - return text.replace(/\n/g, '
'); + return text.replace(/\r\n/g, '\n').replace(/\n/g, '
'); } function convertBrToNewlines(html) {