@sie-js/vkp 1.0.3 → 1.0.4

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/index.js +22 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sie-js/vkp",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Parser and utils for the .VKP files format which is used in the V-Klay.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -13,7 +13,8 @@
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
15
  "iconv-lite": "^0.6.3",
16
- "moo": "^0.5.2"
16
+ "moo": "^0.5.2",
17
+ "rtf-parser": "^1.3.3"
17
18
  },
18
19
  "devDependencies": {
19
20
  "glob": "^10.3.12",
package/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import iconv from 'iconv-lite';
2
2
  import { vkpRawParser } from './parser.js';
3
3
  import { VkpParseError } from './VkpParseError.js';
4
+ import RTFParser from 'rtf-parser';
4
5
 
5
6
  const DEFAULT_PRAGMAS = {
6
7
  warn_no_old_on_apply: true,
@@ -116,6 +117,26 @@ function vkpDetectContent(text) {
116
117
  return "UNKNOWN";
117
118
  }
118
119
 
120
+ // CP1251 -> UTF-8 + CRLF -> LF (with RTC support)
121
+ async function vkpNormalizeWithRTF(text) {
122
+ if (text.indexOf('{\\rtf1') >= 0) {
123
+ let parsed = await new Promise((resolve, reject) => {
124
+ RTFParser.string(text, (err, doc) => {
125
+ if (err) {
126
+ reject(err);
127
+ } else {
128
+ resolve(doc);
129
+ }
130
+ });
131
+ });
132
+ let lines = [];
133
+ for (let p of parsed.content)
134
+ lines.push(p.content.map((s) => s.value).join(''));
135
+ return lines.join('\n');
136
+ }
137
+ return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
138
+ }
139
+
119
140
  // CP1251 -> UTF-8 + CRLF -> LF
120
141
  function vkpNormalize(text) {
121
142
  return iconv.decode(text, 'windows-1251').replace(/(\r\n|\n|\r)/g, "\n");
@@ -126,4 +147,4 @@ function vkpCanonicalize(text) {
126
147
  return iconv.encode(text.replace(/(\r\n|\n|\r)/g, "\r\n"), 'windows-1251');
127
148
  }
128
149
 
129
- export { vkpParse, vkpRawParser, vkpNormalize, vkpCanonicalize, vkpDetectContent };
150
+ export { vkpParse, vkpRawParser, vkpNormalize, vkpNormalizeWithRTF, vkpCanonicalize, vkpDetectContent };