@lobehub/editor 4.9.0 → 4.9.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/editor",
3
- "version": "4.9.0",
3
+ "version": "4.9.1",
4
4
  "description": "A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.",
5
5
  "keywords": [
6
6
  "lobehub",
@@ -31,8 +31,39 @@ function sha256(content) {
31
31
  return crypto.createHash('sha256').update(content).digest('hex');
32
32
  }
33
33
 
34
+ function normalizeLineEndings(text) {
35
+ return text.replaceAll(/\r\n?/g, '\n');
36
+ }
37
+
38
+ function detectLineEnding(text) {
39
+ const crlfCount = (text.match(/\r\n/g) || []).length;
40
+ const lfCount = (text.match(/\n/g) || []).length - crlfCount;
41
+
42
+ return crlfCount > lfCount ? '\r\n' : '\n';
43
+ }
44
+
45
+ function restoreLineEndings(text, lineEnding) {
46
+ return lineEnding === '\r\n' ? text.replaceAll('\n', '\r\n') : text;
47
+ }
48
+
49
+ function getContentHashState(content, hashes) {
50
+ const currentHash = sha256(content);
51
+ const normalizedContent = normalizeLineEndings(content);
52
+ const normalizedHash = normalizedContent === content ? currentHash : sha256(normalizedContent);
53
+
54
+ if (normalizedHash === hashes.patched) {
55
+ return { currentHash, normalizedContent, normalizedHash, status: 'patched' };
56
+ }
57
+
58
+ if (normalizedHash === hashes.original) {
59
+ return { currentHash, normalizedContent, normalizedHash, status: 'original' };
60
+ }
61
+
62
+ return { currentHash, normalizedContent, normalizedHash, status: 'unknown' };
63
+ }
64
+
34
65
  function splitLines(text) {
35
- return text.split('\n');
66
+ return normalizeLineEndings(text).split('\n');
36
67
  }
37
68
 
38
69
  function parsePatch(patchText) {
@@ -177,9 +208,9 @@ function patchLexical() {
177
208
  }
178
209
 
179
210
  const currentContent = fs.readFileSync(targetPath, 'utf8');
180
- const currentHash = sha256(currentContent);
211
+ const currentState = getContentHashState(currentContent, hashes);
181
212
 
182
- if (currentHash === hashes.patched) {
213
+ if (currentState.status === 'patched') {
183
214
  continue;
184
215
  }
185
216
 
@@ -189,13 +220,18 @@ function patchLexical() {
189
220
  throw new Error(`[lobe-editor] Missing patch entry for ${filename}`);
190
221
  }
191
222
 
192
- if (currentHash !== hashes.original) {
223
+ if (currentState.status !== 'original') {
224
+ const normalizedHashMessage =
225
+ currentState.currentHash === currentState.normalizedHash
226
+ ? ''
227
+ : `; normalized content hash ${currentState.normalizedHash}`;
228
+
193
229
  throw new Error(
194
- `[lobe-editor] Refuse to patch ${filename}: unknown content hash ${currentHash}.`,
230
+ `[lobe-editor] Refuse to patch ${filename}: unknown content hash ${currentState.currentHash}${normalizedHashMessage}.`,
195
231
  );
196
232
  }
197
233
 
198
- const patchedContent = applyPatchToContent(currentContent, filePatch);
234
+ const patchedContent = applyPatchToContent(currentState.normalizedContent, filePatch);
199
235
  const patchedHash = sha256(patchedContent);
200
236
 
201
237
  if (patchedHash !== hashes.patched) {
@@ -204,7 +240,10 @@ function patchLexical() {
204
240
  );
205
241
  }
206
242
 
207
- fs.writeFileSync(targetPath, patchedContent);
243
+ fs.writeFileSync(
244
+ targetPath,
245
+ restoreLineEndings(patchedContent, detectLineEnding(currentContent)),
246
+ );
208
247
  patchedFiles.push(filename);
209
248
  }
210
249