@intlayer/engine 9.5.10 → 9.5.12

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 (266) hide show
  1. package/dist/assets/installSkills/skills/bundle-optimization.md +123 -0
  2. package/dist/assets/installSkills/skills/compat.md +0 -4
  3. package/dist/assets/installSkills/skills/content.md +8 -2
  4. package/dist/assets/installSkills/skills/next-js.md +2 -2
  5. package/dist/assets/installSkills/skills/usage.md +1 -1
  6. package/dist/cjs/build.cjs +5 -0
  7. package/dist/cjs/cli.cjs +1 -0
  8. package/dist/cjs/docReview/alignBlocks.cjs +53 -36
  9. package/dist/cjs/docReview/alignBlocks.cjs.map +1 -1
  10. package/dist/cjs/docReview/rebuildDocument.cjs +3 -1
  11. package/dist/cjs/docReview/rebuildDocument.cjs.map +1 -1
  12. package/dist/cjs/docReview/segmentDocument.cjs +8 -5
  13. package/dist/cjs/docReview/segmentDocument.cjs.map +1 -1
  14. package/dist/cjs/init/frameworkSetup/nextAppRouter/transforms.cjs +4 -1
  15. package/dist/cjs/init/frameworkSetup/nextAppRouter/transforms.cjs.map +1 -1
  16. package/dist/cjs/init/index.cjs +2 -12
  17. package/dist/cjs/init/index.cjs.map +1 -1
  18. package/dist/cjs/init/utils/backendPackages.cjs +15 -0
  19. package/dist/cjs/init/utils/backendPackages.cjs.map +1 -0
  20. package/dist/cjs/init/utils/index.cjs +2 -3
  21. package/dist/cjs/installMCP/installMCP.cjs +1 -1
  22. package/dist/cjs/installMCP/installMCP.cjs.map +1 -1
  23. package/dist/cjs/installSkills/index.cjs +2 -0
  24. package/dist/cjs/installSkills/index.cjs.map +1 -1
  25. package/dist/cjs/loadDictionaries/loadDictionaries.cjs +9 -8
  26. package/dist/cjs/loadDictionaries/loadDictionaries.cjs.map +1 -1
  27. package/dist/cjs/loadDictionaries/loadLocalDictionaries.cjs +3 -1
  28. package/dist/cjs/loadDictionaries/loadLocalDictionaries.cjs.map +1 -1
  29. package/dist/cjs/loadDictionaries/loadRemoteDictionaries.cjs +4 -6
  30. package/dist/cjs/loadDictionaries/loadRemoteDictionaries.cjs.map +1 -1
  31. package/dist/cjs/loadDictionaries/log.cjs +1 -1
  32. package/dist/cjs/loadDictionaries/log.cjs.map +1 -1
  33. package/dist/cjs/loadDictionaries/logTypeScriptErrors.cjs +19 -12
  34. package/dist/cjs/loadDictionaries/logTypeScriptErrors.cjs.map +1 -1
  35. package/dist/cjs/loadDictionaries/remoteDictionaryClient.cjs +18 -0
  36. package/dist/cjs/loadDictionaries/remoteDictionaryClient.cjs.map +1 -0
  37. package/dist/cjs/prepareIntlayerServer.cjs +132 -0
  38. package/dist/cjs/prepareIntlayerServer.cjs.map +1 -0
  39. package/dist/cjs/scan/analyzeBundleContent.cjs +26 -12
  40. package/dist/cjs/scan/analyzeBundleContent.cjs.map +1 -1
  41. package/dist/cjs/scan/calculateScore.cjs +4 -1
  42. package/dist/cjs/scan/calculateScore.cjs.map +1 -1
  43. package/dist/cjs/scan/checks.cjs +394 -221
  44. package/dist/cjs/scan/checks.cjs.map +1 -1
  45. package/dist/cjs/scan/detection/checkDetails.cjs +56 -0
  46. package/dist/cjs/scan/detection/checkDetails.cjs.map +1 -0
  47. package/dist/cjs/scan/detection/classifyInternalLinks.cjs +47 -0
  48. package/dist/cjs/scan/detection/classifyInternalLinks.cjs.map +1 -0
  49. package/dist/cjs/scan/detection/detectRoutingStrategy.cjs +143 -0
  50. package/dist/cjs/scan/detection/detectRoutingStrategy.cjs.map +1 -0
  51. package/dist/cjs/scan/detection/detectTechnologies.cjs +89 -0
  52. package/dist/cjs/scan/detection/detectTechnologies.cjs.map +1 -0
  53. package/dist/cjs/scan/detection/index.cjs +36 -0
  54. package/dist/cjs/scan/detection/localeCode.cjs +118 -0
  55. package/dist/cjs/scan/detection/localeCode.cjs.map +1 -0
  56. package/dist/cjs/scan/detection/localizedPages.cjs +48 -0
  57. package/dist/cjs/scan/detection/localizedPages.cjs.map +1 -0
  58. package/dist/cjs/scan/detection/technologySignatures.cjs +564 -0
  59. package/dist/cjs/scan/detection/technologySignatures.cjs.map +1 -0
  60. package/dist/cjs/scan/detection/url.cjs +53 -0
  61. package/dist/cjs/scan/detection/url.cjs.map +1 -0
  62. package/dist/cjs/scan/fetchText.cjs +56 -0
  63. package/dist/cjs/scan/fetchText.cjs.map +1 -0
  64. package/dist/cjs/scan/index.cjs +59 -3
  65. package/dist/cjs/scan/parseHtml.cjs +55 -10
  66. package/dist/cjs/scan/parseHtml.cjs.map +1 -1
  67. package/dist/cjs/scan/robots.cjs +65 -0
  68. package/dist/cjs/scan/robots.cjs.map +1 -0
  69. package/dist/cjs/scan/runScanChecks.cjs +89 -0
  70. package/dist/cjs/scan/runScanChecks.cjs.map +1 -0
  71. package/dist/cjs/scan/scanWebsite.cjs +58 -52
  72. package/dist/cjs/scan/scanWebsite.cjs.map +1 -1
  73. package/dist/cjs/scan/sitemap.cjs +109 -0
  74. package/dist/cjs/scan/sitemap.cjs.map +1 -0
  75. package/dist/cjs/utils/chunkJSON.cjs +24 -9
  76. package/dist/cjs/utils/chunkJSON.cjs.map +1 -1
  77. package/dist/cjs/utils/getChunk.cjs +2 -2
  78. package/dist/cjs/utils/getChunk.cjs.map +1 -1
  79. package/dist/cjs/utils/getProcessChainCommands.cjs +117 -0
  80. package/dist/cjs/utils/getProcessChainCommands.cjs.map +1 -0
  81. package/dist/cjs/utils/index.cjs +2 -0
  82. package/dist/cjs/utils/runParallel/index.cjs +1 -1
  83. package/dist/cjs/utils/runParallel/index.cjs.map +1 -1
  84. package/dist/cjs/utils/runParallel/pidTree.cjs +23 -11
  85. package/dist/cjs/utils/runParallel/pidTree.cjs.map +1 -1
  86. package/dist/cjs/utils/runParallel/ps.cjs +8 -3
  87. package/dist/cjs/utils/runParallel/ps.cjs.map +1 -1
  88. package/dist/cjs/utils/runParallel/runTask.cjs +2 -1
  89. package/dist/cjs/utils/runParallel/runTask.cjs.map +1 -1
  90. package/dist/cjs/utils/runParallel/wmic.cjs +8 -3
  91. package/dist/cjs/utils/runParallel/wmic.cjs.map +1 -1
  92. package/dist/cjs/utils/startContentWatcher.cjs +99 -0
  93. package/dist/cjs/utils/startContentWatcher.cjs.map +1 -0
  94. package/dist/cjs/watcher.cjs +2 -2
  95. package/dist/cjs/watcher.cjs.map +1 -1
  96. package/dist/cjs/writeContentDeclaration/detectExportedComponentName.cjs +7 -4
  97. package/dist/cjs/writeContentDeclaration/detectExportedComponentName.cjs.map +1 -1
  98. package/dist/cjs/writeContentDeclaration/writeMarkdownFile.cjs +2 -2
  99. package/dist/cjs/writeContentDeclaration/writeMarkdownFile.cjs.map +1 -1
  100. package/dist/cjs/writeJsonIfChanged.cjs.map +1 -1
  101. package/dist/esm/build.mjs +2 -1
  102. package/dist/esm/cli.mjs +2 -2
  103. package/dist/esm/docReview/alignBlocks.mjs +53 -36
  104. package/dist/esm/docReview/alignBlocks.mjs.map +1 -1
  105. package/dist/esm/docReview/rebuildDocument.mjs +3 -1
  106. package/dist/esm/docReview/rebuildDocument.mjs.map +1 -1
  107. package/dist/esm/docReview/segmentDocument.mjs +8 -5
  108. package/dist/esm/docReview/segmentDocument.mjs.map +1 -1
  109. package/dist/esm/init/frameworkSetup/nextAppRouter/transforms.mjs +4 -1
  110. package/dist/esm/init/frameworkSetup/nextAppRouter/transforms.mjs.map +1 -1
  111. package/dist/esm/init/index.mjs +1 -11
  112. package/dist/esm/init/index.mjs.map +1 -1
  113. package/dist/esm/init/utils/backendPackages.mjs +14 -0
  114. package/dist/esm/init/utils/backendPackages.mjs.map +1 -0
  115. package/dist/esm/init/utils/index.mjs +2 -2
  116. package/dist/esm/installMCP/installMCP.mjs +1 -1
  117. package/dist/esm/installMCP/installMCP.mjs.map +1 -1
  118. package/dist/esm/installSkills/index.mjs +2 -0
  119. package/dist/esm/installSkills/index.mjs.map +1 -1
  120. package/dist/esm/loadDictionaries/loadDictionaries.mjs +9 -8
  121. package/dist/esm/loadDictionaries/loadDictionaries.mjs.map +1 -1
  122. package/dist/esm/loadDictionaries/loadLocalDictionaries.mjs +3 -1
  123. package/dist/esm/loadDictionaries/loadLocalDictionaries.mjs.map +1 -1
  124. package/dist/esm/loadDictionaries/loadRemoteDictionaries.mjs +3 -5
  125. package/dist/esm/loadDictionaries/loadRemoteDictionaries.mjs.map +1 -1
  126. package/dist/esm/loadDictionaries/log.mjs +1 -1
  127. package/dist/esm/loadDictionaries/log.mjs.map +1 -1
  128. package/dist/esm/loadDictionaries/logTypeScriptErrors.mjs +19 -12
  129. package/dist/esm/loadDictionaries/logTypeScriptErrors.mjs.map +1 -1
  130. package/dist/esm/loadDictionaries/remoteDictionaryClient.mjs +16 -0
  131. package/dist/esm/loadDictionaries/remoteDictionaryClient.mjs.map +1 -0
  132. package/dist/esm/prepareIntlayerServer.mjs +128 -0
  133. package/dist/esm/prepareIntlayerServer.mjs.map +1 -0
  134. package/dist/esm/scan/analyzeBundleContent.mjs +26 -12
  135. package/dist/esm/scan/analyzeBundleContent.mjs.map +1 -1
  136. package/dist/esm/scan/calculateScore.mjs +4 -1
  137. package/dist/esm/scan/calculateScore.mjs.map +1 -1
  138. package/dist/esm/scan/checks.mjs +387 -220
  139. package/dist/esm/scan/checks.mjs.map +1 -1
  140. package/dist/esm/scan/detection/checkDetails.mjs +54 -0
  141. package/dist/esm/scan/detection/checkDetails.mjs.map +1 -0
  142. package/dist/esm/scan/detection/classifyInternalLinks.mjs +46 -0
  143. package/dist/esm/scan/detection/classifyInternalLinks.mjs.map +1 -0
  144. package/dist/esm/scan/detection/detectRoutingStrategy.mjs +141 -0
  145. package/dist/esm/scan/detection/detectRoutingStrategy.mjs.map +1 -0
  146. package/dist/esm/scan/detection/detectTechnologies.mjs +87 -0
  147. package/dist/esm/scan/detection/detectTechnologies.mjs.map +1 -0
  148. package/dist/esm/scan/detection/index.mjs +10 -0
  149. package/dist/esm/scan/detection/localeCode.mjs +108 -0
  150. package/dist/esm/scan/detection/localeCode.mjs.map +1 -0
  151. package/dist/esm/scan/detection/localizedPages.mjs +46 -0
  152. package/dist/esm/scan/detection/localizedPages.mjs.map +1 -0
  153. package/dist/esm/scan/detection/technologySignatures.mjs +563 -0
  154. package/dist/esm/scan/detection/technologySignatures.mjs.map +1 -0
  155. package/dist/esm/scan/detection/url.mjs +47 -0
  156. package/dist/esm/scan/detection/url.mjs.map +1 -0
  157. package/dist/esm/scan/fetchText.mjs +55 -0
  158. package/dist/esm/scan/fetchText.mjs.map +1 -0
  159. package/dist/esm/scan/index.mjs +15 -3
  160. package/dist/esm/scan/parseHtml.mjs +52 -11
  161. package/dist/esm/scan/parseHtml.mjs.map +1 -1
  162. package/dist/esm/scan/robots.mjs +63 -0
  163. package/dist/esm/scan/robots.mjs.map +1 -0
  164. package/dist/esm/scan/runScanChecks.mjs +88 -0
  165. package/dist/esm/scan/runScanChecks.mjs.map +1 -0
  166. package/dist/esm/scan/scanWebsite.mjs +59 -53
  167. package/dist/esm/scan/scanWebsite.mjs.map +1 -1
  168. package/dist/esm/scan/sitemap.mjs +105 -0
  169. package/dist/esm/scan/sitemap.mjs.map +1 -0
  170. package/dist/esm/utils/chunkJSON.mjs +24 -9
  171. package/dist/esm/utils/chunkJSON.mjs.map +1 -1
  172. package/dist/esm/utils/getChunk.mjs +2 -2
  173. package/dist/esm/utils/getChunk.mjs.map +1 -1
  174. package/dist/esm/utils/getProcessChainCommands.mjs +115 -0
  175. package/dist/esm/utils/getProcessChainCommands.mjs.map +1 -0
  176. package/dist/esm/utils/index.mjs +2 -1
  177. package/dist/esm/utils/runParallel/index.mjs +1 -1
  178. package/dist/esm/utils/runParallel/index.mjs.map +1 -1
  179. package/dist/esm/utils/runParallel/pidTree.mjs +23 -11
  180. package/dist/esm/utils/runParallel/pidTree.mjs.map +1 -1
  181. package/dist/esm/utils/runParallel/ps.mjs +8 -3
  182. package/dist/esm/utils/runParallel/ps.mjs.map +1 -1
  183. package/dist/esm/utils/runParallel/runTask.mjs +2 -1
  184. package/dist/esm/utils/runParallel/runTask.mjs.map +1 -1
  185. package/dist/esm/utils/runParallel/wmic.mjs +8 -3
  186. package/dist/esm/utils/runParallel/wmic.mjs.map +1 -1
  187. package/dist/esm/utils/startContentWatcher.mjs +98 -0
  188. package/dist/esm/utils/startContentWatcher.mjs.map +1 -0
  189. package/dist/esm/watcher.mjs +1 -1
  190. package/dist/esm/watcher.mjs.map +1 -1
  191. package/dist/esm/writeContentDeclaration/detectExportedComponentName.mjs +7 -4
  192. package/dist/esm/writeContentDeclaration/detectExportedComponentName.mjs.map +1 -1
  193. package/dist/esm/writeContentDeclaration/writeMarkdownFile.mjs +2 -2
  194. package/dist/esm/writeContentDeclaration/writeMarkdownFile.mjs.map +1 -1
  195. package/dist/esm/writeJsonIfChanged.mjs +1 -1
  196. package/dist/esm/writeJsonIfChanged.mjs.map +1 -1
  197. package/dist/types/build.d.ts +2 -1
  198. package/dist/types/cli.d.ts +2 -2
  199. package/dist/types/docReview/rebuildDocument.d.ts.map +1 -1
  200. package/dist/types/docReview/segmentDocument.d.ts.map +1 -1
  201. package/dist/types/init/frameworkSetup/nextAppRouter/transforms.d.ts.map +1 -1
  202. package/dist/types/init/index.d.ts +1 -1
  203. package/dist/types/init/index.d.ts.map +1 -1
  204. package/dist/types/init/utils/backendPackages.d.ts +5 -0
  205. package/dist/types/init/utils/backendPackages.d.ts.map +1 -0
  206. package/dist/types/init/utils/index.d.ts +2 -2
  207. package/dist/types/installSkills/index.d.ts +1 -0
  208. package/dist/types/installSkills/index.d.ts.map +1 -1
  209. package/dist/types/loadDictionaries/loadRemoteDictionaries.d.ts.map +1 -1
  210. package/dist/types/loadDictionaries/logTypeScriptErrors.d.ts.map +1 -1
  211. package/dist/types/loadDictionaries/remoteDictionaryClient.d.ts +14 -0
  212. package/dist/types/loadDictionaries/remoteDictionaryClient.d.ts.map +1 -0
  213. package/dist/types/prepareIntlayerServer.d.ts +67 -0
  214. package/dist/types/prepareIntlayerServer.d.ts.map +1 -0
  215. package/dist/types/scan/analyzeBundleContent.d.ts.map +1 -1
  216. package/dist/types/scan/calculateScore.d.ts +4 -1
  217. package/dist/types/scan/calculateScore.d.ts.map +1 -1
  218. package/dist/types/scan/checks.d.ts +114 -21
  219. package/dist/types/scan/checks.d.ts.map +1 -1
  220. package/dist/types/scan/detection/checkDetails.d.ts +29 -0
  221. package/dist/types/scan/detection/checkDetails.d.ts.map +1 -0
  222. package/dist/types/scan/detection/classifyInternalLinks.d.ts +44 -0
  223. package/dist/types/scan/detection/classifyInternalLinks.d.ts.map +1 -0
  224. package/dist/types/scan/detection/detectRoutingStrategy.d.ts +60 -0
  225. package/dist/types/scan/detection/detectRoutingStrategy.d.ts.map +1 -0
  226. package/dist/types/scan/detection/detectTechnologies.d.ts +60 -0
  227. package/dist/types/scan/detection/detectTechnologies.d.ts.map +1 -0
  228. package/dist/types/scan/detection/index.d.ts +9 -0
  229. package/dist/types/scan/detection/localeCode.d.ts +49 -0
  230. package/dist/types/scan/detection/localeCode.d.ts.map +1 -0
  231. package/dist/types/scan/detection/localizedPages.d.ts +40 -0
  232. package/dist/types/scan/detection/localizedPages.d.ts.map +1 -0
  233. package/dist/types/scan/detection/technologySignatures.d.ts +48 -0
  234. package/dist/types/scan/detection/technologySignatures.d.ts.map +1 -0
  235. package/dist/types/scan/detection/url.d.ts +27 -0
  236. package/dist/types/scan/detection/url.d.ts.map +1 -0
  237. package/dist/types/scan/fetchText.d.ts +30 -0
  238. package/dist/types/scan/fetchText.d.ts.map +1 -0
  239. package/dist/types/scan/index.d.ts +16 -4
  240. package/dist/types/scan/parseHtml.d.ts +17 -0
  241. package/dist/types/scan/parseHtml.d.ts.map +1 -1
  242. package/dist/types/scan/robots.d.ts +23 -0
  243. package/dist/types/scan/robots.d.ts.map +1 -0
  244. package/dist/types/scan/runScanChecks.d.ts +43 -0
  245. package/dist/types/scan/runScanChecks.d.ts.map +1 -0
  246. package/dist/types/scan/scanWebsite.d.ts.map +1 -1
  247. package/dist/types/scan/sitemap.d.ts +49 -0
  248. package/dist/types/scan/sitemap.d.ts.map +1 -0
  249. package/dist/types/scan/types.d.ts +15 -0
  250. package/dist/types/scan/types.d.ts.map +1 -1
  251. package/dist/types/utils/chunkJSON.d.ts.map +1 -1
  252. package/dist/types/utils/getProcessChainCommands.d.ts +35 -0
  253. package/dist/types/utils/getProcessChainCommands.d.ts.map +1 -0
  254. package/dist/types/utils/index.d.ts +2 -1
  255. package/dist/types/utils/runParallel/pidTree.d.ts.map +1 -1
  256. package/dist/types/utils/startContentWatcher.d.ts +40 -0
  257. package/dist/types/utils/startContentWatcher.d.ts.map +1 -0
  258. package/dist/types/watcher.d.ts.map +1 -1
  259. package/dist/types/writeContentDeclaration/writeMarkdownFile.d.ts.map +1 -1
  260. package/package.json +15 -7
  261. package/dist/cjs/init/utils/devScript.cjs +0 -57
  262. package/dist/cjs/init/utils/devScript.cjs.map +0 -1
  263. package/dist/esm/init/utils/devScript.mjs +0 -55
  264. package/dist/esm/init/utils/devScript.mjs.map +0 -1
  265. package/dist/types/init/utils/devScript.d.ts +0 -48
  266. package/dist/types/init/utils/devScript.d.ts.map +0 -1
@@ -0,0 +1,123 @@
1
+ ---
2
+ name: intlayer-bundle-optimization
3
+ description: Keeps Intlayer dictionaries small in production bundles (purge, minify, import modes). Use when the user asks to "reduce bundle size", "optimize dictionaries", fix "cannot be purged or minified" / "Opaque field" build warnings, or configure `build.optimize`, `build.purge`, `build.minify` or `importMode`.
4
+ metadata:
5
+ author: Intlayer
6
+ url: https://intlayer.org
7
+ license: Apache-2.0
8
+ category: productivity
9
+ tags: [i18n, performance, bundle]
10
+ documentation: https://intlayer.org/doc/concept/bundle-optimization
11
+ support: contact@intlayer.org
12
+ ---
13
+
14
+ # Intlayer Bundle Optimization
15
+
16
+ [Doc](https://intlayer.org/doc/concept/bundle-optimization.md)
17
+
18
+ ## Configuration
19
+
20
+ ```typescript fileName="intlayer.config.ts"
21
+ import type { IntlayerConfig } from "intlayer";
22
+
23
+ const config: IntlayerConfig = {
24
+ dictionary: {
25
+ importMode: "dynamic", // 'static' (default) | 'dynamic' | 'fetch'
26
+ },
27
+ build: {
28
+ optimize: undefined, // auto: enabled in production builds only
29
+ purge: true, // drop fields never read in source code
30
+ minify: true, // rename field keys to short aliases (title → a)
31
+ chunkGrouping: true, // dynamic mode: one dictionary chunk per split boundary
32
+ dictionariesPreload: true, // dynamic mode: fetch content with its chunk
33
+ },
34
+ };
35
+
36
+ export default config;
37
+ ```
38
+
39
+ - `purge` and `minify` do nothing when `optimize` is `false`.
40
+ - `minify` skips key renaming while `editor.enabled` is `true`, and for `importMode: 'fetch'` dictionaries.
41
+ - `importMode: 'dynamic'` ships only the current locale's JSON; it can also be set per dictionary.
42
+
43
+ ## Dictionaries Follow Their Chunk
44
+
45
+ The build optimization rewrites each `useIntlayer('key')` / `getIntlayer('key')` into a direct import of that dictionary, then empties the global dictionary registry:
46
+
47
+ - A dictionary is bundled with the component that reads it, so a lazy-loaded route only ships its own content.
48
+
49
+ With `importMode: 'dynamic'`, `vite-intlayer` adds two more build plugins (build only, not in dev):
50
+
51
+ - `intlayerChunk` (`build.chunkGrouping`, default `true`) merges the per-dictionary, per-locale chunks by the code-split boundary that uses them (`React.lazy`, split route). Each boundary loads its content in one request per locale. Dictionaries used by several boundaries go to a shared chunk.
52
+ - `intlayerPreload` (`build.dictionariesPreload`, default `true`) starts the dictionary fetch when its chunk is loaded, instead of when the component renders, so navigation does not flash a loading state.
53
+
54
+ Keep dictionaries scoped to their component or page. A dictionary read from many routes ends up in a shared chunk.
55
+
56
+ ## Write Code the Analyzer Can Follow
57
+
58
+ Purge and minify rely on a static analysis of every `useIntlayer` / `getIntlayer` call. When a read cannot be followed, the build keeps the whole field or dictionary and warns:
59
+
60
+ - `Dictionary <key> cannot be purged or minified`: the content object escapes.
61
+ - `Dictionary <key> partially minified. Opaque field: '<path>'`: a field is read with a dynamic key.
62
+
63
+ ### Read fields by name
64
+
65
+ ```tsx
66
+ // ✅ Destructure or use dot access
67
+ const { title, description } = useIntlayer("post");
68
+ ```
69
+
70
+ ### Use `select()` instead of dynamic keys
71
+
72
+ ```tsx
73
+ // ❌ Opaque field: 'statuses' is kept whole, keys not minified
74
+ const { statuses } = useIntlayer("post");
75
+ <p>{statuses[post.status]}</p>;
76
+
77
+ // ✅ post.content.ts: publishStatus: select({ draft: "…", published: "…" })
78
+ const { publishStatus } = useIntlayer("post");
79
+ <p>{publishStatus(post.status)}</p>;
80
+ ```
81
+
82
+ Same for numbers (`enu()`), booleans (`cond()`) and genders (`gender()`).
83
+
84
+ ### Call `useIntlayer` in each component
85
+
86
+ ```tsx
87
+ // ❌ Passing content as a prop: the whole dictionary is kept
88
+ const Post = () => {
89
+ const content = useIntlayer("post");
90
+ return <PostFooter content={content} />;
91
+ };
92
+
93
+ // ✅ Each component reads its own fields; the dictionary is loaded once
94
+ const PostFooter = () => {
95
+ const { footer } = useIntlayer("post");
96
+ return <footer>{footer}</footer>;
97
+ };
98
+ ```
99
+
100
+ ### Don't let the content object escape
101
+
102
+ Spreading (`{ ...content }`), iterating (`Object.entries(content)`) or passing it to a helper (`format(content)`) keeps the whole dictionary.
103
+
104
+ ## Accepted Exceptions
105
+
106
+ Some framework APIs need the content as a value, for example a TanStack Start route that loads it in `loader` and reads it in `head`:
107
+
108
+ ```tsx
109
+ loader: async ({ params }) => ({
110
+ content: await getIntlayerAsync("admin-metadata", params.locale),
111
+ }),
112
+ head: ({ loaderData }) => ({
113
+ meta: [{ title: loaderData?.content.title }],
114
+ }),
115
+ ```
116
+
117
+ The analyzer cannot follow `loaderData`, so this dictionary stays whole and the warning is expected. Keep such dictionaries dedicated and small (e.g. a `*-metadata` dictionary holding only the page metadata) so the rest of the content is still optimized.
118
+
119
+ ## References
120
+
121
+ - [Bundle Optimization](https://intlayer.org/doc/concept/bundle-optimization.md)
122
+ - [Select](https://intlayer.org/doc/concept/content/select.md)
123
+ - [Configuration](https://intlayer.org/doc/concept/configuration.md)
@@ -80,8 +80,4 @@ Once the app runs on Intlayer, components can be moved progressively to the nati
80
80
  - [Lingui](https://intlayer.org/doc/compatibility/lingui.md)
81
81
  - [NuxtJS I18n](https://intlayer.org/doc/compatibility/nuxtjs-i18n.md)
82
82
  - [NGX Translate](https://intlayer.org/doc/compatibility/ngx-translate.md)
83
- - [Transloco](https://intlayer.org/doc/compatibility/transloco.md)
84
83
  - [Svelte I18n](https://intlayer.org/doc/compatibility/svelte-i18n.md)
85
- - [Next Translate](https://intlayer.org/doc/compatibility/next-translate.md)
86
- - [Polyglot.js](https://intlayer.org/doc/compatibility/polyglot.md)
87
- - [i18n-js](https://intlayer.org/doc/compatibility/i18n-js.md)
@@ -210,7 +210,7 @@ const publishStatus = select({
210
210
  // Usage: publishStatus(post.status)
211
211
  ```
212
212
 
213
- > Prefer `select()` over indexing a plain object (`content[status]`): dynamic property access prevents the compiler from pruning and minifying the content.
213
+ > Prefer `select()` over indexing a plain object (`content.statuses[status]`): dynamic property access prevents the compiler from pruning and minifying the content, and the build warns `Opaque field`.
214
214
 
215
215
  ### Choosing a node by discriminant
216
216
 
@@ -304,6 +304,7 @@ import {
304
304
  html,
305
305
  md,
306
306
  nest,
307
+ select,
307
308
  t,
308
309
  type Dictionary,
309
310
  } from "intlayer";
@@ -312,7 +313,7 @@ const content = {
312
313
  key: "test",
313
314
  title: "Test component content",
314
315
  description:
315
- "Content declarations for the Test component, including examples of plurals, conditions, gender-specific messages, dynamic insertions, markdown, file-based content and nested dictionaries used for demonstration and testing purposes.",
316
+ "Content declarations for the Test component, including examples of plurals, conditions, gender-specific messages, string-based selections, dynamic insertions, markdown, file-based content and nested dictionaries used for demonstration and testing purposes.",
316
317
  content: {
317
318
  baseContent: "Intlayer", // Content that no need to be i18n
318
319
  welcomeMessage: t({
@@ -342,6 +343,11 @@ const content = {
342
343
  female: "my content for female users",
343
344
  fallback: "my content when gender is not specified", // Optional but avoid undefined type
344
345
  }),
346
+ mySelect: select({
347
+ draft: "my content when the status is draft",
348
+ published: "my content when the status is published",
349
+ fallback: "my content for any other status", // Optional but avoid undefined type
350
+ }),
345
351
  myInsertion: insert(
346
352
  "Hello, my name is {{name}} and I am {{age}} years old!"
347
353
  ),
@@ -90,8 +90,8 @@ The [Intlayer Compiler](https://intlayer.org/doc/compiler.md) can extract all yo
90
90
  - [Next.js 14](https://intlayer.org/doc/environment/nextjs/14.md)
91
91
  - [Next.js 15](https://intlayer.org/doc/environment/nextjs/15.md)
92
92
  - [Next.js with Page Router](https://intlayer.org/doc/environment/nextjs/next-with-page-router.md)
93
- - [Intlayer with next-intl](https://intlayer.org/doc/next-intl.md)
94
- - [Intlayer with next-i18next](https://intlayer.org/doc/next-i18next.md)
93
+ - [Intlayer with next-intl](https://intlayer.org/doc/migration/next-intl.md)
94
+ - [Intlayer with next-i18next](https://intlayer.org/doc/migration/next-i18next.md)
95
95
 
96
96
  ### Concepts
97
97
 
@@ -19,7 +19,7 @@ To use Intlayer effectively:
19
19
  1. **Retrieve Locales**: Check `intlayer.config.{ts,js,json,json5,jsonc,cjs,mjs}`, `.intlayerrc` to see the configured locales.
20
20
 
21
21
  2. **Declare Content**:
22
- We recommend creating one content declaration file per component, located alongside the component file. This keeps translations close to the code.
22
+ We recommend creating one content declaration file per component or per section of your application, located alongside the component file. This keeps translations close to the code.
23
23
 
24
24
  3. **Consume Content**: Use the provided hooks and functions to access your content.
25
25
  - [Intlayer Exports](https://intlayer.org/doc/packages/intlayer/exports.md)
@@ -19,11 +19,13 @@ const require_loadDictionaries_loadDictionaries = require('./loadDictionaries/lo
19
19
  const require_loadDictionaries_loadLocalDictionaries = require('./loadDictionaries/loadLocalDictionaries.cjs');
20
20
  const require_writeConfiguration_index = require('./writeConfiguration/index.cjs');
21
21
  const require_prepareIntlayer = require('./prepareIntlayer.cjs');
22
+ const require_prepareIntlayerServer = require('./prepareIntlayerServer.cjs');
22
23
  const require_writeContentDeclaration_detectExportedComponentName = require('./writeContentDeclaration/detectExportedComponentName.cjs');
23
24
  const require_writeContentDeclaration_transformJSFile = require('./writeContentDeclaration/transformJSFile.cjs');
24
25
  const require_writeContentDeclaration_writeJSFile = require('./writeContentDeclaration/writeJSFile.cjs');
25
26
  const require_writeContentDeclaration_writeContentDeclaration = require('./writeContentDeclaration/writeContentDeclaration.cjs');
26
27
 
28
+ exports.INTLAYER_WATCH_ENV_VAR = require_prepareIntlayerServer.INTLAYER_WATCH_ENV_VAR;
27
29
  exports.buildDictionary = require_buildIntlayerDictionary_buildIntlayerDictionary.buildDictionary;
28
30
  exports.cleanOutputDir = require_cleanOutputDir.cleanOutputDir;
29
31
  exports.createDictionaryEntryPoint = require_createDictionaryEntryPoint_createDictionaryEntryPoint.createDictionaryEntryPoint;
@@ -46,6 +48,8 @@ exports.getBuiltDynamicDictionariesPath = require_createDictionaryEntryPoint_get
46
48
  exports.getBuiltFetchDictionariesPath = require_createDictionaryEntryPoint_getBuiltFetchDictionariesPath.getBuiltFetchDictionariesPath;
47
49
  exports.getBuiltRemoteDictionariesPath = require_createDictionaryEntryPoint_getBuiltRemoteDictionariesPath.getBuiltRemoteDictionariesPath;
48
50
  exports.getBuiltUnmergedDictionariesPath = require_createDictionaryEntryPoint_getBuiltUnmergedDictionariesPath.getBuiltUnmergedDictionariesPath;
51
+ exports.getIsDevWatcherCommand = require_prepareIntlayerServer.getIsDevWatcherCommand;
52
+ exports.getIsServerDevelopment = require_prepareIntlayerServer.getIsServerDevelopment;
49
53
  exports.getTypeName = require_createType_createModuleAugmentation.getTypeName;
50
54
  exports.isCachedConfigurationUpToDate = require_writeConfiguration_index.isCachedConfigurationUpToDate;
51
55
  exports.loadContentDeclaration = require_loadDictionaries_loadContentDeclaration.loadContentDeclaration;
@@ -54,6 +58,7 @@ exports.loadDictionaries = require_loadDictionaries_loadDictionaries.loadDiction
54
58
  exports.loadLocalDictionaries = require_loadDictionaries_loadLocalDictionaries.loadLocalDictionaries;
55
59
  exports.loadRemoteDictionaries = require_loadDictionaries_loadRemoteDictionaries.loadRemoteDictionaries;
56
60
  exports.prepareIntlayer = require_prepareIntlayer.prepareIntlayer;
61
+ exports.prepareIntlayerServer = require_prepareIntlayerServer.prepareIntlayerServer;
57
62
  exports.processContentDeclaration = require_buildIntlayerDictionary_processContentDeclaration.processContentDeclaration;
58
63
  exports.readDictionariesFromDisk = require_utils_readDictionariesFromDisk.readDictionariesFromDisk;
59
64
  exports.transformJSFile = require_writeContentDeclaration_transformJSFile.transformJSFile;
package/dist/cjs/cli.cjs CHANGED
@@ -33,6 +33,7 @@ exports.detectExportedComponentName = require_writeContentDeclaration_detectExpo
33
33
  exports.detectFormatCommand = require_detectFormatCommand.detectFormatCommand;
34
34
  exports.detectPackageManager = require_init_utils_packageManager.detectPackageManager;
35
35
  exports.enableEditorInConfig = require_init_cms.enableEditorInConfig;
36
+ exports.findLockFileDir = require_init_utils_packageManager.findLockFileDir;
36
37
  exports.getContentDeclarationFileTemplate = require_getContentDeclarationFileTemplate_getContentDeclarationFileTemplate.getContentDeclarationFileTemplate;
37
38
  exports.getDefaultApplicationURL = require_init_index.getDefaultApplicationURL;
38
39
  exports.getDocumentationUrl = require_init_documentationRouter.getDocumentationUrl;
@@ -63,6 +63,7 @@ const alignBaseAndTargetBlocks = (baseBlocks, targetBlocks) => {
63
63
  const computeMatchScore = (baseIndex, targetIndex) => {
64
64
  const baseBlock = baseBlocks[baseIndex];
65
65
  const targetBlock = targetBlocks[targetIndex];
66
+ if (!baseBlock || !targetBlock) return STRUCTURAL_MISMATCH_PENALTY;
66
67
  const hasComparableHeadingDepths = baseBlock.headingDepth !== null && targetBlock.headingDepth !== null;
67
68
  if (hasComparableHeadingDepths && baseBlock.headingDepth !== targetBlock.headingDepth) return STRUCTURAL_MISMATCH_PENALTY;
68
69
  const baseBodyLength = measureBodyLength(baseBlock);
@@ -76,49 +77,65 @@ const alignBaseAndTargetBlocks = (baseBlocks, targetBlocks) => {
76
77
  return headingDepthBonus + typeBonus + lengthBonus + anchorSimilarity * 8;
77
78
  };
78
79
  for (let i = 1; i <= baseLength; i += 1) {
79
- scoreMatrix[i][0] = scoreMatrix[i - 1][0] + GAP_PENALTY;
80
- traceMatrix[i][0] = "up";
80
+ const row = scoreMatrix[i];
81
+ const prevRow = scoreMatrix[i - 1];
82
+ const traceRow = traceMatrix[i];
83
+ if (row && prevRow) row[0] = (prevRow[0] ?? 0) + GAP_PENALTY;
84
+ if (traceRow) traceRow[0] = "up";
81
85
  }
86
+ const firstRow = scoreMatrix[0];
87
+ const firstTraceRow = traceMatrix[0];
82
88
  for (let j = 1; j <= targetLength; j += 1) {
83
- scoreMatrix[0][j] = scoreMatrix[0][j - 1] + GAP_PENALTY;
84
- traceMatrix[0][j] = "left";
89
+ if (firstRow) firstRow[j] = (firstRow[j - 1] ?? 0) + GAP_PENALTY;
90
+ if (firstTraceRow) firstTraceRow[j] = "left";
85
91
  }
86
- for (let i = 1; i <= baseLength; i += 1) for (let j = 1; j <= targetLength; j += 1) {
87
- const match = scoreMatrix[i - 1][j - 1] + computeMatchScore(i - 1, j - 1);
88
- const deleteGap = scoreMatrix[i - 1][j] + GAP_PENALTY;
89
- const insertGap = scoreMatrix[i][j - 1] + GAP_PENALTY;
90
- const best = Math.max(match, deleteGap, insertGap);
91
- scoreMatrix[i][j] = best;
92
- traceMatrix[i][j] = best === match ? "diagonal" : best === deleteGap ? "up" : "left";
92
+ for (let i = 1; i <= baseLength; i += 1) {
93
+ const currentRow = scoreMatrix[i];
94
+ const prevRow = scoreMatrix[i - 1];
95
+ const currentTraceRow = traceMatrix[i];
96
+ if (!currentRow || !prevRow || !currentTraceRow) continue;
97
+ for (let j = 1; j <= targetLength; j += 1) {
98
+ const match = (prevRow[j - 1] ?? 0) + computeMatchScore(i - 1, j - 1);
99
+ const deleteGap = (prevRow[j] ?? 0) + GAP_PENALTY;
100
+ const insertGap = (currentRow[j - 1] ?? 0) + GAP_PENALTY;
101
+ const best = Math.max(match, deleteGap, insertGap);
102
+ currentRow[j] = best;
103
+ currentTraceRow[j] = best === match ? "diagonal" : best === deleteGap ? "up" : "left";
104
+ }
93
105
  }
94
106
  const result = [];
95
107
  let i = baseLength;
96
108
  let j = targetLength;
97
- while (i > 0 || j > 0) if (i > 0 && j > 0 && traceMatrix[i][j] === "diagonal") {
98
- const baseIndex = i - 1;
99
- const targetIndex = j - 1;
100
- const similarityScore = require_docReview_computeSimilarity.computeJaccardSimilarity(baseBlocks[baseIndex].anchorText, targetBlocks[targetIndex].anchorText, 3);
101
- result.unshift({
102
- baseIndex,
103
- targetIndex,
104
- similarityScore
105
- });
106
- i -= 1;
107
- j -= 1;
108
- } else if (i > 0 && (j === 0 || traceMatrix[i][j] === "up")) {
109
- result.unshift({
110
- baseIndex: i - 1,
111
- targetIndex: null,
112
- similarityScore: 0
113
- });
114
- i -= 1;
115
- } else if (j > 0 && (i === 0 || traceMatrix[i][j] === "left")) {
116
- result.unshift({
117
- baseIndex: -1,
118
- targetIndex: j - 1,
119
- similarityScore: 0
120
- });
121
- j -= 1;
109
+ while (i > 0 || j > 0) {
110
+ const direction = traceMatrix[i]?.[j];
111
+ if (i > 0 && j > 0 && direction === "diagonal") {
112
+ const baseIndex = i - 1;
113
+ const targetIndex = j - 1;
114
+ const baseAnchor = baseBlocks[baseIndex]?.anchorText ?? "";
115
+ const targetAnchor = targetBlocks[targetIndex]?.anchorText ?? "";
116
+ const similarityScore = require_docReview_computeSimilarity.computeJaccardSimilarity(baseAnchor, targetAnchor, 3);
117
+ result.unshift({
118
+ baseIndex,
119
+ targetIndex,
120
+ similarityScore
121
+ });
122
+ i -= 1;
123
+ j -= 1;
124
+ } else if (i > 0 && (j === 0 || direction === "up")) {
125
+ result.unshift({
126
+ baseIndex: i - 1,
127
+ targetIndex: null,
128
+ similarityScore: 0
129
+ });
130
+ i -= 1;
131
+ } else if (j > 0 && (i === 0 || direction === "left")) {
132
+ result.unshift({
133
+ baseIndex: -1,
134
+ targetIndex: j - 1,
135
+ similarityScore: 0
136
+ });
137
+ j -= 1;
138
+ }
122
139
  }
123
140
  return result;
124
141
  };
@@ -1 +1 @@
1
- {"version":3,"file":"alignBlocks.cjs","names":["computeJaccardSimilarity"],"sources":["../../../src/docReview/alignBlocks.ts"],"sourcesContent":["import { computeJaccardSimilarity } from './computeSimilarity';\nimport type { AlignmentPair, FingerprintedBlock } from './types';\n\n/** Cost of leaving a block unaligned (an insertion or a deletion). */\nconst GAP_PENALTY = -2;\n\n/**\n * Score of a pair that structural evidence rules out.\n *\n * Strictly below the cost of two gaps (`2 * GAP_PENALTY`) so the aligner always\n * prefers reporting an insertion plus a deletion over such a pair. Every other\n * score is positive, which means that without an explicit veto the aligner would\n * rather pair two unrelated blocks than leave them unaligned — and a bogus pair\n * is planned as `reuse`, which keeps the stale translation verbatim *and*\n * inserts the freshly translated base block next to it, producing the duplicated\n * headings this penalty exists to prevent.\n */\nconst STRUCTURAL_MISMATCH_PENALTY = GAP_PENALTY * 2 - 1;\n\n/** Reward for two blocks opened by a heading of the very same depth. */\nconst HEADING_DEPTH_MATCH_BONUS = 3;\n\n/** Minimum length ratio for the (small) \"comparable size\" reward. */\nconst COMPARABLE_LENGTH_RATIO = 0.75;\n\n/**\n * Body length from which a section is considered to carry content of its own.\n *\n * Kept above a single sentence so a translator's short lead-in paragraph is not\n * mistaken for a whole section body.\n */\nconst SUBSTANTIAL_BODY_LENGTH = 80;\n\n/**\n * Length of the body a block carries below its opening heading.\n *\n * A section that only holds its heading (because subsections carry all of its\n * content) is structurally different from one that holds a full body, and that\n * difference survives translation.\n *\n * @param block - The block to measure.\n * @returns The trimmed length of everything below the opening heading line.\n */\nconst measureBodyLength = (block: FingerprintedBlock): number => {\n if (block.headingDepth === null) return block.content.trim().length;\n\n const [, ...bodyLines] = block.content.split('\\n');\n\n return bodyLines.join('\\n').trim().length;\n};\n\n/**\n * Align the blocks of a base document with the blocks of its translation using a\n * Needleman–Wunsch global alignment over heading depth, anchor similarity and\n * block type.\n *\n * Because prose differs across languages, the score is weighted toward the\n * structural signals — heading depth first, then the anchor (digits and symbols)\n * — rather than the words themselves.\n *\n * @param baseBlocks - Blocks of the base (source) document.\n * @param targetBlocks - Blocks of the target (translated) document.\n * @returns The ordered list of alignment pairs, including insertions and deletions.\n */\nexport const alignBaseAndTargetBlocks = (\n baseBlocks: FingerprintedBlock[],\n targetBlocks: FingerprintedBlock[]\n): AlignmentPair[] => {\n const baseLength = baseBlocks.length;\n const targetLength = targetBlocks.length;\n\n const scoreMatrix: number[][] = Array.from({ length: baseLength + 1 }, () =>\n Array.from({ length: targetLength + 1 }, () => 0)\n );\n const traceMatrix: ('diagonal' | 'up' | 'left')[][] = Array.from(\n { length: baseLength + 1 },\n () => Array.from({ length: targetLength + 1 }, () => 'diagonal')\n );\n\n const computeMatchScore = (\n baseIndex: number,\n targetIndex: number\n ): number => {\n const baseBlock = baseBlocks[baseIndex]!;\n const targetBlock = targetBlocks[targetIndex]!;\n\n // Translating a document never changes its heading depths, so two headings\n // of different depths cannot be counterparts, however similar their content.\n const hasComparableHeadingDepths =\n baseBlock.headingDepth !== null && targetBlock.headingDepth !== null;\n\n if (\n hasComparableHeadingDepths &&\n baseBlock.headingDepth !== targetBlock.headingDepth\n ) {\n return STRUCTURAL_MISMATCH_PENALTY;\n }\n\n // A section reduced to its bare heading (its content living in subsections)\n // is not the translation of a section holding a full body. Pairing them\n // reuses that whole body while the base heading is translated again right\n // next to it — which is exactly how a duplicated heading appears.\n const baseBodyLength = measureBodyLength(baseBlock);\n const targetBodyLength = measureBodyLength(targetBlock);\n const isBodyPresenceMismatched =\n (baseBodyLength === 0 && targetBodyLength >= SUBSTANTIAL_BODY_LENGTH) ||\n (targetBodyLength === 0 && baseBodyLength >= SUBSTANTIAL_BODY_LENGTH);\n\n if (isBodyPresenceMismatched) return STRUCTURAL_MISMATCH_PENALTY;\n\n const lengthRatio =\n Math.min(baseBlock.content.length, targetBlock.content.length) /\n Math.max(baseBlock.content.length, targetBlock.content.length);\n\n const headingDepthBonus = hasComparableHeadingDepths\n ? HEADING_DEPTH_MATCH_BONUS\n : 0;\n const typeBonus = baseBlock.type === targetBlock.type ? 2 : 0;\n const anchorSimilarity = computeJaccardSimilarity(\n baseBlock.anchorText,\n targetBlock.anchorText,\n 3\n );\n const lengthBonus = lengthRatio > COMPARABLE_LENGTH_RATIO ? 1 : 0;\n\n // weighted toward the structural signals (heading depth, then anchor)\n return headingDepthBonus + typeBonus + lengthBonus + anchorSimilarity * 8;\n };\n\n // initialize first row and column\n for (let i = 1; i <= baseLength; i += 1) {\n scoreMatrix[i][0] = scoreMatrix[i - 1][0] + GAP_PENALTY;\n traceMatrix[i][0] = 'up';\n }\n for (let j = 1; j <= targetLength; j += 1) {\n scoreMatrix[0][j] = scoreMatrix[0][j - 1] + GAP_PENALTY;\n traceMatrix[0][j] = 'left';\n }\n\n // fill\n for (let i = 1; i <= baseLength; i += 1) {\n for (let j = 1; j <= targetLength; j += 1) {\n const match = scoreMatrix[i - 1][j - 1] + computeMatchScore(i - 1, j - 1);\n const deleteGap = scoreMatrix[i - 1][j] + GAP_PENALTY;\n const insertGap = scoreMatrix[i][j - 1] + GAP_PENALTY;\n\n const best = Math.max(match, deleteGap, insertGap);\n scoreMatrix[i][j] = best;\n traceMatrix[i][j] =\n best === match ? 'diagonal' : best === deleteGap ? 'up' : 'left';\n }\n }\n\n // traceback\n const result: AlignmentPair[] = [];\n let i = baseLength;\n let j = targetLength;\n while (i > 0 || j > 0) {\n if (i > 0 && j > 0 && traceMatrix[i][j] === 'diagonal') {\n const baseIndex = i - 1;\n const targetIndex = j - 1;\n const similarityScore = computeJaccardSimilarity(\n baseBlocks[baseIndex].anchorText,\n targetBlocks[targetIndex].anchorText,\n 3\n );\n result.unshift({ baseIndex, targetIndex, similarityScore });\n i -= 1;\n j -= 1;\n } else if (i > 0 && (j === 0 || traceMatrix[i][j] === 'up')) {\n result.unshift({\n baseIndex: i - 1,\n targetIndex: null,\n similarityScore: 0,\n });\n i -= 1;\n } else if (j > 0 && (i === 0 || traceMatrix[i][j] === 'left')) {\n // target block has no corresponding base block (deleted)\n result.unshift({\n baseIndex: -1,\n targetIndex: j - 1,\n similarityScore: 0,\n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OAAO,QAAQ;GAAE;GAAW;GAAa;EAAgB,CAAC;EAC1D,KAAK;EACL,KAAK;CACP,OAAO,IAAI,IAAI,MAAM,MAAM,KAAK,YAAY,EAAE,CAAC,OAAO,OAAO;EAC3D,OAAO,QAAQ;GACb,WAAW,IAAI;GACf,aAAa;GACb,iBAAiB;EACnB,CAAC;EACD,KAAK;CACP,OAAO,IAAI,IAAI,MAAM,MAAM,KAAK,YAAY,EAAE,CAAC,OAAO,SAAS;EAE7D,OAAO,QAAQ;GACb,WAAW;GACX,aAAa,IAAI;GACjB,iBAAiB;EACnB,CAAC;EACD,KAAK;CACP;CAEF,OAAO;AACT"}
1
+ {"version":3,"file":"alignBlocks.cjs","names":["computeJaccardSimilarity"],"sources":["../../../src/docReview/alignBlocks.ts"],"sourcesContent":["import { computeJaccardSimilarity } from './computeSimilarity';\nimport type { AlignmentPair, FingerprintedBlock } from './types';\n\n/** Cost of leaving a block unaligned (an insertion or a deletion). */\nconst GAP_PENALTY = -2;\n\n/**\n * Score of a pair that structural evidence rules out.\n *\n * Strictly below the cost of two gaps (`2 * GAP_PENALTY`) so the aligner always\n * prefers reporting an insertion plus a deletion over such a pair. Every other\n * score is positive, which means that without an explicit veto the aligner would\n * rather pair two unrelated blocks than leave them unaligned — and a bogus pair\n * is planned as `reuse`, which keeps the stale translation verbatim *and*\n * inserts the freshly translated base block next to it, producing the duplicated\n * headings this penalty exists to prevent.\n */\nconst STRUCTURAL_MISMATCH_PENALTY = GAP_PENALTY * 2 - 1;\n\n/** Reward for two blocks opened by a heading of the very same depth. */\nconst HEADING_DEPTH_MATCH_BONUS = 3;\n\n/** Minimum length ratio for the (small) \"comparable size\" reward. */\nconst COMPARABLE_LENGTH_RATIO = 0.75;\n\n/**\n * Body length from which a section is considered to carry content of its own.\n *\n * Kept above a single sentence so a translator's short lead-in paragraph is not\n * mistaken for a whole section body.\n */\nconst SUBSTANTIAL_BODY_LENGTH = 80;\n\n/**\n * Length of the body a block carries below its opening heading.\n *\n * A section that only holds its heading (because subsections carry all of its\n * content) is structurally different from one that holds a full body, and that\n * difference survives translation.\n *\n * @param block - The block to measure.\n * @returns The trimmed length of everything below the opening heading line.\n */\nconst measureBodyLength = (block: FingerprintedBlock): number => {\n if (block.headingDepth === null) return block.content.trim().length;\n\n const [, ...bodyLines] = block.content.split('\\n');\n\n return bodyLines.join('\\n').trim().length;\n};\n\n/**\n * Align the blocks of a base document with the blocks of its translation using a\n * Needleman–Wunsch global alignment over heading depth, anchor similarity and\n * block type.\n *\n * Because prose differs across languages, the score is weighted toward the\n * structural signals — heading depth first, then the anchor (digits and symbols)\n * — rather than the words themselves.\n *\n * @param baseBlocks - Blocks of the base (source) document.\n * @param targetBlocks - Blocks of the target (translated) document.\n * @returns The ordered list of alignment pairs, including insertions and deletions.\n */\nexport const alignBaseAndTargetBlocks = (\n baseBlocks: FingerprintedBlock[],\n targetBlocks: FingerprintedBlock[]\n): AlignmentPair[] => {\n const baseLength = baseBlocks.length;\n const targetLength = targetBlocks.length;\n\n const scoreMatrix: number[][] = Array.from({ length: baseLength + 1 }, () =>\n Array.from({ length: targetLength + 1 }, () => 0)\n );\n const traceMatrix: ('diagonal' | 'up' | 'left')[][] = Array.from(\n { length: baseLength + 1 },\n () => Array.from({ length: targetLength + 1 }, () => 'diagonal')\n );\n\n const computeMatchScore = (\n baseIndex: number,\n targetIndex: number\n ): number => {\n const baseBlock = baseBlocks[baseIndex];\n const targetBlock = targetBlocks[targetIndex];\n\n if (!baseBlock || !targetBlock) return STRUCTURAL_MISMATCH_PENALTY;\n\n // Translating a document never changes its heading depths, so two headings\n // of different depths cannot be counterparts, however similar their content.\n const hasComparableHeadingDepths =\n baseBlock.headingDepth !== null && targetBlock.headingDepth !== null;\n\n if (\n hasComparableHeadingDepths &&\n baseBlock.headingDepth !== targetBlock.headingDepth\n ) {\n return STRUCTURAL_MISMATCH_PENALTY;\n }\n\n // A section reduced to its bare heading (its content living in subsections)\n // is not the translation of a section holding a full body. Pairing them\n // reuses that whole body while the base heading is translated again right\n // next to it — which is exactly how a duplicated heading appears.\n const baseBodyLength = measureBodyLength(baseBlock);\n const targetBodyLength = measureBodyLength(targetBlock);\n const isBodyPresenceMismatched =\n (baseBodyLength === 0 && targetBodyLength >= SUBSTANTIAL_BODY_LENGTH) ||\n (targetBodyLength === 0 && baseBodyLength >= SUBSTANTIAL_BODY_LENGTH);\n\n if (isBodyPresenceMismatched) return STRUCTURAL_MISMATCH_PENALTY;\n\n const lengthRatio =\n Math.min(baseBlock.content.length, targetBlock.content.length) /\n Math.max(baseBlock.content.length, targetBlock.content.length);\n\n const headingDepthBonus = hasComparableHeadingDepths\n ? HEADING_DEPTH_MATCH_BONUS\n : 0;\n const typeBonus = baseBlock.type === targetBlock.type ? 2 : 0;\n const anchorSimilarity = computeJaccardSimilarity(\n baseBlock.anchorText,\n targetBlock.anchorText,\n 3\n );\n const lengthBonus = lengthRatio > COMPARABLE_LENGTH_RATIO ? 1 : 0;\n\n // weighted toward the structural signals (heading depth, then anchor)\n return headingDepthBonus + typeBonus + lengthBonus + anchorSimilarity * 8;\n };\n\n // initialize first row and column\n for (let i = 1; i <= baseLength; i += 1) {\n const row = scoreMatrix[i];\n const prevRow = scoreMatrix[i - 1];\n const traceRow = traceMatrix[i];\n\n if (row && prevRow) {\n row[0] = (prevRow[0] ?? 0) + GAP_PENALTY;\n }\n if (traceRow) {\n traceRow[0] = 'up';\n }\n }\n const firstRow = scoreMatrix[0];\n const firstTraceRow = traceMatrix[0];\n\n for (let j = 1; j <= targetLength; j += 1) {\n if (firstRow) {\n firstRow[j] = (firstRow[j - 1] ?? 0) + GAP_PENALTY;\n }\n if (firstTraceRow) {\n firstTraceRow[j] = 'left';\n }\n }\n\n // fill\n for (let i = 1; i <= baseLength; i += 1) {\n const currentRow = scoreMatrix[i];\n const prevRow = scoreMatrix[i - 1];\n const currentTraceRow = traceMatrix[i];\n\n if (!currentRow || !prevRow || !currentTraceRow) continue;\n\n for (let j = 1; j <= targetLength; j += 1) {\n const match = (prevRow[j - 1] ?? 0) + computeMatchScore(i - 1, j - 1);\n const deleteGap = (prevRow[j] ?? 0) + GAP_PENALTY;\n const insertGap = (currentRow[j - 1] ?? 0) + GAP_PENALTY;\n\n const best = Math.max(match, deleteGap, insertGap);\n currentRow[j] = best;\n currentTraceRow[j] =\n best === match ? 'diagonal' : best === deleteGap ? 'up' : 'left';\n }\n }\n\n // traceback\n const result: AlignmentPair[] = [];\n let i = baseLength;\n let j = targetLength;\n while (i > 0 || j > 0) {\n const direction = traceMatrix[i]?.[j];\n if (i > 0 && j > 0 && direction === 'diagonal') {\n const baseIndex = i - 1;\n const targetIndex = j - 1;\n const baseAnchor = baseBlocks[baseIndex]?.anchorText ?? '';\n const targetAnchor = targetBlocks[targetIndex]?.anchorText ?? '';\n const similarityScore = computeJaccardSimilarity(\n baseAnchor,\n targetAnchor,\n 3\n );\n result.unshift({ baseIndex, targetIndex, similarityScore });\n i -= 1;\n j -= 1;\n } else if (i > 0 && (j === 0 || direction === 'up')) {\n result.unshift({\n baseIndex: i - 1,\n targetIndex: null,\n similarityScore: 0,\n });\n i -= 1;\n } else if (j > 0 && (i === 0 || direction === 'left')) {\n // target block has no corresponding base block (deleted)\n result.unshift({\n baseIndex: -1,\n targetIndex: j - 1,\n similarityScore: 0,\n });\n j -= 1;\n }\n }\n return result;\n};\n"],"mappings":";;;;;AAIA,MAAM,cAAc;;;;;;;;;;;;AAapB,MAAM,8BAA8B;;AAGpC,MAAM,4BAA4B;;AAGlC,MAAM,0BAA0B;;;;;;;AAQhC,MAAM,0BAA0B;;;;;;;;;;;AAYhC,MAAM,qBAAqB,UAAsC;CAC/D,IAAI,MAAM,iBAAiB,MAAM,OAAO,MAAM,QAAQ,KAAK,CAAC,CAAC;CAE7D,MAAM,GAAG,GAAG,aAAa,MAAM,QAAQ,MAAM,IAAI;CAEjD,OAAO,UAAU,KAAK,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC;AACrC;;;;;;;;;;;;;;AAeA,MAA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K,cAAc,OAAO;GACnD,OAAO,QAAQ;IACb,WAAW,IAAI;IACf,aAAa;IACb,iBAAiB;GACnB,CAAC;GACD,KAAK;EACP,OAAO,IAAI,IAAI,MAAM,MAAM,KAAK,cAAc,SAAS;GAErD,OAAO,QAAQ;IACb,WAAW;IACX,aAAa,IAAI;IACjB,iBAAiB;GACnB,CAAC;GACD,KAAK;EACP;CACF;CACA,OAAO;AACT"}
@@ -13,7 +13,8 @@ const identifySegmentsToReview = ({ baseBlocks, targetBlocks, plan }) => {
13
13
  plan.actions.forEach((action, actionIndex) => {
14
14
  if (action.kind === "review") {
15
15
  const baseBlock = baseBlocks[action.baseIndex];
16
- const targetBlockText = action.targetIndex !== null ? targetBlocks[action.targetIndex].content : null;
16
+ if (!baseBlock) return;
17
+ const targetBlockText = action.targetIndex !== null ? targetBlocks[action.targetIndex]?.content ?? null : null;
17
18
  segmentsToReview.push({
18
19
  baseBlock,
19
20
  targetBlockText,
@@ -21,6 +22,7 @@ const identifySegmentsToReview = ({ baseBlocks, targetBlocks, plan }) => {
21
22
  });
22
23
  } else if (action.kind === "insert_new") {
23
24
  const baseBlock = baseBlocks[action.baseIndex];
25
+ if (!baseBlock) return;
24
26
  segmentsToReview.push({
25
27
  baseBlock,
26
28
  targetBlockText: null,
@@ -1 +1 @@
1
- {"version":3,"file":"rebuildDocument.cjs","names":[],"sources":["../../../src/docReview/rebuildDocument.ts"],"sourcesContent":["import type { AlignmentPlan, FingerprintedBlock } from './types';\n\n/**\n * A block that needs to be translated or re-translated by an external consumer\n * (an AI client, a human, or an agent).\n */\nexport type SegmentToReview = {\n /** The base block to translate. */\n baseBlock: FingerprintedBlock;\n /** Existing target translation, or `null` when the block is new. */\n targetBlockText: string | null;\n /** Index of the originating action within {@link AlignmentPlan.actions}. */\n actionIndex: number;\n};\n\nexport type RebuildInput = {\n baseBlocks: FingerprintedBlock[];\n targetBlocks: FingerprintedBlock[];\n plan: AlignmentPlan;\n};\n\nexport type RebuildResult = {\n segmentsToReview: SegmentToReview[];\n};\n\n/**\n * Analyze the alignment plan and return only the segments that need\n * review/translation. Does not generate output text - that is done by\n * {@link mergeReviewedSegments} once the translations are available.\n *\n * @param input - The base/target blocks and the alignment plan.\n * @returns The list of segments that require translation.\n */\nexport const identifySegmentsToReview = ({\n baseBlocks,\n targetBlocks,\n plan,\n}: RebuildInput): RebuildResult => {\n const segmentsToReview: SegmentToReview[] = [];\n\n plan.actions.forEach((action, actionIndex) => {\n if (action.kind === 'review') {\n const baseBlock = baseBlocks[action.baseIndex];\n const targetBlockText =\n action.targetIndex !== null\n ? targetBlocks[action.targetIndex].content\n : null;\n\n segmentsToReview.push({ baseBlock, targetBlockText, actionIndex });\n } else if (action.kind === 'insert_new') {\n const baseBlock = baseBlocks[action.baseIndex];\n\n segmentsToReview.push({\n baseBlock,\n targetBlockText: null,\n actionIndex,\n });\n }\n });\n\n return { segmentsToReview };\n};\n\n/** Markdown separates two blocks with a blank line. */\nconst endsWithBlockBoundary = (text: string): boolean =>\n /\\n[ \\t]*\\n$/.test(text);\n\n/**\n * The newlines missing at the end of `text` for it to close a markdown block.\n *\n * @param text - The document built so far.\n * @returns `''`, `'\\n'` or `'\\n\\n'` depending on how the text already ends.\n */\nconst buildBlockSeparator = (text: string): string => {\n if (text.length === 0 || endsWithBlockBoundary(text)) return '';\n\n return text.endsWith('\\n') ? '\\n' : '\\n\\n';\n};\n\n/**\n * Merge reviewed translations back into the final document following the\n * alignment plan, reusing untouched target blocks as-is.\n *\n * @param plan - The alignment plan.\n * @param targetBlocks - Blocks of the existing target document.\n * @param reviewedSegments - Map of action index to its reviewed translation.\n * @returns The rebuilt target document.\n */\nexport const mergeReviewedSegments = (\n plan: AlignmentPlan,\n targetBlocks: FingerprintedBlock[],\n reviewedSegments: Map<number, string>\n): string => {\n let mergedText = '';\n let previousPartWasGenerated = false;\n\n /**\n * Append one part, keeping a blank line around the content this run produced.\n *\n * Blocks own the blank lines that trail them, so two verbatim blocks already\n * separate themselves and are appended untouched — a document with nothing to\n * change merges back byte for byte. Generated content is the exception: the\n * block it lands after may be the one that used to end the document, which\n * carries no trailing blank line, and the two would be glued into a single\n * markdown block (a heading swallowed by the paragraph above it).\n */\n const appendPart = (content: string, isGenerated: boolean): void => {\n if (content.length === 0) return;\n\n if (isGenerated || previousPartWasGenerated) {\n mergedText += buildBlockSeparator(mergedText);\n }\n\n mergedText += content;\n previousPartWasGenerated = isGenerated;\n };\n\n plan.actions.forEach((action, actionIndex) => {\n if (action.kind === 'reuse') {\n appendPart(targetBlocks[action.targetIndex]!.content, false);\n } else if (action.kind === 'review' || action.kind === 'insert_new') {\n const reviewedContent = reviewedSegments.get(actionIndex);\n\n if (reviewedContent !== undefined) {\n appendPart(reviewedContent, true);\n } else {\n // Fallback: if review failed, use existing or blank\n if (action.kind === 'review' && action.targetIndex !== null) {\n appendPart(targetBlocks[action.targetIndex]!.content, false);\n } else {\n appendPart('\\n', false);\n }\n }\n } else if (action.kind === 'delete') {\n const reviewedContent = reviewedSegments.get(actionIndex);\n if (reviewedContent !== undefined) {\n // Caller explicitly resolved this block: empty string = actually delete,\n // non-empty string = replacement content.\n appendPart(reviewedContent, true);\n } else {\n // Default: keep verbatim. A target block with no base counterpart may\n // just be a section the aligner could not follow (reordering, split\n // prose) — keeping it prevents accidental data loss in log/read-only mode.\n appendPart(targetBlocks[action.targetIndex]!.content, false);\n }\n }\n });\n\n return mergedText;\n};\n"],"mappings":";;;;;;;;;;AAiCA,MAAa,4BAA4B,EACvC,YACA,cACA,WACiC;CACjC,MAAM,mBAAsC,CAAC;CAE7C,KAAK,QAAQ,SAAS,QAAQ,gBAAgB;EAC5C,IAAI,OAAO,SAAS,UAAU;GAC5B,MAAM,YAAY,WAAW,OAAO;GACpC,MAAM,kBACJ,OAAO,gBAAgB,OACnB,aAAa,OAAO,YAAY,CAAC,UACjC;GAEN,iBAAiB,KAAK;IAAE;IAAW;IAAiB;GAAY,CAAC;EACnE,OAAO,IAAI,OAAO,SAAS,cAAc;GACvC,MAAM,YAAY,WAAW,OAAO;GAEpC,iBAAiB,KAAK;IACpB;IACA,iBAAiB;IACjB;GACF,CAAC;EACH;CACF,CAAC;CAED,OAAO,EAAE,iBAAiB;AAC5B;;AAGA,MAAM,yBAAyB,SAC7B,cAAc,KAAK,IAAI;;;;;;;AAQzB,MAAM,uBAAuB,SAAyB;CACpD,IAAI,KAAK,WAAW,KAAK,sBAAsB,IAAI,GAAG,OAAO;CAE7D,OAAO,KAAK,SAAS,IAAI,IAAI,OAAO;AACtC;;;;;;;;;;AAWA,MAAa,yBACX,MACA,cACA,qBACW;CACX,IAAI,aAAa;CACjB,IAAI,2BAA2B;;;;;;;;;;;CAY/B,MAAM,cAAc,SAAiB,gBAA+B;EAClE,IAAI,QAAQ,WAAW,GAAG;EAE1B,IAAI,eAAe,0BACjB,cAAc,oBAAoB,UAAU;EAG9C,cAAc;EACd,2BAA2B;CAC7B;CAEA,KAAK,QAAQ,SAAS,QAAQ,gBAAgB;EAC5C,IAAI,OAAO,SAAS,SAClB,WAAW,aAAa,OAAO,YAAY,CAAE,SAAS,KAAK;OACtD,IAAI,OAAO,SAAS,YAAY,OAAO,SAAS,cAAc;GACnE,MAAM,kBAAkB,iBAAiB,IAAI,WAAW;GAExD,IAAI,oBAAoB,QACtB,WAAW,iBAAiB,IAAI;QAGhC,IAAI,OAAO,SAAS,YAAY,OAAO,gBAAgB,MACrD,WAAW,aAAa,OAAO,YAAY,CAAE,SAAS,KAAK;QAE3D,WAAW,MAAM,KAAK;EAG5B,OAAO,IAAI,OAAO,SAAS,UAAU;GACnC,MAAM,kBAAkB,iBAAiB,IAAI,WAAW;GACxD,IAAI,oBAAoB,QAGtB,WAAW,iBAAiB,IAAI;QAKhC,WAAW,aAAa,OAAO,YAAY,CAAE,SAAS,KAAK;EAE/D;CACF,CAAC;CAED,OAAO;AACT"}
1
+ {"version":3,"file":"rebuildDocument.cjs","names":[],"sources":["../../../src/docReview/rebuildDocument.ts"],"sourcesContent":["import type { AlignmentPlan, FingerprintedBlock } from './types';\n\n/**\n * A block that needs to be translated or re-translated by an external consumer\n * (an AI client, a human, or an agent).\n */\nexport type SegmentToReview = {\n /** The base block to translate. */\n baseBlock: FingerprintedBlock;\n /** Existing target translation, or `null` when the block is new. */\n targetBlockText: string | null;\n /** Index of the originating action within {@link AlignmentPlan.actions}. */\n actionIndex: number;\n};\n\nexport type RebuildInput = {\n baseBlocks: FingerprintedBlock[];\n targetBlocks: FingerprintedBlock[];\n plan: AlignmentPlan;\n};\n\nexport type RebuildResult = {\n segmentsToReview: SegmentToReview[];\n};\n\n/**\n * Analyze the alignment plan and return only the segments that need\n * review/translation. Does not generate output text - that is done by\n * {@link mergeReviewedSegments} once the translations are available.\n *\n * @param input - The base/target blocks and the alignment plan.\n * @returns The list of segments that require translation.\n */\nexport const identifySegmentsToReview = ({\n baseBlocks,\n targetBlocks,\n plan,\n}: RebuildInput): RebuildResult => {\n const segmentsToReview: SegmentToReview[] = [];\n\n plan.actions.forEach((action, actionIndex) => {\n if (action.kind === 'review') {\n const baseBlock = baseBlocks[action.baseIndex];\n\n if (!baseBlock) return;\n\n const targetBlockText =\n action.targetIndex !== null\n ? (targetBlocks[action.targetIndex]?.content ?? null)\n : null;\n\n segmentsToReview.push({ baseBlock, targetBlockText, actionIndex });\n } else if (action.kind === 'insert_new') {\n const baseBlock = baseBlocks[action.baseIndex];\n\n if (!baseBlock) return;\n\n segmentsToReview.push({\n baseBlock,\n targetBlockText: null,\n actionIndex,\n });\n }\n });\n\n return { segmentsToReview };\n};\n\n/** Markdown separates two blocks with a blank line. */\nconst endsWithBlockBoundary = (text: string): boolean =>\n /\\n[ \\t]*\\n$/.test(text);\n\n/**\n * The newlines missing at the end of `text` for it to close a markdown block.\n *\n * @param text - The document built so far.\n * @returns `''`, `'\\n'` or `'\\n\\n'` depending on how the text already ends.\n */\nconst buildBlockSeparator = (text: string): string => {\n if (text.length === 0 || endsWithBlockBoundary(text)) return '';\n\n return text.endsWith('\\n') ? '\\n' : '\\n\\n';\n};\n\n/**\n * Merge reviewed translations back into the final document following the\n * alignment plan, reusing untouched target blocks as-is.\n *\n * @param plan - The alignment plan.\n * @param targetBlocks - Blocks of the existing target document.\n * @param reviewedSegments - Map of action index to its reviewed translation.\n * @returns The rebuilt target document.\n */\nexport const mergeReviewedSegments = (\n plan: AlignmentPlan,\n targetBlocks: FingerprintedBlock[],\n reviewedSegments: Map<number, string>\n): string => {\n let mergedText = '';\n let previousPartWasGenerated = false;\n\n /**\n * Append one part, keeping a blank line around the content this run produced.\n *\n * Blocks own the blank lines that trail them, so two verbatim blocks already\n * separate themselves and are appended untouched — a document with nothing to\n * change merges back byte for byte. Generated content is the exception: the\n * block it lands after may be the one that used to end the document, which\n * carries no trailing blank line, and the two would be glued into a single\n * markdown block (a heading swallowed by the paragraph above it).\n */\n const appendPart = (content: string, isGenerated: boolean): void => {\n if (content.length === 0) return;\n\n if (isGenerated || previousPartWasGenerated) {\n mergedText += buildBlockSeparator(mergedText);\n }\n\n mergedText += content;\n previousPartWasGenerated = isGenerated;\n };\n\n plan.actions.forEach((action, actionIndex) => {\n if (action.kind === 'reuse') {\n appendPart(targetBlocks[action.targetIndex]!.content, false);\n } else if (action.kind === 'review' || action.kind === 'insert_new') {\n const reviewedContent = reviewedSegments.get(actionIndex);\n\n if (reviewedContent !== undefined) {\n appendPart(reviewedContent, true);\n } else {\n // Fallback: if review failed, use existing or blank\n if (action.kind === 'review' && action.targetIndex !== null) {\n appendPart(targetBlocks[action.targetIndex]!.content, false);\n } else {\n appendPart('\\n', false);\n }\n }\n } else if (action.kind === 'delete') {\n const reviewedContent = reviewedSegments.get(actionIndex);\n if (reviewedContent !== undefined) {\n // Caller explicitly resolved this block: empty string = actually delete,\n // non-empty string = replacement content.\n appendPart(reviewedContent, true);\n } else {\n // Default: keep verbatim. A target block with no base counterpart may\n // just be a section the aligner could not follow (reordering, split\n // prose) — keeping it prevents accidental data loss in log/read-only mode.\n appendPart(targetBlocks[action.targetIndex]!.content, false);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tB,WAAW,iBAAiB,IAAI;QAGhC,IAAI,OAAO,SAAS,YAAY,OAAO,gBAAgB,MACrD,WAAW,aAAa,OAAO,YAAY,CAAE,SAAS,KAAK;QAE3D,WAAW,MAAM,KAAK;EAG5B,OAAO,IAAI,OAAO,SAAS,UAAU;GACnC,MAAM,kBAAkB,iBAAiB,IAAI,WAAW;GACxD,IAAI,oBAAoB,QAGtB,WAAW,iBAAiB,IAAI;QAKhC,WAAW,aAAa,OAAO,YAAY,CAAE,SAAS,KAAK;EAE/D;CACF,CAAC;CAED,OAAO;AACT"}
@@ -1,8 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  //#region src/docReview/segmentDocument.ts
3
3
  const HEADING_PATTERN = /^\s*(#{1,6})\s+/;
4
- const isBlankLine = (line) => line.trim().length === 0;
5
- const isFencedCodeDelimiter = (line) => /^\s*```/.test(line);
4
+ const isBlankLine = (line) => !line || line.trim().length === 0;
5
+ const isFencedCodeDelimiter = (line) => Boolean(line && /^\s*```/.test(line));
6
6
  /**
7
7
  * Read the depth of an ATX markdown heading (`#` → 1, `######` → 6).
8
8
  *
@@ -10,10 +10,11 @@ const isFencedCodeDelimiter = (line) => /^\s*```/.test(line);
10
10
  * @returns The heading depth, or `null` when the line is not a heading.
11
11
  */
12
12
  const parseHeadingDepth = (line) => {
13
+ if (!line) return null;
13
14
  return HEADING_PATTERN.exec(line)?.[1]?.length ?? null;
14
15
  };
15
16
  const isHeading = (line) => parseHeadingDepth(line) !== null;
16
- const isFrontmatterDelimiter = (line) => /^\s*---\s*$/.test(line);
17
+ const isFrontmatterDelimiter = (line) => Boolean(line && /^\s*---\s*$/.test(line));
17
18
  /**
18
19
  * Split a markdown document into fine-grained blocks.
19
20
  *
@@ -88,7 +89,8 @@ const segmentDocument = (text) => {
88
89
  if (units.length === 0) return [];
89
90
  return units.map((unit, unitIndex) => {
90
91
  const blockStartIndex = unitIndex === 0 ? 0 : unit.startIndex;
91
- const blockEndIndex = unitIndex === units.length - 1 ? lineCount - 1 : units[unitIndex + 1].startIndex - 1;
92
+ const nextUnit = units[unitIndex + 1];
93
+ const blockEndIndex = unitIndex === units.length - 1 || !nextUnit ? lineCount - 1 : nextUnit.startIndex - 1;
92
94
  const blockLines = lines.slice(blockStartIndex, blockEndIndex + 1);
93
95
  const content = blockEndIndex < lineCount - 1 ? `${blockLines.join("\n")}\n` : blockLines.join("\n");
94
96
  return {
@@ -124,8 +126,9 @@ const segmentSections = (text) => {
124
126
  let currentBlocks = [];
125
127
  const flushSection = () => {
126
128
  if (currentBlocks.length === 0) return;
127
- const [firstBlock] = currentBlocks;
129
+ const firstBlock = currentBlocks[0];
128
130
  const lastBlock = currentBlocks[currentBlocks.length - 1];
131
+ if (!firstBlock || !lastBlock) return;
129
132
  sections.push({
130
133
  type: firstBlock.type,
131
134
  content: currentBlocks.map((block) => block.content).join(""),
@@ -1 +1 @@
1
- {"version":3,"file":"segmentDocument.cjs","names":[],"sources":["../../../src/docReview/segmentDocument.ts"],"sourcesContent":["import type { Block, BlockType } from './types';\n\nconst HEADING_PATTERN = /^\\s*(#{1,6})\\s+/;\n\nconst isBlankLine = (line: string): boolean => line.trim().length === 0;\nconst isFencedCodeDelimiter = (line: string): boolean => /^\\s*```/.test(line);\n\n/**\n * Read the depth of an ATX markdown heading (`#` → 1, `######` → 6).\n *\n * @param line - The line to inspect.\n * @returns The heading depth, or `null` when the line is not a heading.\n */\nconst parseHeadingDepth = (line: string): number | null => {\n const match = HEADING_PATTERN.exec(line);\n\n return match?.[1]?.length ?? null;\n};\n\nconst isHeading = (line: string): boolean => parseHeadingDepth(line) !== null;\nconst isFrontmatterDelimiter = (line: string): boolean =>\n /^\\s*---\\s*$/.test(line);\n\n/**\n * A content unit (heading, paragraph, code block or frontmatter) spanning a\n * 0-based, inclusive line range. Blank-line runs are not units of their own;\n * they are folded into the preceding unit as trailing separators (see\n * {@link segmentDocument}) so that the concatenation of every block's content\n * reproduces the document byte-for-byte.\n */\ntype ContentUnit = {\n type: BlockType;\n /** Depth of the ATX heading opening the unit, `null` when it is not a heading. */\n headingDepth: number | null;\n startIndex: number;\n endIndex: number;\n};\n\n/**\n * Split a markdown document into fine-grained blocks.\n *\n * Boundaries are drawn at headings, blank lines (paragraph breaks) and fenced\n * code blocks, while frontmatter and the inside of code fences are kept intact.\n * Each blank-line run is appended to the block that precedes it, so the blocks\n * form an exact partition of the document: concatenating every `content` (in\n * order) yields the original text unchanged. This is what lets the block-aware\n * review re-translate only the paragraphs/snippets that actually changed instead\n * of the whole heading section.\n *\n * @param text - The full markdown document.\n * @returns The ordered list of blocks with their 1-based line ranges.\n */\nexport const segmentDocument = (text: string): Block[] => {\n const lines = text.split('\\n');\n const lineCount = lines.length;\n\n // 1. Tokenize into content units, skipping blank-line runs (folded in below).\n const units: ContentUnit[] = [];\n let index = 0;\n\n while (index < lineCount) {\n const currentLine = lines[index];\n\n if (isBlankLine(currentLine)) {\n index += 1;\n continue;\n }\n\n // Frontmatter: only when it opens the document.\n if (units.length === 0 && isFrontmatterDelimiter(currentLine)) {\n const startIndex = index;\n index += 1;\n while (index < lineCount && !isFrontmatterDelimiter(lines[index])) {\n index += 1;\n }\n // Include the closing delimiter when present.\n if (index < lineCount) index += 1;\n units.push({\n type: 'unknown',\n headingDepth: null,\n startIndex,\n endIndex: index - 1,\n });\n continue;\n }\n\n // Fenced code block: consumed whole so inner blank lines and `#` lines are\n // never treated as boundaries.\n if (isFencedCodeDelimiter(currentLine)) {\n const startIndex = index;\n index += 1;\n while (index < lineCount && !isFencedCodeDelimiter(lines[index])) {\n index += 1;\n }\n // Include the closing fence when present.\n if (index < lineCount) index += 1;\n units.push({\n type: 'code_block',\n headingDepth: null,\n startIndex,\n endIndex: index - 1,\n });\n continue;\n }\n\n // Heading: a single self-contained line.\n const headingDepth = parseHeadingDepth(currentLine);\n\n if (headingDepth !== null) {\n units.push({\n type: 'heading',\n headingDepth,\n startIndex: index,\n endIndex: index,\n });\n index += 1;\n continue;\n }\n\n // Paragraph: a run of consecutive lines until a blank line, a heading or a\n // code fence. Tables and tight lists stay together (no blank line between\n // their rows/items).\n const startIndex = index;\n while (\n index < lineCount &&\n !isBlankLine(lines[index]) &&\n !isHeading(lines[index]) &&\n !isFencedCodeDelimiter(lines[index])\n ) {\n index += 1;\n }\n units.push({\n type: 'paragraph',\n headingDepth: null,\n startIndex,\n endIndex: index - 1,\n });\n }\n\n if (units.length === 0) return [];\n\n // 2. Turn each unit into a block whose line range extends to just before the\n // next unit, so the trailing blank-line run is owned by it. The first block\n // also absorbs any leading blank lines, and the last block runs to EOF.\n return units.map((unit, unitIndex): Block => {\n const blockStartIndex = unitIndex === 0 ? 0 : unit.startIndex;\n const blockEndIndex =\n unitIndex === units.length - 1\n ? lineCount - 1\n : units[unitIndex + 1].startIndex - 1;\n\n const blockLines = lines.slice(blockStartIndex, blockEndIndex + 1);\n // Re-append the boundary newline dropped by `split` for every block but the\n // one ending at EOF, so concatenating all blocks rebuilds the document.\n const content =\n blockEndIndex < lineCount - 1\n ? `${blockLines.join('\\n')}\\n`\n : blockLines.join('\\n');\n\n return {\n type: unit.type,\n content,\n headingDepth: unit.headingDepth,\n lineStart: blockStartIndex + 1,\n lineEnd: blockEndIndex + 1,\n };\n });\n};\n\n/**\n * Split a markdown document into coarse, heading-anchored sections.\n *\n * Built by grouping the fine blocks of {@link segmentDocument}: frontmatter and\n * each heading open a new section, and the following non-heading blocks are\n * folded into it. Because it only concatenates adjacent fine blocks, the result\n * is still an exact partition of the document (sections concatenate back to the\n * source unchanged).\n *\n * Sections are the robust alignment unit between a base document and its\n * translation — both share the same heading structure, so they align almost\n * perfectly and a translation that splits its prose into a different number of\n * paragraphs never causes a section to be dropped. Fine-grained review happens\n * within a section once it is known to have changed.\n *\n * @param text - The full markdown document.\n * @returns The ordered list of sections with their 1-based line ranges.\n */\nexport const segmentSections = (text: string): Block[] => {\n const fineBlocks = segmentDocument(text);\n const sections: Block[] = [];\n let currentBlocks: Block[] = [];\n\n const flushSection = (): void => {\n if (currentBlocks.length === 0) return;\n\n const [firstBlock] = currentBlocks;\n const lastBlock = currentBlocks[currentBlocks.length - 1];\n\n sections.push({\n type: firstBlock.type,\n content: currentBlocks.map((block) => block.content).join(''),\n // A section is identified by the heading that opens it, so it inherits its\n // depth — this is what keeps a `##` section from aligning with a `###` one.\n headingDepth: firstBlock.headingDepth,\n lineStart: firstBlock.lineStart,\n lineEnd: lastBlock.lineEnd,\n });\n currentBlocks = [];\n };\n\n for (const block of fineBlocks) {\n // Frontmatter (a leading `unknown` block) and every heading open a section.\n const opensSection =\n block.type === 'heading' ||\n (block.type === 'unknown' && sections.length === 0);\n\n if (opensSection) flushSection();\n currentBlocks.push(block);\n if (block.type === 'unknown' && sections.length === 0) flushSection();\n }\n\n flushSection();\n\n return sections;\n};\n"],"mappings":";;AAEA,MAAM,kBAAkB;AAExB,MAAM,eAAe,SAA0B,KAAK,KAAK,CAAC,CAAC,WAAW;AACtE,MAAM,yBAAyB,SAA0B,UAAU,KAAK,IAAI;;;;;;;AAQ5E,MAAM,qBAAqB,SAAgC;CAGzD,OAFc,gBAAgB,KAAK,IAExB,CAAC,GAAG,EAAE,EAAE,UAAU;AAC/B;AAEA,MAAM,aAAa,SAA0B,kBAAkB,IAAI,MAAM;AACzE,MAAM,0BAA0B,SAC9B,cAAc,KAAK,IAAI;;;;;;;;;;;;;;;AA+BzB,MAAa,mBAAmB,SAA0B;CACxD,MAAM,QAAQ,KAAK,MAAM,IAAI;CAC7B,MAAM,YAAY,MAAM;CAGxB,MAAM,QAAuB,CAAC;CAC9B,IAAI,QAAQ;CAEZ,OAAO,QAAQ,WAAW;EACxB,MAAM,cAAc,MAAM;EAE1B,IAAI,YAAY,WAAW,GAAG;GAC5B,SAAS;GACT;EACF;EAGA,IAAI,MAAM,WAAW,KAAK,uBAAuB,WAAW,GAAG;GAC7D,MAAM,aAAa;GACnB,SAAS;GACT,OAAO,QAAQ,aAAa,CAAC,uBAAuB,MAAM,MAAM,G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;;AAoBA,MAAa,mBAAmB,SAA0B;CACxD,MAAM,aAAa,gBAAgB,IAAI;CACvC,MAAM,WAAoB,CAAC;CAC3B,IAAI,gBAAyB,CAAC;CAE9B,MAAM,qBAA2B;EAC/B,IAAI,cAAc,WAAW,GAAG;EAEhC,MAAM,CAAC,cAAc;EACrB,MAAM,YAAY,cAAc,cAAc,SAAS;EAEvD,SAAS,KAAK;GACZ,MAAM,WAAW;GACjB,SAAS,cAAc,KAAK,UAAU,MAAM,OAAO,CAAC,CAAC,KAAK,EAAE;GAG5D,cAAc,WAAW;GACzB,WAAW,WAAW;GACtB,SAAS,UAAU;EACrB,CAAC;EACD,gBAAgB,CAAC;CACnB;CAEA,KAAK,MAAM,SAAS,YAAY;EAM9B,IAHE,MAAM,SAAS,aACd,MAAM,SAAS,aAAa,SAAS,WAAW,GAEjC,aAAa;EAC/B,cAAc,KAAK,KAAK;EACxB,IAAI,MAAM,SAAS,aAAa,SAAS,WAAW,GAAG,aAAa;CACtE;CAEA,aAAa;CAEb,OAAO;AACT"}
1
+ {"version":3,"file":"segmentDocument.cjs","names":[],"sources":["../../../src/docReview/segmentDocument.ts"],"sourcesContent":["import type { Block, BlockType } from './types';\n\nconst HEADING_PATTERN = /^\\s*(#{1,6})\\s+/;\n\nconst isBlankLine = (line?: string): boolean =>\n !line || line.trim().length === 0;\nconst isFencedCodeDelimiter = (line?: string): boolean =>\n Boolean(line && /^\\s*```/.test(line));\n\n/**\n * Read the depth of an ATX markdown heading (`#` → 1, `######` → 6).\n *\n * @param line - The line to inspect.\n * @returns The heading depth, or `null` when the line is not a heading.\n */\nconst parseHeadingDepth = (line?: string): number | null => {\n if (!line) return null;\n const match = HEADING_PATTERN.exec(line);\n\n return match?.[1]?.length ?? null;\n};\n\nconst isHeading = (line?: string): boolean => parseHeadingDepth(line) !== null;\nconst isFrontmatterDelimiter = (line?: string): boolean =>\n Boolean(line && /^\\s*---\\s*$/.test(line));\n\n/**\n * A content unit (heading, paragraph, code block or frontmatter) spanning a\n * 0-based, inclusive line range. Blank-line runs are not units of their own;\n * they are folded into the preceding unit as trailing separators (see\n * {@link segmentDocument}) so that the concatenation of every block's content\n * reproduces the document byte-for-byte.\n */\ntype ContentUnit = {\n type: BlockType;\n /** Depth of the ATX heading opening the unit, `null` when it is not a heading. */\n headingDepth: number | null;\n startIndex: number;\n endIndex: number;\n};\n\n/**\n * Split a markdown document into fine-grained blocks.\n *\n * Boundaries are drawn at headings, blank lines (paragraph breaks) and fenced\n * code blocks, while frontmatter and the inside of code fences are kept intact.\n * Each blank-line run is appended to the block that precedes it, so the blocks\n * form an exact partition of the document: concatenating every `content` (in\n * order) yields the original text unchanged. This is what lets the block-aware\n * review re-translate only the paragraphs/snippets that actually changed instead\n * of the whole heading section.\n *\n * @param text - The full markdown document.\n * @returns The ordered list of blocks with their 1-based line ranges.\n */\nexport const segmentDocument = (text: string): Block[] => {\n const lines = text.split('\\n');\n const lineCount = lines.length;\n\n // 1. Tokenize into content units, skipping blank-line runs (folded in below).\n const units: ContentUnit[] = [];\n let index = 0;\n\n while (index < lineCount) {\n const currentLine = lines[index];\n\n if (isBlankLine(currentLine)) {\n index += 1;\n continue;\n }\n\n // Frontmatter: only when it opens the document.\n if (units.length === 0 && isFrontmatterDelimiter(currentLine)) {\n const startIndex = index;\n index += 1;\n while (index < lineCount && !isFrontmatterDelimiter(lines[index])) {\n index += 1;\n }\n // Include the closing delimiter when present.\n if (index < lineCount) index += 1;\n units.push({\n type: 'unknown',\n headingDepth: null,\n startIndex,\n endIndex: index - 1,\n });\n continue;\n }\n\n // Fenced code block: consumed whole so inner blank lines and `#` lines are\n // never treated as boundaries.\n if (isFencedCodeDelimiter(currentLine)) {\n const startIndex = index;\n index += 1;\n while (index < lineCount && !isFencedCodeDelimiter(lines[index])) {\n index += 1;\n }\n // Include the closing fence when present.\n if (index < lineCount) index += 1;\n units.push({\n type: 'code_block',\n headingDepth: null,\n startIndex,\n endIndex: index - 1,\n });\n continue;\n }\n\n // Heading: a single self-contained line.\n const headingDepth = parseHeadingDepth(currentLine);\n\n if (headingDepth !== null) {\n units.push({\n type: 'heading',\n headingDepth,\n startIndex: index,\n endIndex: index,\n });\n index += 1;\n continue;\n }\n\n // Paragraph: a run of consecutive lines until a blank line, a heading or a\n // code fence. Tables and tight lists stay together (no blank line between\n // their rows/items).\n const startIndex = index;\n while (\n index < lineCount &&\n !isBlankLine(lines[index]) &&\n !isHeading(lines[index]) &&\n !isFencedCodeDelimiter(lines[index])\n ) {\n index += 1;\n }\n units.push({\n type: 'paragraph',\n headingDepth: null,\n startIndex,\n endIndex: index - 1,\n });\n }\n\n if (units.length === 0) return [];\n\n // 2. Turn each unit into a block whose line range extends to just before the\n // next unit, so the trailing blank-line run is owned by it. The first block\n // also absorbs any leading blank lines, and the last block runs to EOF.\n return units.map((unit, unitIndex): Block => {\n const blockStartIndex = unitIndex === 0 ? 0 : unit.startIndex;\n const nextUnit = units[unitIndex + 1];\n const blockEndIndex =\n unitIndex === units.length - 1 || !nextUnit\n ? lineCount - 1\n : nextUnit.startIndex - 1;\n\n const blockLines = lines.slice(blockStartIndex, blockEndIndex + 1);\n // Re-append the boundary newline dropped by `split` for every block but the\n // one ending at EOF, so concatenating all blocks rebuilds the document.\n const content =\n blockEndIndex < lineCount - 1\n ? `${blockLines.join('\\n')}\\n`\n : blockLines.join('\\n');\n\n return {\n type: unit.type,\n content,\n headingDepth: unit.headingDepth,\n lineStart: blockStartIndex + 1,\n lineEnd: blockEndIndex + 1,\n };\n });\n};\n\n/**\n * Split a markdown document into coarse, heading-anchored sections.\n *\n * Built by grouping the fine blocks of {@link segmentDocument}: frontmatter and\n * each heading open a new section, and the following non-heading blocks are\n * folded into it. Because it only concatenates adjacent fine blocks, the result\n * is still an exact partition of the document (sections concatenate back to the\n * source unchanged).\n *\n * Sections are the robust alignment unit between a base document and its\n * translation — both share the same heading structure, so they align almost\n * perfectly and a translation that splits its prose into a different number of\n * paragraphs never causes a section to be dropped. Fine-grained review happens\n * within a section once it is known to have changed.\n *\n * @param text - The full markdown document.\n * @returns The ordered list of sections with their 1-based line ranges.\n */\nexport const segmentSections = (text: string): Block[] => {\n const fineBlocks = segmentDocument(text);\n const sections: Block[] = [];\n let currentBlocks: Block[] = [];\n\n const flushSection = (): void => {\n if (currentBlocks.length === 0) return;\n\n const firstBlock = currentBlocks[0];\n const lastBlock = currentBlocks[currentBlocks.length - 1];\n\n if (!firstBlock || !lastBlock) return;\n\n sections.push({\n type: firstBlock.type,\n content: currentBlocks.map((block) => block.content).join(''),\n // A section is identified by the heading that opens it, so it inherits its\n // depth — this is what keeps a `##` section from aligning with a `###` one.\n headingDepth: firstBlock.headingDepth,\n lineStart: firstBlock.lineStart,\n lineEnd: lastBlock.lineEnd,\n });\n currentBlocks = [];\n };\n\n for (const block of fineBlocks) {\n // Frontmatter (a leading `unknown` block) and every heading open a section.\n const opensSection =\n block.type === 'heading' ||\n (block.type === 'unknown' && sections.length === 0);\n\n if (opensSection) flushSection();\n currentBlocks.push(block);\n if (block.type === 'unknown' && sections.length === 0) flushSection();\n }\n\n flushSection();\n\n return sections;\n};\n"],"mappings":";;AAEA,MAAM,kBAAkB;AAExB,MAAM,eAAe,SACnB,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC,WAAW;AAClC,MAAM,yBAAyB,SAC7B,QAAQ,QAAQ,UAAU,KAAK,IAAI,CAAC;;;;;;;AAQtC,MAAM,qBAAqB,SAAiC;CAC1D,IAAI,CAAC,MAAM,OAAO;CAGlB,OAFc,gBAAgB,KAAK,IAExB,CAAC,GAAG,EAAE,EAAE,UAAU;AAC/B;AAEA,MAAM,aAAa,SAA2B,kBAAkB,IAAI,MAAM;AAC1E,MAAM,0BAA0B,SAC9B,QAAQ,QAAQ,cAAc,KAAK,IAAI,CAAC;;;;;;;;;;;;;;;AA+B1C,MAAa,mBAAmB,SAA0B;CACxD,MAAM,QAAQ,KAAK,MAAM,IAAI;CAC7B,MAAM,YAAY,MAAM;CAGxB,MAAM,QAAuB,CA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AAa;EAE5B,MAAM,aAAa,MAAM,MAAM,iBAAiB,gBAAgB,CAAC;EAGjE,MAAM,UACJ,gBAAgB,YAAY,IACxB,GAAG,WAAW,KAAK,IAAI,EAAE,MACzB,WAAW,KAAK,IAAI;EAE1B,OAAO;GACL,MAAM,KAAK;GACX;GACA,cAAc,KAAK;GACnB,WAAW,kBAAkB;GAC7B,SAAS,gBAAgB;EAC3B;CACF,CAAC;AACH;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,mBAAmB,SAA0B;CACxD,MAAM,aAAa,gBAAgB,IAAI;CACvC,MAAM,WAAoB,CAAC;CAC3B,IAAI,gBAAyB,CAAC;CAE9B,MAAM,qBAA2B;EAC/B,IAAI,cAAc,WAAW,GAAG;EAEhC,MAAM,aAAa,cAAc;EACjC,MAAM,YAAY,cAAc,cAAc,SAAS;EAEvD,IAAI,CAAC,cAAc,CAAC,WAAW;EAE/B,SAAS,KAAK;GACZ,MAAM,WAAW;GACjB,SAAS,cAAc,KAAK,UAAU,MAAM,OAAO,CAAC,CAAC,KAAK,EAAE;GAG5D,cAAc,WAAW;GACzB,WAAW,WAAW;GACtB,SAAS,UAAU;EACrB,CAAC;EACD,gBAAgB,CAAC;CACnB;CAEA,KAAK,MAAM,SAAS,YAAY;EAM9B,IAHE,MAAM,SAAS,aACd,MAAM,SAAS,aAAa,SAAS,WAAW,GAEjC,aAAa;EAC/B,cAAc,KAAK,KAAK;EACxB,IAAI,MAAM,SAAS,aAAa,SAAS,WAAW,GAAG,aAAa;CACtE;CAEA,aAAa;CAEb,OAAO;AACT"}
@@ -74,7 +74,10 @@ const setHtmlLang = (ast) => {
74
74
  const langAttr = node.attributes?.find((attr) => attr.type === "JSXAttribute" && attr.name?.name === "lang");
75
75
  const localeExpression = b.jsxExpressionContainer(b.identifier("locale"));
76
76
  if (langAttr) langAttr.value = localeExpression;
77
- else node.attributes.push(b.jsxAttribute(b.jsxIdentifier("lang"), localeExpression));
77
+ else {
78
+ node.attributes = node.attributes ?? [];
79
+ node.attributes.push(b.jsxAttribute(b.jsxIdentifier("lang"), localeExpression));
80
+ }
78
81
  return false;
79
82
  }
80
83
  this.traverse(path);