@intlayer/cli 6.1.6 → 7.0.0-canary.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/dist/cjs/IntlayerEventListener.cjs +187 -239
- package/dist/cjs/IntlayerEventListener.cjs.map +1 -1
- package/dist/cjs/_virtual/_utils_asset.cjs +104 -0
- package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/cjs/build.cjs +23 -42
- package/dist/cjs/build.cjs.map +1 -1
- package/dist/cjs/cli.cjs +370 -431
- package/dist/cjs/cli.cjs.map +1 -1
- package/dist/cjs/config.cjs +10 -35
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/editor.cjs +51 -66
- package/dist/cjs/editor.cjs.map +1 -1
- package/dist/cjs/fill/fill.cjs +73 -301
- package/dist/cjs/fill/fill.cjs.map +1 -1
- package/dist/cjs/fill/formatAutoFillData.cjs +48 -106
- package/dist/cjs/fill/formatAutoFillData.cjs.map +1 -1
- package/dist/cjs/fill/formatAutoFilledFilePath.cjs +27 -43
- package/dist/cjs/fill/formatAutoFilledFilePath.cjs.map +1 -1
- package/dist/cjs/fill/formatFillData.cjs +50 -0
- package/dist/cjs/fill/formatFillData.cjs.map +1 -0
- package/dist/cjs/fill/groupLimiter.cjs +42 -0
- package/dist/cjs/fill/groupLimiter.cjs.map +1 -0
- package/dist/cjs/fill/index.cjs +5 -25
- package/dist/cjs/fill/listTranslationsTasks.cjs +77 -0
- package/dist/cjs/fill/listTranslationsTasks.cjs.map +1 -0
- package/dist/cjs/fill/test-original-case.cjs +10 -0
- package/dist/cjs/fill/test-original-case.cjs.map +1 -0
- package/dist/cjs/fill/translateDictionary.cjs +148 -0
- package/dist/cjs/fill/translateDictionary.cjs.map +1 -0
- package/dist/cjs/fill/writeAutoFill.cjs +48 -0
- package/dist/cjs/fill/writeAutoFill.cjs.map +1 -0
- package/dist/cjs/fill/writeFill.cjs +50 -0
- package/dist/cjs/fill/writeFill.cjs.map +1 -0
- package/dist/cjs/getTargetDictionary.cjs +36 -76
- package/dist/cjs/getTargetDictionary.cjs.map +1 -1
- package/dist/cjs/index.cjs +31 -45
- package/dist/cjs/listContentDeclaration.cjs +36 -64
- package/dist/cjs/listContentDeclaration.cjs.map +1 -1
- package/dist/cjs/liveSync.cjs +146 -221
- package/dist/cjs/liveSync.cjs.map +1 -1
- package/dist/cjs/pull.cjs +132 -195
- package/dist/cjs/pull.cjs.map +1 -1
- package/dist/cjs/push/pullLog.cjs +101 -144
- package/dist/cjs/push/pullLog.cjs.map +1 -1
- package/dist/cjs/push/push.cjs +159 -249
- package/dist/cjs/push/push.cjs.map +1 -1
- package/dist/cjs/pushConfig.cjs +18 -45
- package/dist/cjs/pushConfig.cjs.map +1 -1
- package/dist/cjs/pushLog.cjs +87 -128
- package/dist/cjs/pushLog.cjs.map +1 -1
- package/dist/cjs/reviewDoc.cjs +183 -291
- package/dist/cjs/reviewDoc.cjs.map +1 -1
- package/dist/cjs/test/index.cjs +52 -91
- package/dist/cjs/test/index.cjs.map +1 -1
- package/dist/cjs/test/listMissingTranslations.cjs +35 -62
- package/dist/cjs/test/listMissingTranslations.cjs.map +1 -1
- package/dist/cjs/translateDoc.cjs +127 -221
- package/dist/cjs/translateDoc.cjs.map +1 -1
- package/dist/cjs/utils/calculateChunks.cjs +85 -115
- package/dist/cjs/utils/calculateChunks.cjs.map +1 -1
- package/dist/cjs/utils/checkAccess.cjs +33 -72
- package/dist/cjs/utils/checkAccess.cjs.map +1 -1
- package/dist/cjs/utils/checkFileModifiedRange.cjs +75 -91
- package/dist/cjs/utils/checkFileModifiedRange.cjs.map +1 -1
- package/dist/cjs/utils/checkLastUpdateTime.cjs +17 -30
- package/dist/cjs/utils/checkLastUpdateTime.cjs.map +1 -1
- package/dist/cjs/utils/chunkInference.cjs +28 -47
- package/dist/cjs/utils/chunkInference.cjs.map +1 -1
- package/dist/cjs/utils/fixChunkStartEndChars.cjs +23 -42
- package/dist/cjs/utils/fixChunkStartEndChars.cjs.map +1 -1
- package/dist/cjs/utils/formatTimeDiff.cjs +17 -43
- package/dist/cjs/utils/formatTimeDiff.cjs.map +1 -1
- package/dist/cjs/utils/getIsFileUpdatedRecently.cjs +13 -32
- package/dist/cjs/utils/getIsFileUpdatedRecently.cjs.map +1 -1
- package/dist/cjs/utils/getOutputFilePath.cjs +71 -86
- package/dist/cjs/utils/getOutputFilePath.cjs.map +1 -1
- package/dist/cjs/utils/getParentPackageJSON.cjs +19 -44
- package/dist/cjs/utils/getParentPackageJSON.cjs.map +1 -1
- package/dist/cjs/utils/listSpecialChars.cjs +49 -73
- package/dist/cjs/utils/listSpecialChars.cjs.map +1 -1
- package/dist/cjs/utils/mapChunksBetweenFiles.cjs +90 -132
- package/dist/cjs/utils/mapChunksBetweenFiles.cjs.map +1 -1
- package/dist/cjs/utils/reorderParagraphs.cjs +86 -118
- package/dist/cjs/utils/reorderParagraphs.cjs.map +1 -1
- package/dist/cjs/watch.cjs +21 -39
- package/dist/cjs/watch.cjs.map +1 -1
- package/dist/esm/IntlayerEventListener.mjs +178 -201
- package/dist/esm/IntlayerEventListener.mjs.map +1 -1
- package/dist/esm/_virtual/_utils_asset.mjs +100 -0
- package/dist/esm/_virtual/rolldown_runtime.mjs +8 -0
- package/dist/esm/build.mjs +20 -23
- package/dist/esm/build.mjs.map +1 -1
- package/dist/esm/cli.mjs +349 -380
- package/dist/esm/cli.mjs.map +1 -1
- package/dist/esm/config.mjs +8 -14
- package/dist/esm/config.mjs.map +1 -1
- package/dist/esm/editor.mjs +47 -41
- package/dist/esm/editor.mjs.map +1 -1
- package/dist/esm/fill/fill.mjs +68 -300
- package/dist/esm/fill/fill.mjs.map +1 -1
- package/dist/esm/fill/formatAutoFillData.mjs +47 -81
- package/dist/esm/fill/formatAutoFillData.mjs.map +1 -1
- package/dist/esm/fill/formatAutoFilledFilePath.mjs +25 -19
- package/dist/esm/fill/formatAutoFilledFilePath.mjs.map +1 -1
- package/dist/esm/fill/formatFillData.mjs +50 -0
- package/dist/esm/fill/formatFillData.mjs.map +1 -0
- package/dist/esm/fill/groupLimiter.mjs +40 -0
- package/dist/esm/fill/groupLimiter.mjs.map +1 -0
- package/dist/esm/fill/index.mjs +4 -3
- package/dist/esm/fill/listTranslationsTasks.mjs +70 -0
- package/dist/esm/fill/listTranslationsTasks.mjs.map +1 -0
- package/dist/esm/fill/test-original-case.mjs +10 -0
- package/dist/esm/fill/test-original-case.mjs.map +1 -0
- package/dist/esm/fill/translateDictionary.mjs +141 -0
- package/dist/esm/fill/translateDictionary.mjs.map +1 -0
- package/dist/esm/fill/writeAutoFill.mjs +44 -0
- package/dist/esm/fill/writeAutoFill.mjs.map +1 -0
- package/dist/esm/fill/writeFill.mjs +45 -0
- package/dist/esm/fill/writeFill.mjs.map +1 -0
- package/dist/esm/getTargetDictionary.mjs +27 -48
- package/dist/esm/getTargetDictionary.mjs.map +1 -1
- package/dist/esm/index.mjs +15 -13
- package/dist/esm/listContentDeclaration.mjs +28 -43
- package/dist/esm/listContentDeclaration.mjs.map +1 -1
- package/dist/esm/liveSync.mjs +135 -187
- package/dist/esm/liveSync.mjs.map +1 -1
- package/dist/esm/pull.mjs +125 -178
- package/dist/esm/pull.mjs.map +1 -1
- package/dist/esm/push/pullLog.mjs +99 -125
- package/dist/esm/push/pullLog.mjs.map +1 -1
- package/dist/esm/push/push.mjs +149 -221
- package/dist/esm/push/push.mjs.map +1 -1
- package/dist/esm/pushConfig.mjs +14 -23
- package/dist/esm/pushConfig.mjs.map +1 -1
- package/dist/esm/pushLog.mjs +85 -109
- package/dist/esm/pushLog.mjs.map +1 -1
- package/dist/esm/reviewDoc.mjs +167 -264
- package/dist/esm/reviewDoc.mjs.map +1 -1
- package/dist/esm/test/index.mjs +47 -73
- package/dist/esm/test/index.mjs.map +1 -1
- package/dist/esm/test/listMissingTranslations.mjs +30 -41
- package/dist/esm/test/listMissingTranslations.mjs.map +1 -1
- package/dist/esm/translateDoc.mjs +114 -193
- package/dist/esm/translateDoc.mjs.map +1 -1
- package/dist/esm/utils/calculateChunks.mjs +83 -91
- package/dist/esm/utils/calculateChunks.mjs.map +1 -1
- package/dist/esm/utils/checkAccess.mjs +28 -46
- package/dist/esm/utils/checkAccess.mjs.map +1 -1
- package/dist/esm/utils/checkFileModifiedRange.mjs +73 -65
- package/dist/esm/utils/checkFileModifiedRange.mjs.map +1 -1
- package/dist/esm/utils/checkLastUpdateTime.mjs +15 -6
- package/dist/esm/utils/checkLastUpdateTime.mjs.map +1 -1
- package/dist/esm/utils/chunkInference.mjs +24 -24
- package/dist/esm/utils/chunkInference.mjs.map +1 -1
- package/dist/esm/utils/fixChunkStartEndChars.mjs +22 -18
- package/dist/esm/utils/fixChunkStartEndChars.mjs.map +1 -1
- package/dist/esm/utils/formatTimeDiff.mjs +16 -19
- package/dist/esm/utils/formatTimeDiff.mjs.map +1 -1
- package/dist/esm/utils/getIsFileUpdatedRecently.mjs +11 -8
- package/dist/esm/utils/getIsFileUpdatedRecently.mjs.map +1 -1
- package/dist/esm/utils/getOutputFilePath.mjs +70 -62
- package/dist/esm/utils/getOutputFilePath.mjs.map +1 -1
- package/dist/esm/utils/getParentPackageJSON.mjs +16 -20
- package/dist/esm/utils/getParentPackageJSON.mjs.map +1 -1
- package/dist/esm/utils/listSpecialChars.mjs +48 -49
- package/dist/esm/utils/listSpecialChars.mjs.map +1 -1
- package/dist/esm/utils/mapChunksBetweenFiles.mjs +87 -107
- package/dist/esm/utils/mapChunksBetweenFiles.mjs.map +1 -1
- package/dist/esm/utils/reorderParagraphs.mjs +85 -93
- package/dist/esm/utils/reorderParagraphs.mjs.map +1 -1
- package/dist/esm/watch.mjs +17 -17
- package/dist/esm/watch.mjs.map +1 -1
- package/dist/types/IntlayerEventListener.d.ts +63 -59
- package/dist/types/IntlayerEventListener.d.ts.map +1 -1
- package/dist/types/build.d.ts +10 -7
- package/dist/types/build.d.ts.map +1 -1
- package/dist/types/cli.d.ts +13 -10
- package/dist/types/cli.d.ts.map +1 -1
- package/dist/types/config.d.ts +7 -4
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/editor.d.ts +6 -4
- package/dist/types/editor.d.ts.map +1 -1
- package/dist/types/fill/fill.d.ts +20 -14
- package/dist/types/fill/fill.d.ts.map +1 -1
- package/dist/types/fill/formatAutoFillData.d.ts +10 -7
- package/dist/types/fill/formatAutoFillData.d.ts.map +1 -1
- package/dist/types/fill/formatAutoFilledFilePath.d.ts +6 -2
- package/dist/types/fill/formatAutoFilledFilePath.d.ts.map +1 -1
- package/dist/types/fill/formatFillData.d.ts +12 -0
- package/dist/types/fill/formatFillData.d.ts.map +1 -0
- package/dist/types/fill/groupLimiter.d.ts +15 -0
- package/dist/types/fill/groupLimiter.d.ts.map +1 -0
- package/dist/types/fill/index.d.ts +3 -3
- package/dist/types/fill/listTranslationsTasks.d.ts +15 -0
- package/dist/types/fill/listTranslationsTasks.d.ts.map +1 -0
- package/dist/types/fill/test-original-case.d.ts +1 -0
- package/dist/types/fill/translateDictionary.d.ts +19 -0
- package/dist/types/fill/translateDictionary.d.ts.map +1 -0
- package/dist/types/fill/writeAutoFill.d.ts +7 -0
- package/dist/types/fill/writeAutoFill.d.ts.map +1 -0
- package/dist/types/fill/writeFill.d.ts +7 -0
- package/dist/types/fill/writeFill.d.ts.map +1 -0
- package/dist/types/getTargetDictionary.d.ts +17 -13
- package/dist/types/getTargetDictionary.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -14
- package/dist/types/listContentDeclaration.d.ts +10 -7
- package/dist/types/listContentDeclaration.d.ts.map +1 -1
- package/dist/types/liveSync.d.ts +8 -3
- package/dist/types/liveSync.d.ts.map +1 -1
- package/dist/types/pull.d.ts +9 -6
- package/dist/types/pull.d.ts.map +1 -1
- package/dist/types/push/pullLog.d.ts +25 -21
- package/dist/types/push/pullLog.d.ts.map +1 -1
- package/dist/types/push/push.d.ts +13 -9
- package/dist/types/push/push.d.ts.map +1 -1
- package/dist/types/pushConfig.d.ts +7 -4
- package/dist/types/pushConfig.d.ts.map +1 -1
- package/dist/types/pushLog.d.ts +23 -20
- package/dist/types/pushLog.d.ts.map +1 -1
- package/dist/types/reviewDoc.d.ts +34 -17
- package/dist/types/reviewDoc.d.ts.map +1 -1
- package/dist/types/test/index.d.ts +9 -5
- package/dist/types/test/index.d.ts.map +1 -1
- package/dist/types/test/listMissingTranslations.d.ts +14 -10
- package/dist/types/test/listMissingTranslations.d.ts.map +1 -1
- package/dist/types/translateDoc.d.ts +34 -17
- package/dist/types/translateDoc.d.ts.map +1 -1
- package/dist/types/utils/calculateChunks.d.ts +10 -7
- package/dist/types/utils/calculateChunks.d.ts.map +1 -1
- package/dist/types/utils/checkAccess.d.ts +8 -4
- package/dist/types/utils/checkAccess.d.ts.map +1 -1
- package/dist/types/utils/checkFileModifiedRange.d.ts +8 -6
- package/dist/types/utils/checkFileModifiedRange.d.ts.map +1 -1
- package/dist/types/utils/checkLastUpdateTime.d.ts +4 -1
- package/dist/types/utils/checkLastUpdateTime.d.ts.map +1 -1
- package/dist/types/utils/chunkInference.d.ts +9 -6
- package/dist/types/utils/chunkInference.d.ts.map +1 -1
- package/dist/types/utils/fixChunkStartEndChars.d.ts +4 -1
- package/dist/types/utils/fixChunkStartEndChars.d.ts.map +1 -1
- package/dist/types/utils/formatTimeDiff.d.ts +4 -1
- package/dist/types/utils/formatTimeDiff.d.ts.map +1 -1
- package/dist/types/utils/getIsFileUpdatedRecently.d.ts +4 -1
- package/dist/types/utils/getIsFileUpdatedRecently.d.ts.map +1 -1
- package/dist/types/utils/getOutputFilePath.d.ts +7 -2
- package/dist/types/utils/getOutputFilePath.d.ts.map +1 -1
- package/dist/types/utils/getParentPackageJSON.d.ts +30 -28
- package/dist/types/utils/getParentPackageJSON.d.ts.map +1 -1
- package/dist/types/utils/listSpecialChars.d.ts +9 -7
- package/dist/types/utils/listSpecialChars.d.ts.map +1 -1
- package/dist/types/utils/mapChunksBetweenFiles.d.ts +10 -6
- package/dist/types/utils/mapChunksBetweenFiles.d.ts.map +1 -1
- package/dist/types/utils/reorderParagraphs.d.ts +4 -1
- package/dist/types/utils/reorderParagraphs.d.ts.map +1 -1
- package/dist/types/watch.d.ts +9 -6
- package/dist/types/watch.d.ts.map +1 -1
- package/package.json +42 -47
- package/LICENSE +0 -202
- package/dist/cjs/fill/autoFill.cjs +0 -105
- package/dist/cjs/fill/autoFill.cjs.map +0 -1
- package/dist/cjs/fill/index.cjs.map +0 -1
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/utils/getChunk.cjs +0 -77
- package/dist/cjs/utils/getChunk.cjs.map +0 -1
- package/dist/cjs/utils/splitTextByLine.cjs +0 -35
- package/dist/cjs/utils/splitTextByLine.cjs.map +0 -1
- package/dist/esm/fill/autoFill.mjs +0 -92
- package/dist/esm/fill/autoFill.mjs.map +0 -1
- package/dist/esm/fill/index.mjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/prompts/REVIEW_PROMPT.md +0 -37
- package/dist/esm/prompts/TRANSLATE_PROMPT.md +0 -38
- package/dist/esm/utils/calculrateChunkTest.md +0 -9
- package/dist/esm/utils/getChunk.mjs +0 -53
- package/dist/esm/utils/getChunk.mjs.map +0 -1
- package/dist/esm/utils/splitTextByLine.mjs +0 -11
- package/dist/esm/utils/splitTextByLine.mjs.map +0 -1
- package/dist/types/fill/autoFill.d.ts +0 -4
- package/dist/types/fill/autoFill.d.ts.map +0 -1
- package/dist/types/fill/index.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/utils/getChunk.d.ts +0 -9
- package/dist/types/utils/getChunk.d.ts.map +0 -1
- package/dist/types/utils/splitTextByLine.d.ts +0 -2
- package/dist/types/utils/splitTextByLine.d.ts.map +0 -1
- /package/dist/{cjs → assets}/prompts/REVIEW_PROMPT.md +0 -0
- /package/dist/{cjs → assets}/prompts/TRANSLATE_PROMPT.md +0 -0
- /package/dist/{cjs/utils/calculrateChunkTest.md → assets/utils/_calculateChunkTest.md} +0 -0
|
@@ -1,121 +1,101 @@
|
|
|
1
1
|
import { chunkText } from "./calculateChunks.mjs";
|
|
2
|
-
import { splitTextByLines } from "
|
|
2
|
+
import { splitTextByLines } from "@intlayer/chokidar";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/mapChunksBetweenFiles.ts
|
|
5
|
+
/**
|
|
6
|
+
* Maps chunks from base file to corresponding chunks in updated file,
|
|
7
|
+
* handling insertions, deletions, and modifications properly.
|
|
8
|
+
*/
|
|
3
9
|
const mapChunksBetweenFiles = (baseFileContent, updatedFileContent, maxCharsPerChunk = 800, changedLines) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
mappedRange.start,
|
|
31
|
-
mappedRange.length
|
|
32
|
-
)
|
|
33
|
-
};
|
|
34
|
-
updatedChunk.charStart = getCharStartForLine(
|
|
35
|
-
updatedFileContent,
|
|
36
|
-
updatedChunk.lineStart
|
|
37
|
-
);
|
|
38
|
-
updatedChunk.charLength = updatedChunk.content.length;
|
|
39
|
-
const hasChanges = determineIfChunkHasChanges(
|
|
40
|
-
baseChunk,
|
|
41
|
-
updatedChunk,
|
|
42
|
-
changedLines
|
|
43
|
-
);
|
|
44
|
-
return {
|
|
45
|
-
baseChunk,
|
|
46
|
-
updatedChunk,
|
|
47
|
-
hasChanges
|
|
48
|
-
};
|
|
49
|
-
});
|
|
10
|
+
const baseChunks = chunkText(baseFileContent, maxCharsPerChunk, 0);
|
|
11
|
+
const baseLines = splitTextByLines(baseFileContent);
|
|
12
|
+
const updatedLines = splitTextByLines(updatedFileContent);
|
|
13
|
+
const lineMapping = createLineMapping(baseLines, updatedLines);
|
|
14
|
+
return baseChunks.map((baseChunk) => {
|
|
15
|
+
const mappedRange = mapLineRange(baseChunk.lineStart, baseChunk.lineLength, lineMapping);
|
|
16
|
+
if (!mappedRange) return {
|
|
17
|
+
baseChunk,
|
|
18
|
+
updatedChunk: null,
|
|
19
|
+
hasChanges: true
|
|
20
|
+
};
|
|
21
|
+
const updatedChunk = {
|
|
22
|
+
lineStart: mappedRange.start,
|
|
23
|
+
lineLength: mappedRange.length,
|
|
24
|
+
charStart: 0,
|
|
25
|
+
charLength: 0,
|
|
26
|
+
content: extractLinesFromRange(updatedLines, mappedRange.start, mappedRange.length)
|
|
27
|
+
};
|
|
28
|
+
updatedChunk.charStart = getCharStartForLine(updatedFileContent, updatedChunk.lineStart);
|
|
29
|
+
updatedChunk.charLength = updatedChunk.content.length;
|
|
30
|
+
return {
|
|
31
|
+
baseChunk,
|
|
32
|
+
updatedChunk,
|
|
33
|
+
hasChanges: determineIfChunkHasChanges(baseChunk, updatedChunk, changedLines)
|
|
34
|
+
};
|
|
35
|
+
});
|
|
50
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* Creates a mapping between line numbers in base file and updated file
|
|
39
|
+
* Returns a map where key = base line number, value = updated line number (or null if deleted)
|
|
40
|
+
*/
|
|
51
41
|
const createLineMapping = (baseLines, updatedLines) => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
mapping.set(i - 1, j - 1);
|
|
68
|
-
i--;
|
|
69
|
-
j--;
|
|
70
|
-
} else if (i > 0 && (j === 0 || dp[i - 1][j] >= dp[i][j - 1])) {
|
|
71
|
-
mapping.set(i - 1, null);
|
|
72
|
-
i--;
|
|
73
|
-
} else {
|
|
74
|
-
j--;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return mapping;
|
|
42
|
+
const mapping = /* @__PURE__ */ new Map();
|
|
43
|
+
const dp = Array(baseLines.length + 1).fill(null).map(() => Array(updatedLines.length + 1).fill(0));
|
|
44
|
+
for (let i$1 = 1; i$1 <= baseLines.length; i$1++) for (let j$1 = 1; j$1 <= updatedLines.length; j$1++) if (baseLines[i$1 - 1] === updatedLines[j$1 - 1]) dp[i$1][j$1] = dp[i$1 - 1][j$1 - 1] + 1;
|
|
45
|
+
else dp[i$1][j$1] = Math.max(dp[i$1 - 1][j$1], dp[i$1][j$1 - 1]);
|
|
46
|
+
let i = baseLines.length;
|
|
47
|
+
let j = updatedLines.length;
|
|
48
|
+
while (i > 0 || j > 0) if (i > 0 && j > 0 && baseLines[i - 1] === updatedLines[j - 1]) {
|
|
49
|
+
mapping.set(i - 1, j - 1);
|
|
50
|
+
i--;
|
|
51
|
+
j--;
|
|
52
|
+
} else if (i > 0 && (j === 0 || dp[i - 1][j] >= dp[i][j - 1])) {
|
|
53
|
+
mapping.set(i - 1, null);
|
|
54
|
+
i--;
|
|
55
|
+
} else j--;
|
|
56
|
+
return mapping;
|
|
78
57
|
};
|
|
58
|
+
/**
|
|
59
|
+
* Maps a line range from base file to updated file using the line mapping
|
|
60
|
+
*/
|
|
79
61
|
const mapLineRange = (baseStart, baseLength, lineMapping) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const end = mappedLines[mappedLines.length - 1];
|
|
93
|
-
return {
|
|
94
|
-
start,
|
|
95
|
-
length: end - start + 1
|
|
96
|
-
};
|
|
62
|
+
const mappedLines = [];
|
|
63
|
+
for (let i = baseStart; i < baseStart + baseLength; i++) {
|
|
64
|
+
const mappedLine = lineMapping.get(i);
|
|
65
|
+
if (mappedLine !== null && mappedLine !== void 0) mappedLines.push(mappedLine);
|
|
66
|
+
}
|
|
67
|
+
if (mappedLines.length === 0) return null;
|
|
68
|
+
mappedLines.sort((a, b) => a - b);
|
|
69
|
+
const start = mappedLines[0];
|
|
70
|
+
return {
|
|
71
|
+
start,
|
|
72
|
+
length: mappedLines[mappedLines.length - 1] - start + 1
|
|
73
|
+
};
|
|
97
74
|
};
|
|
75
|
+
/**
|
|
76
|
+
* Extracts lines from a range in the lines array
|
|
77
|
+
*/
|
|
98
78
|
const extractLinesFromRange = (lines, start, length) => {
|
|
99
|
-
|
|
100
|
-
|
|
79
|
+
const endIndex = Math.min(start + length, lines.length);
|
|
80
|
+
return lines.slice(start, endIndex).join("");
|
|
101
81
|
};
|
|
82
|
+
/**
|
|
83
|
+
* Gets the character position where a line starts in the text
|
|
84
|
+
*/
|
|
102
85
|
const getCharStartForLine = (text, lineNumber) => {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
return charStart;
|
|
86
|
+
const lines = splitTextByLines(text);
|
|
87
|
+
let charStart = 0;
|
|
88
|
+
for (let i = 0; i < Math.min(lineNumber, lines.length); i++) charStart += lines[i].length;
|
|
89
|
+
return charStart;
|
|
109
90
|
};
|
|
91
|
+
/**
|
|
92
|
+
* Determines if a chunk has changes based on git changed lines or content comparison
|
|
93
|
+
*/
|
|
110
94
|
const determineIfChunkHasChanges = (baseChunk, updatedChunk, changedLines) => {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
(line) => line >= updatedChunk.lineStart && line < updatedChunk.lineStart + updatedChunk.lineLength
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
return baseChunk.content !== updatedChunk.content;
|
|
117
|
-
};
|
|
118
|
-
export {
|
|
119
|
-
mapChunksBetweenFiles
|
|
95
|
+
if (changedLines && changedLines.length > 0) return changedLines.some((line) => line >= updatedChunk.lineStart && line < updatedChunk.lineStart + updatedChunk.lineLength);
|
|
96
|
+
return baseChunk.content !== updatedChunk.content;
|
|
120
97
|
};
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
export { mapChunksBetweenFiles };
|
|
121
101
|
//# sourceMappingURL=mapChunksBetweenFiles.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/mapChunksBetweenFiles.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"mapChunksBetweenFiles.mjs","names":["updatedChunk: ChunkLineResult","dp: number[][]","i","j","mappedLines: number[]"],"sources":["../../../src/utils/mapChunksBetweenFiles.ts"],"sourcesContent":["import { splitTextByLines } from '@intlayer/chokidar';\nimport { type ChunkLineResult, chunkText } from './calculateChunks';\n\nexport interface ChunkMapping {\n baseChunk: ChunkLineResult;\n updatedChunk: ChunkLineResult | null; // null if the chunk was deleted\n hasChanges: boolean;\n}\n\n/**\n * Maps chunks from base file to corresponding chunks in updated file,\n * handling insertions, deletions, and modifications properly.\n */\nexport const mapChunksBetweenFiles = (\n baseFileContent: string,\n updatedFileContent: string,\n maxCharsPerChunk: number = 800,\n changedLines?: number[]\n): ChunkMapping[] => {\n const baseChunks = chunkText(baseFileContent, maxCharsPerChunk, 0);\n const baseLines = splitTextByLines(baseFileContent);\n const updatedLines = splitTextByLines(updatedFileContent);\n\n // Create a simple line mapping using LCS (Longest Common Subsequence) approach\n const lineMapping = createLineMapping(baseLines, updatedLines);\n\n return baseChunks.map((baseChunk): ChunkMapping => {\n // Map the base chunk's line range to the updated file\n const mappedRange = mapLineRange(\n baseChunk.lineStart,\n baseChunk.lineLength,\n lineMapping\n );\n\n if (!mappedRange) {\n // This chunk was completely deleted\n return {\n baseChunk,\n updatedChunk: null,\n hasChanges: true,\n };\n }\n\n // Create the corresponding chunk in the updated file\n const updatedChunk: ChunkLineResult = {\n lineStart: mappedRange.start,\n lineLength: mappedRange.length,\n charStart: 0, // Will be calculated when needed\n charLength: 0, // Will be calculated when needed\n content: extractLinesFromRange(\n updatedLines,\n mappedRange.start,\n mappedRange.length\n ),\n };\n\n // Calculate character positions\n updatedChunk.charStart = getCharStartForLine(\n updatedFileContent,\n updatedChunk.lineStart\n );\n updatedChunk.charLength = updatedChunk.content.length;\n\n // Determine if this chunk has changes\n const hasChanges = determineIfChunkHasChanges(\n baseChunk,\n updatedChunk,\n changedLines\n );\n\n return {\n baseChunk,\n updatedChunk,\n hasChanges,\n };\n });\n};\n\n/**\n * Creates a mapping between line numbers in base file and updated file\n * Returns a map where key = base line number, value = updated line number (or null if deleted)\n */\nconst createLineMapping = (\n baseLines: string[],\n updatedLines: string[]\n): Map<number, number | null> => {\n const mapping = new Map<number, number | null>();\n\n // Use a simple diff algorithm (similar to Myers algorithm but simplified)\n const dp: number[][] = Array(baseLines.length + 1)\n .fill(null)\n .map(() => Array(updatedLines.length + 1).fill(0));\n\n // Fill the DP table\n for (let i = 1; i <= baseLines.length; i++) {\n for (let j = 1; j <= updatedLines.length; j++) {\n if (baseLines[i - 1] === updatedLines[j - 1]) {\n dp[i][j] = dp[i - 1][j - 1] + 1;\n } else {\n dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);\n }\n }\n }\n\n // Backtrack to create the mapping\n let i = baseLines.length;\n let j = updatedLines.length;\n\n while (i > 0 || j > 0) {\n if (i > 0 && j > 0 && baseLines[i - 1] === updatedLines[j - 1]) {\n // Lines match\n mapping.set(i - 1, j - 1);\n i--;\n j--;\n } else if (i > 0 && (j === 0 || dp[i - 1][j] >= dp[i][j - 1])) {\n // Line was deleted from base\n mapping.set(i - 1, null);\n i--;\n } else {\n // Line was added to updated (no mapping needed for base)\n j--;\n }\n }\n\n return mapping;\n};\n\n/**\n * Maps a line range from base file to updated file using the line mapping\n */\nconst mapLineRange = (\n baseStart: number,\n baseLength: number,\n lineMapping: Map<number, number | null>\n): { start: number; length: number } | null => {\n const mappedLines: number[] = [];\n\n for (let i = baseStart; i < baseStart + baseLength; i++) {\n const mappedLine = lineMapping.get(i);\n if (mappedLine !== null && mappedLine !== undefined) {\n mappedLines.push(mappedLine);\n }\n }\n\n if (mappedLines.length === 0) {\n return null; // All lines were deleted\n }\n\n // Find the continuous range in the mapped lines\n mappedLines.sort((a, b) => a - b);\n const start = mappedLines[0];\n const end = mappedLines[mappedLines.length - 1];\n\n return {\n start,\n length: end - start + 1,\n };\n};\n\n/**\n * Extracts lines from a range in the lines array\n */\nconst extractLinesFromRange = (\n lines: string[],\n start: number,\n length: number\n): string => {\n const endIndex = Math.min(start + length, lines.length);\n return lines.slice(start, endIndex).join('');\n};\n\n/**\n * Gets the character position where a line starts in the text\n */\nconst getCharStartForLine = (text: string, lineNumber: number): number => {\n const lines = splitTextByLines(text);\n let charStart = 0;\n\n for (let i = 0; i < Math.min(lineNumber, lines.length); i++) {\n charStart += lines[i].length;\n }\n\n return charStart;\n};\n\n/**\n * Determines if a chunk has changes based on git changed lines or content comparison\n */\nconst determineIfChunkHasChanges = (\n baseChunk: ChunkLineResult,\n updatedChunk: ChunkLineResult,\n changedLines?: number[]\n): boolean => {\n // If we have git changed lines, use them for precise detection\n if (changedLines && changedLines.length > 0) {\n return changedLines.some(\n (line) =>\n line >= updatedChunk.lineStart &&\n line < updatedChunk.lineStart + updatedChunk.lineLength\n );\n }\n\n // Fallback to content comparison\n return baseChunk.content !== updatedChunk.content;\n};\n"],"mappings":";;;;;;;;AAaA,MAAa,yBACX,iBACA,oBACA,mBAA2B,KAC3B,iBACmB;CACnB,MAAM,aAAa,UAAU,iBAAiB,kBAAkB,EAAE;CAClE,MAAM,YAAY,iBAAiB,gBAAgB;CACnD,MAAM,eAAe,iBAAiB,mBAAmB;CAGzD,MAAM,cAAc,kBAAkB,WAAW,aAAa;AAE9D,QAAO,WAAW,KAAK,cAA4B;EAEjD,MAAM,cAAc,aAClB,UAAU,WACV,UAAU,YACV,YACD;AAED,MAAI,CAAC,YAEH,QAAO;GACL;GACA,cAAc;GACd,YAAY;GACb;EAIH,MAAMA,eAAgC;GACpC,WAAW,YAAY;GACvB,YAAY,YAAY;GACxB,WAAW;GACX,YAAY;GACZ,SAAS,sBACP,cACA,YAAY,OACZ,YAAY,OACb;GACF;AAGD,eAAa,YAAY,oBACvB,oBACA,aAAa,UACd;AACD,eAAa,aAAa,aAAa,QAAQ;AAS/C,SAAO;GACL;GACA;GACA,YATiB,2BACjB,WACA,cACA,aACD;GAMA;GACD;;;;;;AAOJ,MAAM,qBACJ,WACA,iBAC+B;CAC/B,MAAM,0BAAU,IAAI,KAA4B;CAGhD,MAAMC,KAAiB,MAAM,UAAU,SAAS,EAAE,CAC/C,KAAK,KAAK,CACV,UAAU,MAAM,aAAa,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC;AAGpD,MAAK,IAAIC,MAAI,GAAGA,OAAK,UAAU,QAAQ,MACrC,MAAK,IAAIC,MAAI,GAAGA,OAAK,aAAa,QAAQ,MACxC,KAAI,UAAUD,MAAI,OAAO,aAAaC,MAAI,GACxC,IAAGD,KAAGC,OAAK,GAAGD,MAAI,GAAGC,MAAI,KAAK;KAE9B,IAAGD,KAAGC,OAAK,KAAK,IAAI,GAAGD,MAAI,GAAGC,MAAI,GAAGD,KAAGC,MAAI,GAAG;CAMrD,IAAI,IAAI,UAAU;CAClB,IAAI,IAAI,aAAa;AAErB,QAAO,IAAI,KAAK,IAAI,EAClB,KAAI,IAAI,KAAK,IAAI,KAAK,UAAU,IAAI,OAAO,aAAa,IAAI,IAAI;AAE9D,UAAQ,IAAI,IAAI,GAAG,IAAI,EAAE;AACzB;AACA;YACS,IAAI,MAAM,MAAM,KAAK,GAAG,IAAI,GAAG,MAAM,GAAG,GAAG,IAAI,KAAK;AAE7D,UAAQ,IAAI,IAAI,GAAG,KAAK;AACxB;OAGA;AAIJ,QAAO;;;;;AAMT,MAAM,gBACJ,WACA,YACA,gBAC6C;CAC7C,MAAMC,cAAwB,EAAE;AAEhC,MAAK,IAAI,IAAI,WAAW,IAAI,YAAY,YAAY,KAAK;EACvD,MAAM,aAAa,YAAY,IAAI,EAAE;AACrC,MAAI,eAAe,QAAQ,eAAe,OACxC,aAAY,KAAK,WAAW;;AAIhC,KAAI,YAAY,WAAW,EACzB,QAAO;AAIT,aAAY,MAAM,GAAG,MAAM,IAAI,EAAE;CACjC,MAAM,QAAQ,YAAY;AAG1B,QAAO;EACL;EACA,QAJU,YAAY,YAAY,SAAS,KAI7B,QAAQ;EACvB;;;;;AAMH,MAAM,yBACJ,OACA,OACA,WACW;CACX,MAAM,WAAW,KAAK,IAAI,QAAQ,QAAQ,MAAM,OAAO;AACvD,QAAO,MAAM,MAAM,OAAO,SAAS,CAAC,KAAK,GAAG;;;;;AAM9C,MAAM,uBAAuB,MAAc,eAA+B;CACxE,MAAM,QAAQ,iBAAiB,KAAK;CACpC,IAAI,YAAY;AAEhB,MAAK,IAAI,IAAI,GAAG,IAAI,KAAK,IAAI,YAAY,MAAM,OAAO,EAAE,IACtD,cAAa,MAAM,GAAG;AAGxB,QAAO;;;;;AAMT,MAAM,8BACJ,WACA,cACA,iBACY;AAEZ,KAAI,gBAAgB,aAAa,SAAS,EACxC,QAAO,aAAa,MACjB,SACC,QAAQ,aAAa,aACrB,OAAO,aAAa,YAAY,aAAa,WAChD;AAIH,QAAO,UAAU,YAAY,aAAa"}
|
|
@@ -1,101 +1,93 @@
|
|
|
1
1
|
import { listSpecialChars } from "./listSpecialChars.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/reorderParagraphs.ts
|
|
4
|
+
/**
|
|
5
|
+
* Split a text into paragraphs.
|
|
6
|
+
*
|
|
7
|
+
* We consider a paragraph boundary to be a block of at least two consecutive
|
|
8
|
+
* new-lines that is immediately followed by a non-white-space character. This
|
|
9
|
+
* way, internal blank lines that are part of the same paragraph (e.g. a list
|
|
10
|
+
* item that purposely contains a visual break) are preserved while true
|
|
11
|
+
* paragraph breaks – the ones generated when calling `arr.join("\n\n")` in
|
|
12
|
+
* the tests – are still detected.
|
|
13
|
+
*/
|
|
2
14
|
const splitByParagraph = (text) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
return paragraphs;
|
|
15
|
+
const paragraphs = [];
|
|
16
|
+
const tokens = text.split(/(\n{2,})/);
|
|
17
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
18
|
+
const token = tokens[i];
|
|
19
|
+
if (i % 2 === 0) {
|
|
20
|
+
if (token) paragraphs.push(token);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const pairsOfNewlines = Math.floor(token.length / 2);
|
|
24
|
+
const blankParagraphs = Math.max(0, pairsOfNewlines - 2);
|
|
25
|
+
for (let j = 0; j < blankParagraphs; j++) paragraphs.push("\n\n");
|
|
26
|
+
}
|
|
27
|
+
return paragraphs;
|
|
18
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Determine whether two paragraphs match – either exactly, or by sharing the
|
|
31
|
+
* same "special-character signature".
|
|
32
|
+
*/
|
|
19
33
|
const paragraphMatches = (paragraph, baseParagraph, paragraphSignature, baseSignature) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return true;
|
|
34
|
+
if (paragraph === baseParagraph) return true;
|
|
35
|
+
if (paragraphSignature.length !== baseSignature.length) return false;
|
|
36
|
+
for (let i = 0; i < paragraphSignature.length; i++) if (paragraphSignature[i].char !== baseSignature[i].char) return false;
|
|
37
|
+
return true;
|
|
28
38
|
};
|
|
39
|
+
/**
|
|
40
|
+
* Re-order `textToReorder` so that its paragraphs follow the ordering found in
|
|
41
|
+
* `baseFileContent`, while preserving any extra paragraphs (those not present
|
|
42
|
+
* in the base file) in a position that is intuitive for a human reader: right
|
|
43
|
+
* after the closest preceding paragraph coming from the base file.
|
|
44
|
+
*/
|
|
29
45
|
const reorderParagraphs = (textToReorder, baseFileContent) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const extraParagraphsBuckets = {};
|
|
74
|
-
insertAfterBaseIdx.forEach((afterIdx, paragraphIdx) => {
|
|
75
|
-
if (afterIdx === -1) return;
|
|
76
|
-
extraParagraphsBuckets[afterIdx] = extraParagraphsBuckets[afterIdx] || [];
|
|
77
|
-
extraParagraphsBuckets[afterIdx].push(paragraphIdx);
|
|
78
|
-
});
|
|
79
|
-
for (let bIdx = 0; bIdx < baseFileParagraphs.length; bIdx++) {
|
|
80
|
-
const matchedParagraphIdx = firstMatchIndexForBase[bIdx];
|
|
81
|
-
if (matchedParagraphIdx !== -1) {
|
|
82
|
-
result.push(textToReorderParagraphs[matchedParagraphIdx]);
|
|
83
|
-
}
|
|
84
|
-
if (extraParagraphsBuckets[bIdx]) {
|
|
85
|
-
extraParagraphsBuckets[bIdx].forEach((pIdx) => {
|
|
86
|
-
result.push(textToReorderParagraphs[pIdx]);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
const leadingExtras = [];
|
|
91
|
-
insertAfterBaseIdx.forEach((afterIdx, pIdx) => {
|
|
92
|
-
if (afterIdx === -1 && paragraphMatchedBaseIdx[pIdx] === null) {
|
|
93
|
-
leadingExtras.push(textToReorderParagraphs[pIdx]);
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
return [...leadingExtras, ...result].join("\n\n");
|
|
97
|
-
};
|
|
98
|
-
export {
|
|
99
|
-
reorderParagraphs
|
|
46
|
+
const baseFileParagraphs = splitByParagraph(baseFileContent);
|
|
47
|
+
const textToReorderParagraphs = splitByParagraph(textToReorder);
|
|
48
|
+
const baseSignatures = baseFileParagraphs.map((p) => listSpecialChars(p));
|
|
49
|
+
const textSignatures = textToReorderParagraphs.map((p) => listSpecialChars(p));
|
|
50
|
+
const firstMatchIndexForBase = Array(baseFileParagraphs.length).fill(-1);
|
|
51
|
+
const paragraphMatchedBaseIdx = Array(textToReorderParagraphs.length).fill(null);
|
|
52
|
+
for (let i = 0; i < textToReorderParagraphs.length; i++) {
|
|
53
|
+
const paragraph = textToReorderParagraphs[i];
|
|
54
|
+
const sig = textSignatures[i];
|
|
55
|
+
let foundIdx = baseFileParagraphs.findIndex((baseParagraph, idx) => firstMatchIndexForBase[idx] === -1 && paragraph === baseParagraph);
|
|
56
|
+
if (foundIdx === -1) foundIdx = baseFileParagraphs.findIndex((baseParagraph, idx) => firstMatchIndexForBase[idx] === -1 && paragraphMatches(paragraph, baseParagraph, sig, baseSignatures[idx]));
|
|
57
|
+
if (foundIdx !== -1) {
|
|
58
|
+
firstMatchIndexForBase[foundIdx] = i;
|
|
59
|
+
paragraphMatchedBaseIdx[i] = foundIdx;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const insertAfterBaseIdx = Array(textToReorderParagraphs.length).fill(-1);
|
|
63
|
+
let maxBaseIdxEncountered = -1;
|
|
64
|
+
for (let i = 0; i < textToReorderParagraphs.length; i++) {
|
|
65
|
+
const matchedBase = paragraphMatchedBaseIdx[i];
|
|
66
|
+
if (matchedBase !== null) {
|
|
67
|
+
if (matchedBase > maxBaseIdxEncountered) maxBaseIdxEncountered = matchedBase;
|
|
68
|
+
} else insertAfterBaseIdx[i] = maxBaseIdxEncountered;
|
|
69
|
+
}
|
|
70
|
+
const result = [];
|
|
71
|
+
const extraParagraphsBuckets = {};
|
|
72
|
+
insertAfterBaseIdx.forEach((afterIdx, paragraphIdx) => {
|
|
73
|
+
if (afterIdx === -1) return;
|
|
74
|
+
extraParagraphsBuckets[afterIdx] = extraParagraphsBuckets[afterIdx] || [];
|
|
75
|
+
extraParagraphsBuckets[afterIdx].push(paragraphIdx);
|
|
76
|
+
});
|
|
77
|
+
for (let bIdx = 0; bIdx < baseFileParagraphs.length; bIdx++) {
|
|
78
|
+
const matchedParagraphIdx = firstMatchIndexForBase[bIdx];
|
|
79
|
+
if (matchedParagraphIdx !== -1) result.push(textToReorderParagraphs[matchedParagraphIdx]);
|
|
80
|
+
if (extraParagraphsBuckets[bIdx]) extraParagraphsBuckets[bIdx].forEach((pIdx) => {
|
|
81
|
+
result.push(textToReorderParagraphs[pIdx]);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const leadingExtras = [];
|
|
85
|
+
insertAfterBaseIdx.forEach((afterIdx, pIdx) => {
|
|
86
|
+
if (afterIdx === -1 && paragraphMatchedBaseIdx[pIdx] === null) leadingExtras.push(textToReorderParagraphs[pIdx]);
|
|
87
|
+
});
|
|
88
|
+
return [...leadingExtras, ...result].join("\n\n");
|
|
100
89
|
};
|
|
90
|
+
|
|
91
|
+
//#endregion
|
|
92
|
+
export { reorderParagraphs };
|
|
101
93
|
//# sourceMappingURL=reorderParagraphs.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/reorderParagraphs.ts"],"sourcesContent":["import { listSpecialChars } from './listSpecialChars';\n\n/**\n * Split a text into paragraphs.\n *\n * We consider a paragraph boundary to be a block of at least two consecutive\n * new-lines that is immediately followed by a non-white-space character. This\n * way, internal blank lines that are part of the same paragraph (e.g. a list\n * item that purposely contains a visual break) are preserved while true\n * paragraph breaks – the ones generated when calling `arr.join(\"\\n\\n\")` in\n * the tests – are still detected.\n */\nconst splitByParagraph = (text: string): string[] => {\n const paragraphs: string[] = [];\n\n // Capture the delimiter so that we can inspect how many new-lines it\n // contains. We know that the test strings only use LF, so we keep the\n // regex simple here.\n const tokens = text.split(/(\\n{2,})/);\n\n for (let i = 0; i < tokens.length; i++) {\n const token = tokens[i];\n\n // Even-indexed tokens are the actual paragraph contents.\n if (i % 2 === 0) {\n if (token) paragraphs.push(token);\n continue;\n }\n\n // Odd-indexed tokens are the delimiters (>= two consecutive new-lines).\n // The first and last pairs represent the natural separators that are\n // added when paragraphs are later joined with \"\\n\\n\". Any additional\n // pairs in between correspond to *explicit* blank paragraphs that were\n // present in the original text and must therefore be preserved.\n const pairsOfNewlines = Math.floor(token.length / 2);\n const blankParagraphs = Math.max(0, pairsOfNewlines - 2);\n\n for (let j = 0; j < blankParagraphs; j++) {\n paragraphs.push('\\n\\n');\n }\n }\n\n return paragraphs;\n};\n\n/**\n * Determine whether two paragraphs match – either exactly, or by sharing the\n * same \"special-character signature\".\n */\nconst paragraphMatches = (\n paragraph: string,\n baseParagraph: string,\n paragraphSignature: ReturnType<typeof listSpecialChars>,\n baseSignature: ReturnType<typeof listSpecialChars>\n): boolean => {\n if (paragraph === baseParagraph) return true;\n // fallback to special-character signature comparison\n if (paragraphSignature.length !== baseSignature.length) return false;\n\n for (let i = 0; i < paragraphSignature.length; i++) {\n if (paragraphSignature[i].char !== baseSignature[i].char) {\n return false;\n }\n }\n return true;\n};\n\n/**\n * Re-order `textToReorder` so that its paragraphs follow the ordering found in\n * `baseFileContent`, while preserving any extra paragraphs (those not present\n * in the base file) in a position that is intuitive for a human reader: right\n * after the closest preceding paragraph coming from the base file.\n */\nexport const reorderParagraphs = (\n textToReorder: string,\n baseFileContent: string\n): string => {\n // 1. Split both texts into paragraphs and pre-compute their signatures.\n const baseFileParagraphs = splitByParagraph(baseFileContent);\n const textToReorderParagraphs = splitByParagraph(textToReorder);\n\n const baseSignatures = baseFileParagraphs.map((p) => listSpecialChars(p));\n const textSignatures = textToReorderParagraphs.map((p) =>\n listSpecialChars(p)\n );\n\n // 2. For every paragraph in the text to reorder, find the *first* base\n // paragraph it matches. We only allow each base paragraph to be matched\n // once. Any further identical paragraphs will be treated as \"extra\" and\n // will be positioned later on, next to their closest neighbour.\n const firstMatchIndexForBase: number[] = Array(\n baseFileParagraphs.length\n ).fill(-1);\n const paragraphMatchedBaseIdx: (number | null)[] = Array(\n textToReorderParagraphs.length\n ).fill(null);\n\n for (let i = 0; i < textToReorderParagraphs.length; i++) {\n const paragraph = textToReorderParagraphs[i];\n const sig = textSignatures[i];\n\n // exact match pass first for performance\n let foundIdx = baseFileParagraphs.findIndex(\n (baseParagraph, idx) =>\n firstMatchIndexForBase[idx] === -1 && paragraph === baseParagraph\n );\n\n if (foundIdx === -1) {\n // fallback to the signature comparison\n foundIdx = baseFileParagraphs.findIndex(\n (baseParagraph, idx) =>\n firstMatchIndexForBase[idx] === -1 &&\n paragraphMatches(paragraph, baseParagraph, sig, baseSignatures[idx])\n );\n }\n\n if (foundIdx !== -1) {\n firstMatchIndexForBase[foundIdx] = i;\n paragraphMatchedBaseIdx[i] = foundIdx;\n }\n }\n\n // 3. For the paragraphs that *didn't* get matched to a base paragraph, we\n // record the highest-numbered base paragraph that was matched *before* it\n // in the original text. The extra paragraph will later be placed right\n // after that paragraph in the final ordering.\n const insertAfterBaseIdx: number[] = Array(\n textToReorderParagraphs.length\n ).fill(-1);\n let maxBaseIdxEncountered = -1;\n\n for (let i = 0; i < textToReorderParagraphs.length; i++) {\n const matchedBase = paragraphMatchedBaseIdx[i];\n\n if (matchedBase !== null) {\n if (matchedBase > maxBaseIdxEncountered) {\n maxBaseIdxEncountered = matchedBase;\n }\n } else {\n insertAfterBaseIdx[i] = maxBaseIdxEncountered;\n }\n }\n\n // 4. Build the final, reordered list of paragraphs.\n const result: string[] = [];\n\n // Helper: quickly retrieve all indices of paragraphs that should be inserted\n // after a given base index, while keeping their original order.\n const extraParagraphsBuckets: Record<number, number[]> = {};\n insertAfterBaseIdx.forEach((afterIdx, paragraphIdx) => {\n if (afterIdx === -1) return; // will be handled later (if any)\n extraParagraphsBuckets[afterIdx] = extraParagraphsBuckets[afterIdx] || [];\n extraParagraphsBuckets[afterIdx].push(paragraphIdx);\n });\n\n for (let bIdx = 0; bIdx < baseFileParagraphs.length; bIdx++) {\n const matchedParagraphIdx = firstMatchIndexForBase[bIdx];\n\n if (matchedParagraphIdx !== -1) {\n result.push(textToReorderParagraphs[matchedParagraphIdx]);\n }\n\n if (extraParagraphsBuckets[bIdx]) {\n extraParagraphsBuckets[bIdx].forEach((pIdx) => {\n result.push(textToReorderParagraphs[pIdx]);\n });\n }\n }\n\n // Finally, if there were extra paragraphs appearing *before* any matched\n // base paragraph (insertAfterBaseIdx === -1), we prepend them to the output\n // in their original order.\n const leadingExtras: string[] = [];\n insertAfterBaseIdx.forEach((afterIdx, pIdx) => {\n if (afterIdx === -1 && paragraphMatchedBaseIdx[pIdx] === null) {\n leadingExtras.push(textToReorderParagraphs[pIdx]);\n }\n });\n\n return [...leadingExtras, ...result].join('\\n\\n');\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"reorderParagraphs.mjs","names":["paragraphs: string[]","firstMatchIndexForBase: number[]","paragraphMatchedBaseIdx: (number | null)[]","insertAfterBaseIdx: number[]","result: string[]","extraParagraphsBuckets: Record<number, number[]>","leadingExtras: string[]"],"sources":["../../../src/utils/reorderParagraphs.ts"],"sourcesContent":["import { listSpecialChars } from './listSpecialChars';\n\n/**\n * Split a text into paragraphs.\n *\n * We consider a paragraph boundary to be a block of at least two consecutive\n * new-lines that is immediately followed by a non-white-space character. This\n * way, internal blank lines that are part of the same paragraph (e.g. a list\n * item that purposely contains a visual break) are preserved while true\n * paragraph breaks – the ones generated when calling `arr.join(\"\\n\\n\")` in\n * the tests – are still detected.\n */\nconst splitByParagraph = (text: string): string[] => {\n const paragraphs: string[] = [];\n\n // Capture the delimiter so that we can inspect how many new-lines it\n // contains. We know that the test strings only use LF, so we keep the\n // regex simple here.\n const tokens = text.split(/(\\n{2,})/);\n\n for (let i = 0; i < tokens.length; i++) {\n const token = tokens[i];\n\n // Even-indexed tokens are the actual paragraph contents.\n if (i % 2 === 0) {\n if (token) paragraphs.push(token);\n continue;\n }\n\n // Odd-indexed tokens are the delimiters (>= two consecutive new-lines).\n // The first and last pairs represent the natural separators that are\n // added when paragraphs are later joined with \"\\n\\n\". Any additional\n // pairs in between correspond to *explicit* blank paragraphs that were\n // present in the original text and must therefore be preserved.\n const pairsOfNewlines = Math.floor(token.length / 2);\n const blankParagraphs = Math.max(0, pairsOfNewlines - 2);\n\n for (let j = 0; j < blankParagraphs; j++) {\n paragraphs.push('\\n\\n');\n }\n }\n\n return paragraphs;\n};\n\n/**\n * Determine whether two paragraphs match – either exactly, or by sharing the\n * same \"special-character signature\".\n */\nconst paragraphMatches = (\n paragraph: string,\n baseParagraph: string,\n paragraphSignature: ReturnType<typeof listSpecialChars>,\n baseSignature: ReturnType<typeof listSpecialChars>\n): boolean => {\n if (paragraph === baseParagraph) return true;\n // fallback to special-character signature comparison\n if (paragraphSignature.length !== baseSignature.length) return false;\n\n for (let i = 0; i < paragraphSignature.length; i++) {\n if (paragraphSignature[i].char !== baseSignature[i].char) {\n return false;\n }\n }\n return true;\n};\n\n/**\n * Re-order `textToReorder` so that its paragraphs follow the ordering found in\n * `baseFileContent`, while preserving any extra paragraphs (those not present\n * in the base file) in a position that is intuitive for a human reader: right\n * after the closest preceding paragraph coming from the base file.\n */\nexport const reorderParagraphs = (\n textToReorder: string,\n baseFileContent: string\n): string => {\n // 1. Split both texts into paragraphs and pre-compute their signatures.\n const baseFileParagraphs = splitByParagraph(baseFileContent);\n const textToReorderParagraphs = splitByParagraph(textToReorder);\n\n const baseSignatures = baseFileParagraphs.map((p) => listSpecialChars(p));\n const textSignatures = textToReorderParagraphs.map((p) =>\n listSpecialChars(p)\n );\n\n // 2. For every paragraph in the text to reorder, find the *first* base\n // paragraph it matches. We only allow each base paragraph to be matched\n // once. Any further identical paragraphs will be treated as \"extra\" and\n // will be positioned later on, next to their closest neighbour.\n const firstMatchIndexForBase: number[] = Array(\n baseFileParagraphs.length\n ).fill(-1);\n const paragraphMatchedBaseIdx: (number | null)[] = Array(\n textToReorderParagraphs.length\n ).fill(null);\n\n for (let i = 0; i < textToReorderParagraphs.length; i++) {\n const paragraph = textToReorderParagraphs[i];\n const sig = textSignatures[i];\n\n // exact match pass first for performance\n let foundIdx = baseFileParagraphs.findIndex(\n (baseParagraph, idx) =>\n firstMatchIndexForBase[idx] === -1 && paragraph === baseParagraph\n );\n\n if (foundIdx === -1) {\n // fallback to the signature comparison\n foundIdx = baseFileParagraphs.findIndex(\n (baseParagraph, idx) =>\n firstMatchIndexForBase[idx] === -1 &&\n paragraphMatches(paragraph, baseParagraph, sig, baseSignatures[idx])\n );\n }\n\n if (foundIdx !== -1) {\n firstMatchIndexForBase[foundIdx] = i;\n paragraphMatchedBaseIdx[i] = foundIdx;\n }\n }\n\n // 3. For the paragraphs that *didn't* get matched to a base paragraph, we\n // record the highest-numbered base paragraph that was matched *before* it\n // in the original text. The extra paragraph will later be placed right\n // after that paragraph in the final ordering.\n const insertAfterBaseIdx: number[] = Array(\n textToReorderParagraphs.length\n ).fill(-1);\n let maxBaseIdxEncountered = -1;\n\n for (let i = 0; i < textToReorderParagraphs.length; i++) {\n const matchedBase = paragraphMatchedBaseIdx[i];\n\n if (matchedBase !== null) {\n if (matchedBase > maxBaseIdxEncountered) {\n maxBaseIdxEncountered = matchedBase;\n }\n } else {\n insertAfterBaseIdx[i] = maxBaseIdxEncountered;\n }\n }\n\n // 4. Build the final, reordered list of paragraphs.\n const result: string[] = [];\n\n // Helper: quickly retrieve all indices of paragraphs that should be inserted\n // after a given base index, while keeping their original order.\n const extraParagraphsBuckets: Record<number, number[]> = {};\n insertAfterBaseIdx.forEach((afterIdx, paragraphIdx) => {\n if (afterIdx === -1) return; // will be handled later (if any)\n extraParagraphsBuckets[afterIdx] = extraParagraphsBuckets[afterIdx] || [];\n extraParagraphsBuckets[afterIdx].push(paragraphIdx);\n });\n\n for (let bIdx = 0; bIdx < baseFileParagraphs.length; bIdx++) {\n const matchedParagraphIdx = firstMatchIndexForBase[bIdx];\n\n if (matchedParagraphIdx !== -1) {\n result.push(textToReorderParagraphs[matchedParagraphIdx]);\n }\n\n if (extraParagraphsBuckets[bIdx]) {\n extraParagraphsBuckets[bIdx].forEach((pIdx) => {\n result.push(textToReorderParagraphs[pIdx]);\n });\n }\n }\n\n // Finally, if there were extra paragraphs appearing *before* any matched\n // base paragraph (insertAfterBaseIdx === -1), we prepend them to the output\n // in their original order.\n const leadingExtras: string[] = [];\n insertAfterBaseIdx.forEach((afterIdx, pIdx) => {\n if (afterIdx === -1 && paragraphMatchedBaseIdx[pIdx] === null) {\n leadingExtras.push(textToReorderParagraphs[pIdx]);\n }\n });\n\n return [...leadingExtras, ...result].join('\\n\\n');\n};\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAM,oBAAoB,SAA2B;CACnD,MAAMA,aAAuB,EAAE;CAK/B,MAAM,SAAS,KAAK,MAAM,WAAW;AAErC,MAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;EACtC,MAAM,QAAQ,OAAO;AAGrB,MAAI,IAAI,MAAM,GAAG;AACf,OAAI,MAAO,YAAW,KAAK,MAAM;AACjC;;EAQF,MAAM,kBAAkB,KAAK,MAAM,MAAM,SAAS,EAAE;EACpD,MAAM,kBAAkB,KAAK,IAAI,GAAG,kBAAkB,EAAE;AAExD,OAAK,IAAI,IAAI,GAAG,IAAI,iBAAiB,IACnC,YAAW,KAAK,OAAO;;AAI3B,QAAO;;;;;;AAOT,MAAM,oBACJ,WACA,eACA,oBACA,kBACY;AACZ,KAAI,cAAc,cAAe,QAAO;AAExC,KAAI,mBAAmB,WAAW,cAAc,OAAQ,QAAO;AAE/D,MAAK,IAAI,IAAI,GAAG,IAAI,mBAAmB,QAAQ,IAC7C,KAAI,mBAAmB,GAAG,SAAS,cAAc,GAAG,KAClD,QAAO;AAGX,QAAO;;;;;;;;AAST,MAAa,qBACX,eACA,oBACW;CAEX,MAAM,qBAAqB,iBAAiB,gBAAgB;CAC5D,MAAM,0BAA0B,iBAAiB,cAAc;CAE/D,MAAM,iBAAiB,mBAAmB,KAAK,MAAM,iBAAiB,EAAE,CAAC;CACzE,MAAM,iBAAiB,wBAAwB,KAAK,MAClD,iBAAiB,EAAE,CACpB;CAMD,MAAMC,yBAAmC,MACvC,mBAAmB,OACpB,CAAC,KAAK,GAAG;CACV,MAAMC,0BAA6C,MACjD,wBAAwB,OACzB,CAAC,KAAK,KAAK;AAEZ,MAAK,IAAI,IAAI,GAAG,IAAI,wBAAwB,QAAQ,KAAK;EACvD,MAAM,YAAY,wBAAwB;EAC1C,MAAM,MAAM,eAAe;EAG3B,IAAI,WAAW,mBAAmB,WAC/B,eAAe,QACd,uBAAuB,SAAS,MAAM,cAAc,cACvD;AAED,MAAI,aAAa,GAEf,YAAW,mBAAmB,WAC3B,eAAe,QACd,uBAAuB,SAAS,MAChC,iBAAiB,WAAW,eAAe,KAAK,eAAe,KAAK,CACvE;AAGH,MAAI,aAAa,IAAI;AACnB,0BAAuB,YAAY;AACnC,2BAAwB,KAAK;;;CAQjC,MAAMC,qBAA+B,MACnC,wBAAwB,OACzB,CAAC,KAAK,GAAG;CACV,IAAI,wBAAwB;AAE5B,MAAK,IAAI,IAAI,GAAG,IAAI,wBAAwB,QAAQ,KAAK;EACvD,MAAM,cAAc,wBAAwB;AAE5C,MAAI,gBAAgB,MAClB;OAAI,cAAc,sBAChB,yBAAwB;QAG1B,oBAAmB,KAAK;;CAK5B,MAAMC,SAAmB,EAAE;CAI3B,MAAMC,yBAAmD,EAAE;AAC3D,oBAAmB,SAAS,UAAU,iBAAiB;AACrD,MAAI,aAAa,GAAI;AACrB,yBAAuB,YAAY,uBAAuB,aAAa,EAAE;AACzE,yBAAuB,UAAU,KAAK,aAAa;GACnD;AAEF,MAAK,IAAI,OAAO,GAAG,OAAO,mBAAmB,QAAQ,QAAQ;EAC3D,MAAM,sBAAsB,uBAAuB;AAEnD,MAAI,wBAAwB,GAC1B,QAAO,KAAK,wBAAwB,qBAAqB;AAG3D,MAAI,uBAAuB,MACzB,wBAAuB,MAAM,SAAS,SAAS;AAC7C,UAAO,KAAK,wBAAwB,MAAM;IAC1C;;CAON,MAAMC,gBAA0B,EAAE;AAClC,oBAAmB,SAAS,UAAU,SAAS;AAC7C,MAAI,aAAa,MAAM,wBAAwB,UAAU,KACvD,eAAc,KAAK,wBAAwB,MAAM;GAEnD;AAEF,QAAO,CAAC,GAAG,eAAe,GAAG,OAAO,CAAC,KAAK,OAAO"}
|
package/dist/esm/watch.mjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { runParallel, watch } from "@intlayer/chokidar";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import { getAppLogger, getConfiguration } from "@intlayer/config";
|
|
3
|
+
|
|
4
|
+
//#region src/watch.ts
|
|
5
|
+
/**
|
|
6
|
+
* Get locales dictionaries .content.{json|ts|tsx|js|jsx|mjs|cjs} and build the JSON dictionaries in the .intlayer directory.
|
|
7
|
+
* Watch mode available to get the change in the .content.{json|ts|tsx|js|jsx|mjs|cjs}
|
|
8
|
+
*/
|
|
6
9
|
const watchContentDeclaration = async (options) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
persistent: true,
|
|
15
|
-
skipPrepare: options?.skipPrepare ?? false
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
export {
|
|
19
|
-
watchContentDeclaration
|
|
10
|
+
const appLogger = getAppLogger(getConfiguration(options?.configOptions));
|
|
11
|
+
if (options?.with) runParallel(options.with);
|
|
12
|
+
appLogger("Watching Intlayer content declarations");
|
|
13
|
+
watch({
|
|
14
|
+
persistent: true,
|
|
15
|
+
skipPrepare: options?.skipPrepare ?? false
|
|
16
|
+
});
|
|
20
17
|
};
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { watchContentDeclaration };
|
|
21
21
|
//# sourceMappingURL=watch.mjs.map
|
package/dist/esm/watch.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/watch.ts"],"sourcesContent":["import { runParallel, watch } from '@intlayer/chokidar';\nimport {\n
|
|
1
|
+
{"version":3,"file":"watch.mjs","names":[],"sources":["../../src/watch.ts"],"sourcesContent":["import { runParallel, watch } from '@intlayer/chokidar';\nimport {\n type GetConfigurationOptions,\n getAppLogger,\n getConfiguration,\n} from '@intlayer/config';\n\ntype WatchOptions = {\n skipPrepare?: boolean;\n with?: string | string[];\n configOptions?: GetConfigurationOptions;\n};\n\n/**\n * Get locales dictionaries .content.{json|ts|tsx|js|jsx|mjs|cjs} and build the JSON dictionaries in the .intlayer directory.\n * Watch mode available to get the change in the .content.{json|ts|tsx|js|jsx|mjs|cjs}\n */\nexport const watchContentDeclaration = async (options?: WatchOptions) => {\n const config = getConfiguration(options?.configOptions);\n const appLogger = getAppLogger(config);\n\n if (options?.with) {\n runParallel(options.with);\n }\n\n appLogger('Watching Intlayer content declarations');\n\n watch({\n persistent: true,\n skipPrepare: options?.skipPrepare ?? false,\n });\n};\n"],"mappings":";;;;;;;;AAiBA,MAAa,0BAA0B,OAAO,YAA2B;CAEvE,MAAM,YAAY,aADH,iBAAiB,SAAS,cAAc,CACjB;AAEtC,KAAI,SAAS,KACX,aAAY,QAAQ,KAAK;AAG3B,WAAU,yCAAyC;AAEnD,OAAM;EACJ,YAAY;EACZ,aAAa,SAAS,eAAe;EACtC,CAAC"}
|