@collectionscms/plugin-text-generator
Advanced tools
Comparing version 1.2.4 to 1.2.5
{ | ||
"name": "@collectionscms/plugin-text-generator", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "Text Generation Plugin for Collections.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -36,14 +36,21 @@ import * as deepl from "deepl-node"; | ||
codePlaceholders.push(codeContent); | ||
return `<code>${codePlaceholders.length - 1}</code>`; | ||
return `<code>P_${codePlaceholders.length - 1}</code>`; | ||
}); | ||
// Replace the paragraph with a placeholder | ||
// Extract paragraphs | ||
const paragraphPlaceholders = []; | ||
bodyWithPlaceholders = bodyWithPlaceholders.replace(/<p>([\s\S]*?)<\/p>/g, (_, codeContent) => { | ||
paragraphPlaceholders.push(codeContent); | ||
return `<p>${paragraphPlaceholders.length - 1}</p>`; | ||
bodyWithPlaceholders.match(/<p>([\s\S]*?)<\/p>/g)?.forEach((paragraph) => { | ||
const content = paragraph.replace(/<\/?p>/g, "").trim(); | ||
if (content) { | ||
paragraphPlaceholders.push(content); | ||
} | ||
}); | ||
const translatedTexts = await this.translator.translateText([title, subtitle, bodyWithPlaceholders].filter((text) => text.trim() !== ""), sourceLang, targetLang, { | ||
preserveFormatting: true, | ||
tagHandling: "html", | ||
}); | ||
// Translate the text | ||
const [translatedTexts, translatedParagraphs] = await Promise.all([ | ||
this.translator.translateText([title, subtitle, bodyWithPlaceholders].filter((text) => text.trim() !== ""), sourceLang, targetLang, { | ||
preserveFormatting: true, | ||
tagHandling: "html", | ||
ignoreTags: ["code"], | ||
}), | ||
this.translateParagraphByOpenAI(targetLangEnglishName, paragraphPlaceholders), | ||
]); | ||
// Title | ||
@@ -56,11 +63,27 @@ const translatedTitle = title ? translatedTexts[0].text : ""; | ||
// Body | ||
// Restore the original content | ||
let translatedBody = translatedTexts[translatedTexts.length - 1].text; | ||
translatedBody = translatedBody.replace(/<code>(\d+)<\/code>/g, (_, index) => { | ||
// Restore the original code block | ||
translatedBody = translatedBody.replace(/<code>P_(\d+)<\/code>/g, (_, index) => { | ||
return `<code>${codePlaceholders[parseInt(index, 10)]}</code>`; | ||
}); | ||
const translatedParagraphs = await this.translateParagraphByOpenAI(targetLangEnglishName, paragraphPlaceholders); | ||
translatedBody = translatedBody.replace(/<p>(\d+)<\/p>/g, (_, index) => { | ||
return `<p>${translatedParagraphs[parseInt(index, 10)]}</p>`; | ||
}); | ||
// Replace with paragraphs translated by openAI | ||
if (paragraphPlaceholders.length === translatedParagraphs.length) { | ||
let replaceCount = 0; | ||
let isReplaceSuccessful = true; | ||
const replacedBody = translatedBody.replace(/<p>([\s\S]*?)<\/p>/g, (_, paragraph) => { | ||
const content = paragraph.replace(/<\/?p>/g, "").trim(); | ||
if (!content) | ||
return "<p></p>"; | ||
const translatedParagraph = translatedParagraphs[replaceCount]; | ||
if (!translatedParagraph) { | ||
isReplaceSuccessful = false; | ||
return `<p>${paragraph}</p>`; | ||
} | ||
replaceCount++; | ||
return `<p>${translatedParagraph}</p>`; | ||
}); | ||
if (isReplaceSuccessful) { | ||
translatedBody = replacedBody; | ||
} | ||
} | ||
return { | ||
@@ -67,0 +90,0 @@ title: translatedTitle, |
/* eslint-disable max-len */ | ||
import { jest } from "@jest/globals"; | ||
import "dotenv/config"; | ||
import { Translator } from "./translator.js"; | ||
jest.setTimeout(10000); | ||
describe("Translator", () => { | ||
@@ -73,9 +75,23 @@ const translatorApiKey = process.env.TRANSLATOR_API_KEY; | ||
describe.only("rewrite variation", () => { | ||
it("should be rewritten paragraph", async () => { | ||
it("should be rewritten paragraph from Japanese to English", async () => { | ||
const result = await translator.translate({ | ||
title: "", | ||
subtitle: "", | ||
body: "<h2><strong>はじめに</strong></h2><p>Web アプリケーション開発において、ユーザー体験を向上させる重要な機能の 1 つに、ドラッグ&ドロップが挙げられます。</p><p></p><p>シンプルなマウス操作で要素を移動したり、ファイルをアップロードしたり、<br>直感的な操作性を実現することができます。</p><p></p><p>今回は、<strong>HTML Drag and Drop API について調査した</strong>ので、基礎的な内容をまとめました!<br>時間の節約になれば、嬉しいです 🙌</p>", | ||
sourceLang: "ja", | ||
targetLang: "en-US", | ||
targetLangEnglishName: "English", | ||
}); | ||
console.log(`result`, result); | ||
expect(result.title).toBe(""); | ||
expect(result.subtitle).toBe(""); | ||
expect(result.body).toBeTruthy(); | ||
}); | ||
it("should be rewritten paragraph from English to Japanese", async () => { | ||
const result = await translator.translate({ | ||
title: "", | ||
subtitle: "", | ||
body: `<p>ChatGPT can now search the web in a much better way than before. You can get fast, timely answers with links to relevant web sources, which you would have previously needed to go to a search engine for. This blends the benefits of a natural language interface with the value of up-to-date sports scores, news, stock quotes, and more.</p><p>Go straight to<br/>the source</p><pre><code># Mocked approach | ||
it 'creates a user (mocked)' do | ||
end</code><p>For more information, <a href="https://openai.com/index/introducing-chatgpt-search/">click here.</a></p>`, | ||
it 'creates a user (mocked)' do | ||
end</code><p>For more information, <a href="https://openai.com/index/introducing-chatgpt-search/">click here.</a></p>`, | ||
sourceLang: "en", | ||
@@ -82,0 +98,0 @@ targetLang: "ja", |
15065
327