@sie-js/vkp 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +23 -2
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -117,9 +117,26 @@ function vkpDetectContent(text) {
|
|
|
117
117
|
return "UNKNOWN";
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
// CP1251 -> UTF-8 + CRLF -> LF (with
|
|
120
|
+
// CP1251 -> UTF-8 + CRLF -> LF (with RTF support)
|
|
121
121
|
async function vkpNormalizeWithRTF(text) {
|
|
122
|
+
if (!Buffer.isBuffer(text))
|
|
123
|
+
throw new Error(`Patch text is not Buffer!`);
|
|
124
|
+
|
|
122
125
|
if (text.indexOf('{\\rtf1') >= 0) {
|
|
126
|
+
// Strip RTF images
|
|
127
|
+
while (true) {
|
|
128
|
+
let pictureIndex = text.indexOf('{\\pict');
|
|
129
|
+
if (pictureIndex >= 0) {
|
|
130
|
+
let pictureEndIndex = text.indexOf('}', pictureIndex);
|
|
131
|
+
if (pictureIndex >= 0) {
|
|
132
|
+
text = Buffer.concat([ text.slice(0, pictureIndex), text.slice(pictureEndIndex + 1) ]);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
text = text.toString('utf-8').replace(/{\\pict.*?\}/gsi, ''); // remove pictures
|
|
123
140
|
let parsed = await new Promise((resolve, reject) => {
|
|
124
141
|
RTFParser.string(text, (err, doc) => {
|
|
125
142
|
if (err) {
|
|
@@ -129,9 +146,11 @@ async function vkpNormalizeWithRTF(text) {
|
|
|
129
146
|
}
|
|
130
147
|
});
|
|
131
148
|
});
|
|
149
|
+
|
|
132
150
|
let lines = [];
|
|
133
|
-
for (let p of parsed.content)
|
|
151
|
+
for (let p of parsed.content) {
|
|
134
152
|
lines.push(p.content.map((s) => s.value).join(''));
|
|
153
|
+
}
|
|
135
154
|
return lines.join('\n');
|
|
136
155
|
}
|
|
137
156
|
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
@@ -139,6 +158,8 @@ async function vkpNormalizeWithRTF(text) {
|
|
|
139
158
|
|
|
140
159
|
// CP1251 -> UTF-8 + CRLF -> LF
|
|
141
160
|
function vkpNormalize(text) {
|
|
161
|
+
if (!Buffer.isBuffer(text))
|
|
162
|
+
throw new Error(`Patch text is not Buffer!`);
|
|
142
163
|
return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
|
|
143
164
|
}
|
|
144
165
|
|