feat: protect placeholder [0] {0}

This commit is contained in:
xxnuo
2026-01-01 17:13:07 +08:00
parent 433d38bd23
commit bb3b0123a2

View File

@@ -123,7 +123,7 @@ export class TranslationEngine {
processedText = this._sanitizeHTML(text);
}
const { cleanText: placeholderText, replacements: placeholderReplacements } = this._hidePlaceholders(processedText);
const { cleanText: placeholderText, replacements: placeholderReplacements } = this._hidePlaceholders(processedText, options);
const { cleanText, replacements } = this._hideEmojis(placeholderText);
let translation: string;
@@ -307,10 +307,13 @@ export class TranslationEngine {
return result;
}
private _hidePlaceholders(text: string): { cleanText: string; replacements: Array<{ original: string; placeholder: string }> } {
private _hidePlaceholders(
text: string,
options: TranslateOptions = {}
): { cleanText: string; replacements: Array<{ original: string; placeholder: string }> } {
const replacements: Array<{ original: string; placeholder: string }> = [];
const used = new Set<string>();
const placeholderRegex = /\{(\d+)\}/g;
const placeholderRegex = /(\{\d+\}|\[\d+\])/g;
const cleanText = text.replace(placeholderRegex, (match) => {
let placeholder = `__MTRAN_PH_${replacements.length}__`;
let salt = 0;