From 44eafd6872866ff1a789646b102c41f03360cde2 Mon Sep 17 00:00:00 2001 From: Charles Simard-Lecours Date: Wed, 9 Nov 2022 17:17:44 -0500 Subject: [PATCH] feat: add config option to disable the non-printable utf8 escaping --- README.md | 4 ++++ src/transform.js | 16 ++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ee452730..988d2591 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,10 @@ export default { // { // lineWidth: -1, // } + + + escapeNonPrintableUnicodeCharacters: true + // If you do not want the parser to convert non printable characters to UTF-8 Character Sequences (ex : \\u00a0), put this to true. } ``` diff --git a/src/transform.js b/src/transform.js index 07ba0c76..007f62c5 100644 --- a/src/transform.js +++ b/src/transform.js @@ -42,6 +42,7 @@ export default class i18nTransform extends Transform { customValueTemplate: null, failOnWarnings: false, yamlOptions: null, + escapeNonPrintableUnicodeCharacters: true } this.options = { ...this.defaults, ...options } @@ -370,12 +371,15 @@ export default class i18nTransform extends Transform { }) } else { text = JSON.stringify(contents, null, this.options.indentation) + '\n' - // Convert non-printable Unicode characters to unicode escape sequence - // https://unicode.org/reports/tr18/#General_Category_Property - text = text.replace(/[\p{Z}\p{Cc}\p{Cf}]/gu, (chr) => { - const n = chr.charCodeAt(0) - return n < 128 ? chr : `\\u${`0000${n.toString(16)}`.substr(-4)}` - }) + + if(this.options.escapeNonPrintableUnicodeCharacters){ + // Convert non-printable Unicode characters to unicode escape sequence + // https://unicode.org/reports/tr18/#General_Category_Property + text = text.replace(/[\p{Z}\p{Cc}\p{Cf}]/gu, (chr) => { + const n = chr.charCodeAt(0) + return n < 128 ? chr : `\\u${`0000${n.toString(16)}`.substr(-4)}` + }) + } } if (this.options.lineEnding === 'auto') {