@uniweb/build 1.13.2 → 1.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/build",
3
- "version": "1.13.2",
3
+ "version": "1.13.4",
4
4
  "description": "Build tooling for the Uniweb Component Web Platform",
5
5
  "type": "module",
6
6
  "exports": {
@@ -56,12 +56,12 @@
56
56
  "js-yaml": "^4.3.2",
57
57
  "sharp": "^0.35.5",
58
58
  "yaml": "^2.5.0",
59
- "@uniweb/semantic-parser": "~1.13.0",
60
- "@uniweb/content-writer": "~1.3.1",
61
- "@uniweb/theming": "~1.2.1",
62
59
  "@uniweb/content-reader": "~1.5.7",
60
+ "@uniweb/content-writer": "~1.3.1",
63
61
  "@uniweb/schemas": "~1.6.7",
64
- "@uniweb/projections": "~1.2.8"
62
+ "@uniweb/projections": "~1.2.8",
63
+ "@uniweb/theming": "~1.2.1",
64
+ "@uniweb/semantic-parser": "~1.13.0"
65
65
  },
66
66
  "optionalDependencies": {
67
67
  "@uniweb/runtime": "~1.3.3"
package/src/i18n/audit.js CHANGED
@@ -48,6 +48,7 @@ export async function auditLocale(localesPath, locale) {
48
48
  const missing = []
49
49
  const stale = []
50
50
  const losesMarkup = []
51
+ const outdated = []
51
52
 
52
53
  // Check manifest entries
53
54
  for (const hash of manifestHashes) {
@@ -55,6 +56,11 @@ export async function auditLocale(localesPath, locale) {
55
56
  const unit = manifest.units[hash]
56
57
  const source = unit.source
57
58
  const translation = getTranslationText(translations[hash])
59
+ // Made from an older source text (`locale-sync.js::settleEntry`). It still shows.
60
+ if (translations[hash]?.outdated) {
61
+ outdated.push({ hash, source, translation, was: [...new Set(Object.values(translations[hash].outdated))] })
62
+ continue
63
+ }
58
64
  valid.push({ hash, source, translation })
59
65
 
60
66
  // ⭐ The source element carries inline markdown, and what the merge makes of
@@ -98,7 +104,8 @@ export async function auditLocale(localesPath, locale) {
98
104
  valid,
99
105
  missing,
100
106
  stale,
101
- losesMarkup
107
+ losesMarkup,
108
+ outdated
102
109
  }
103
110
  }
104
111
 
package/src/i18n/index.js CHANGED
@@ -15,7 +15,8 @@ import { computeHash, normalizeText } from './hash.js'
15
15
  import { availableLocales, resolveLocaleList, localesToBuild } from './locales.js'
16
16
  import { extractTranslatableContent } from './extract.js'
17
17
  import { dataBlockModels } from './data-models.js'
18
- import { syncManifests, formatSyncReport } from './sync.js'
18
+ import { syncManifests, formatSyncReport, formatKeptTranslations } from './sync.js'
19
+ import { keepEditedTranslations, rowsWantTranslation, manifestRows } from './outdated.js'
19
20
  import { mergeTranslations, generateAllLocales } from './merge.js'
20
21
  import { auditLocale, cleanLocale, formatAuditReport } from './audit.js'
21
22
  import {
@@ -64,6 +65,9 @@ export {
64
65
  extractTranslatableContent,
65
66
  syncManifests,
66
67
  formatSyncReport,
68
+ formatKeptTranslations,
69
+ keepEditedTranslations,
70
+ rowsWantTranslation,
67
71
  mergeTranslations,
68
72
  generateAllLocales,
69
73
 
@@ -182,6 +186,12 @@ export async function extractManifest(siteRoot, siteContent, options = {}) {
182
186
  // Generate sync report
183
187
  const report = syncManifests(previousManifest, manifest)
184
188
 
189
+ // What the manifest is about to forget says what an edited text's translation was made from.
190
+ report.kept = keepEditedTranslations(siteRoot, manifestRows(previousManifest), manifestRows(manifest), {
191
+ sourceLocale: manifest.defaultLocale,
192
+ dryRun
193
+ })
194
+
185
195
  // Write new manifest (skip in dry-run mode)
186
196
  if (!dryRun) {
187
197
  await writeFile(manifestPath, JSON.stringify(manifest, null, 2))
@@ -282,9 +292,13 @@ export async function getTranslationStatus(siteRoot, options = {}) {
282
292
 
283
293
  let translated = 0
284
294
  let missing = 0
295
+ let outdated = 0
285
296
 
286
297
  for (const hash of Object.keys(manifest.units)) {
287
- if (translatedHashes.has(hash)) {
298
+ // An entry flagged `outdated` still renders, and is not this text's translation.
299
+ if (translations[hash]?.outdated) {
300
+ outdated++
301
+ } else if (translatedHashes.has(hash)) {
288
302
  translated++
289
303
  } else {
290
304
  missing++
@@ -295,6 +309,7 @@ export async function getTranslationStatus(siteRoot, options = {}) {
295
309
  exists: true,
296
310
  translated,
297
311
  missing,
312
+ ...(outdated ? { outdated } : {}),
298
313
  coverage: totalUnits > 0 ? Math.round((translated / totalUnits) * 100) : 100
299
314
  }
300
315
  }
@@ -642,6 +657,9 @@ export function formatTranslationStatus(status) {
642
657
  if (info.missing > 0) {
643
658
  lines.push(` ${info.missing} strings missing`)
644
659
  }
660
+ if (info.outdated > 0) {
661
+ lines.push(` ${info.outdated} outdated: the old translation still shows, and its source has changed`)
662
+ }
645
663
  }
646
664
  }
647
665
 
@@ -13,7 +13,8 @@ import { existsSync, readdirSync } from 'node:fs'
13
13
  import { normalizeLanguageList, resolveDefaultLocale } from '@uniweb/core/locale-config'
14
14
 
15
15
  // Files in the locales directory that are not a locale's translations.
16
- export const RESERVED_LOCALE_FILES = new Set(['manifest.json', '_memory.json'])
16
+ export const KEPT_FILE = '_kept.json'
17
+ export const RESERVED_LOCALE_FILES = new Set(['manifest.json', '_memory.json', KEPT_FILE])
17
18
 
18
19
  /**
19
20
  * The locales a directory holds translations for — one per `<locale>.json` — sorted.
@@ -0,0 +1,146 @@
1
+ /**
2
+ * ⭐ A SOURCE EDIT KEEPS ITS TRANSLATION UNTIL SOMEONE REVIEWS IT, in a file as in the app: a slightly
3
+ * outdated sentence beats a sudden one in the source language, and a typo fixed in the source should
4
+ * not change a translated page. So a row with no translation, in a place where a similar text has gone,
5
+ * takes that text's translation, and its entry says so (`{ default, outdated: { place: older text } }`).
6
+ * It is what a pull writes for an edit made in the app (`uwx/locale-sync.js::settleEntry`), and a push
7
+ * sends it back as the app keeps it.
8
+ *
9
+ * ⛔ Until 2026-10-31 the row fell back to the source at the next build or push, whatever the edit.
10
+ *
11
+ * Each lane reads the old text from its own record: `uniweb i18n sync` from the manifest it is about to
12
+ * replace, a push from the store it is about to change. Only a block element's text in a page's section,
13
+ * as the app's rows are: a picture, a data block's string, a page's title and a record's body fall back
14
+ * as they did.
15
+ */
16
+
17
+ import { computeHash } from './hash.js'
18
+ import { toPlainText } from './merge.js'
19
+ import { isSmallEdit, wordOverlap } from './word-overlap.js'
20
+ import { discoverLocales, loadLocaleTranslations, translationFor, addTranslationEntries } from '../uwx/locale-sync.js'
21
+
22
+ // A language's words for a text here. Not an empty one, nor the text's own: `uniweb i18n init` starts a
23
+ // language's file with each text's source, and that is nothing to keep.
24
+ function translationHere(table, text, context) {
25
+ const hash = computeHash(text)
26
+ const said = translationFor(table[hash], context)
27
+ if (typeof said !== 'string' || said.trim() === '' || computeHash(toPlainText(said)) === hash) return undefined
28
+ return said
29
+ }
30
+
31
+ // Whether a language has nothing written for a row here.
32
+ function wants(table, row, context) {
33
+ const said = translationFor(table[computeHash(row)], context)
34
+ return typeof said !== 'string' || said.trim() === ''
35
+ }
36
+
37
+ /**
38
+ * The entries a language takes for what changed. A row with a translation here keeps it, and a text
39
+ * that has gone goes to one row, the most like it: of a sentence split in two, one half keeps it.
40
+ *
41
+ * @param {{ [place: string]: string[] }} previous - each place's rows as they were
42
+ * @param {{ [place: string]: string[] }} current - each place's rows now, in order
43
+ * @param {object} table - the language's translation file
44
+ * @returns {{ [hash: string]: object }} what to add to the file
45
+ */
46
+ export function carriedTranslations(previous, current, table) {
47
+ const added = {}
48
+ for (const [place, rows] of Object.entries(current)) {
49
+ const context = { key: place }
50
+ const now = new Set(rows.map(computeHash))
51
+ const gone = new Map()
52
+ for (const text of previous[place] || []) {
53
+ const hash = computeHash(text)
54
+ const said = translationHere(table, text, context)
55
+ if (!now.has(hash) && said !== undefined) gone.set(hash, { text, said })
56
+ }
57
+ if (gone.size === 0) continue
58
+ const wanting = new Map()
59
+ for (const row of rows) {
60
+ if (wants(table, row, context)) wanting.set(computeHash(row), row)
61
+ }
62
+ const pairs = []
63
+ for (const [hash, row] of wanting) {
64
+ for (const [from, { text }] of gone) {
65
+ if (isSmallEdit(text, row)) pairs.push({ hash, from, overlap: wordOverlap(text, row) })
66
+ }
67
+ }
68
+ pairs.sort((x, y) => y.overlap - x.overlap)
69
+ const taken = new Set()
70
+ for (const { hash, from } of pairs) {
71
+ if (taken.has(hash) || !gone.has(from)) continue
72
+ const { text, said } = gone.get(from)
73
+ gone.delete(from)
74
+ taken.add(hash)
75
+ const entry = (added[hash] ||= table[hash] && typeof table[hash] === 'object' ? { ...table[hash] } : {})
76
+ if (entry.default === undefined) entry.default = said
77
+ else if (entry.default !== said) entry.overrides = { ...entry.overrides, [place]: said }
78
+ entry.outdated = { ...entry.outdated, [place]: text }
79
+ }
80
+ }
81
+ return added
82
+ }
83
+
84
+ /**
85
+ * What each place held at the last extraction: a manifest's units that are a block element's text.
86
+ *
87
+ * @param {object|null} manifest
88
+ * @returns {{ [place: string]: string[] }}
89
+ */
90
+ export function manifestRows(manifest) {
91
+ const rows = {}
92
+ for (const unit of Object.values(manifest?.units || {})) {
93
+ if (!/^(title|subtitle|heading|paragraph|list)(\.|$)/.test(unit.field || '')) continue
94
+ for (const context of unit.contexts || []) {
95
+ if (context.section === '_meta' || context.section === '_site') continue
96
+ ;(rows[`${context.page}:${context.section}`] ||= []).push(unit.source)
97
+ }
98
+ }
99
+ return rows
100
+ }
101
+
102
+ // Each language's translation file but the source's, if it has one.
103
+ function languageTables(siteRoot, sourceLocale) {
104
+ return loadLocaleTranslations(siteRoot, discoverLocales(siteRoot).filter((locale) => locale !== sourceLocale))
105
+ }
106
+
107
+ /**
108
+ * Whether a language has no words for one of these rows. Only such a row can keep a translation, so a
109
+ * push that finds none has no reason to read the store.
110
+ *
111
+ * @param {string} siteRoot
112
+ * @param {{ [place: string]: string[] }} current
113
+ * @param {{ sourceLocale?: string }} [o]
114
+ * @returns {boolean}
115
+ */
116
+ export function rowsWantTranslation(siteRoot, current, { sourceLocale = null } = {}) {
117
+ const tables = Object.values(languageTables(siteRoot, sourceLocale))
118
+ return Object.entries(current).some(([place, rows]) =>
119
+ rows.some((row) => tables.some((table) => wants(table, row, { key: place }))),
120
+ )
121
+ }
122
+
123
+ /**
124
+ * Give each language's file what `carriedTranslations` finds, and say what it found.
125
+ *
126
+ * @param {string} siteRoot
127
+ * @param {{ [place: string]: string[] }} previous
128
+ * @param {{ [place: string]: string[] }} current
129
+ * @param {object} [o]
130
+ * @param {string} [o.sourceLocale] - its file, if it has one, is no translation
131
+ * @param {boolean} [o.dryRun]
132
+ * @returns {{ [locale: string]: Array<{ was: string, now: string }> }} a language with nothing kept is absent
133
+ */
134
+ export function keepEditedTranslations(siteRoot, previous, current, { sourceLocale = null, dryRun = false } = {}) {
135
+ const sourceOf = new Map(Object.values(current).flat().map((text) => [computeHash(text), text]))
136
+ const kept = {}
137
+ for (const [locale, table] of Object.entries(languageTables(siteRoot, sourceLocale))) {
138
+ const added = carriedTranslations(previous, current, table)
139
+ if (Object.keys(added).length === 0) continue
140
+ if (!dryRun) addTranslationEntries(siteRoot, locale, added)
141
+ kept[locale] = Object.entries(added).flatMap(([hash, entry]) =>
142
+ [...new Set(Object.values(entry.outdated))].map((was) => ({ was, now: sourceOf.get(hash) })),
143
+ )
144
+ }
145
+ return kept
146
+ }
@@ -11,6 +11,7 @@
11
11
  */
12
12
 
13
13
  import { readFile, writeFile, readdir, mkdir, rm } from 'fs/promises'
14
+ import { RESERVED_LOCALE_FILES } from './locales.js'
14
15
  import { existsSync } from 'node:fs'
15
16
  import { join, resolve, sep } from 'node:path'
16
17
  import { pathToFileURL } from 'node:url'
@@ -1338,7 +1339,7 @@ export async function getRecordLocales(localesPath) {
1338
1339
  try {
1339
1340
  const files = await readdir(recordLocalesDir)
1340
1341
  return files
1341
- .filter(f => f.endsWith('.json') && f !== 'manifest.json')
1342
+ .filter(f => f.endsWith('.json') && !RESERVED_LOCALE_FILES.has(f))
1342
1343
  .map(f => f.replace('.json', ''))
1343
1344
  .sort()
1344
1345
  } catch {
package/src/i18n/sync.js CHANGED
@@ -208,6 +208,8 @@ export function formatSyncReport(report) {
208
208
  }
209
209
  }
210
210
 
211
+ lines.push(...formatKeptTranslations(report.kept).map((line) => ` ${line}`))
212
+
211
213
  if (report.added.length > 0) {
212
214
  lines.push(` + ${report.added.length} new strings`)
213
215
  for (const item of report.added.slice(0, 5)) {
@@ -235,6 +237,25 @@ export function formatSyncReport(report) {
235
237
  return lines.join('\n')
236
238
  }
237
239
 
240
+ /**
241
+ * What `keepEditedTranslations` kept, as lines to print.
242
+ *
243
+ * @param {{ [locale: string]: Array<{ was: string, now: string }> }} [kept]
244
+ * @returns {string[]} none when nothing was kept
245
+ */
246
+ export function formatKeptTranslations(kept) {
247
+ const pairs = new Map()
248
+ for (const { was, now } of Object.values(kept || {}).flat()) pairs.set(`${was}\n${now}`, { was, now })
249
+ if (pairs.size === 0) return []
250
+ const lines = [
251
+ `↺ ${pairs.size} edited string${pairs.size === 1 ? ' keeps its' : 's keep their'} old translation until someone reviews it (${Object.keys(kept).join(', ')})`,
252
+ ]
253
+ for (const { was, now } of [...pairs.values()].slice(0, 5)) lines.push(` - "${truncate(was, 30)}" → "${truncate(now, 30)}"`)
254
+ if (pairs.size > 5) lines.push(` ... and ${pairs.size - 5} more`)
255
+ lines.push(' Each is flagged `outdated` in the language files: replace it with the new translation, or with the same words as a plain string if they still hold.')
256
+ return lines
257
+ }
258
+
238
259
  /**
239
260
  * Format a context object for display
240
261
  */
@@ -0,0 +1,34 @@
1
+ /**
2
+ * What counts as a small edit: the app's rule for when a translation stays with its row, flagged, after
3
+ * the row's source changes. Each side holds its own copy, so a pull and a push flag the rows the editor
4
+ * flags. The editor's was measured from the outside on 2026-10-31, with pairs pushed to it
5
+ * (`tests/i18n-edit-keeps-translation.test.js`), since it re-tests a row it reads: a row kept under a text
6
+ * it does not count as similar shows the old translation, and no flag.
7
+ */
8
+
9
+ const wordsOf = (text) => String(text).toLowerCase().match(/[\p{L}\p{N}]+/gu) || []
10
+
11
+ /**
12
+ * How much of two texts is the same: twice the words they share, each as often as both have it, over the
13
+ * words of both. Case and punctuation are no words, and a hyphen or an apostrophe splits one.
14
+ *
15
+ * @param {string} a
16
+ * @param {string} b
17
+ * @returns {number} from 0 to 1
18
+ */
19
+ export function wordOverlap(a, b) {
20
+ const [first, second] = [wordsOf(a), wordsOf(b)]
21
+ if (first.length + second.length === 0) return 0
22
+ const left = new Map()
23
+ for (const word of first) left.set(word, (left.get(word) || 0) + 1)
24
+ let shared = 0
25
+ for (const word of second) {
26
+ if (!left.get(word)) continue
27
+ left.set(word, left.get(word) - 1)
28
+ shared++
29
+ }
30
+ return (2 * shared) / (first.length + second.length)
31
+ }
32
+
33
+ /** Whether a translation made from one text stays with another. */
34
+ export const isSmallEdit = (a, b) => wordOverlap(a, b) >= 0.5
@@ -1170,9 +1170,11 @@ export function siteContentPlugin(options = {}) {
1170
1170
 
1171
1171
  let result = html
1172
1172
 
1173
- // Update <html lang> for non-default locales
1174
- if (activeLocale) {
1175
- result = result.replace(/<html([^>]*)\slang="[^"]*"/, `<html$1 lang="${activeLocale}"`)
1173
+ // <html lang> is the language this page is in. ⛔ Until 2026-10-30 only the other locales were
1174
+ // rewritten, so a site written in Spanish built its default pages as `lang="en"`, the scaffold's.
1175
+ const lang = activeLocale || resolveDefaultLocale(contentToInject?.config)
1176
+ if (lang) {
1177
+ result = result.replace(/<html([^>]*)\slang="[^"]*"/, `<html$1 lang="${lang}"`)
1176
1178
  }
1177
1179
 
1178
1180
  if (!headInjection) return result
@@ -43,7 +43,7 @@ import { parseFrontmatter } from './entity-source.js'
43
43
  import { setYamlKeyFirst } from './yaml-edit.js'
44
44
  import { isProseMirrorField, isOpenMapSection, openMapFromRows } from './data-schema.js'
45
45
  import { recordFileLayout, contentBodyTarget } from './record-layout.js'
46
- import { unwrapLocalizedContent } from './locale-sync.js'
46
+ import { recordFieldPlace, unwrapLocalizedContent } from './locale-sync.js'
47
47
  import { parseBibtex, exportBibtex } from '@citestyle/bibtex'
48
48
 
49
49
  // Probed in this order to locate a single-record source file by slug.
@@ -351,7 +351,7 @@ function decodeRecord(fields, value, dec, sectionName) {
351
351
  continue
352
352
  }
353
353
  const isBody = Boolean(dec.bodyAt) && sectionName === dec.bodyAt.section && key === dec.bodyAt.key
354
- const decoded = decodeLeaf(raw, field, dec, isBody)
354
+ const decoded = decodeLeaf(raw, field, dec, isBody, key)
355
355
  if (isBody) dec.body = typeof decoded === 'string' ? decoded : String(decoded ?? '')
356
356
  // A translatable value with no text in any language is no value: the field is not written.
357
357
  else if (decoded !== undefined) out[key] = decoded
@@ -386,11 +386,11 @@ function decodeList(def, value, dec) {
386
386
  // list of localized values is unwrapped element by element. ⛔ Until 2026-09-26 a body was
387
387
  // taken to be ProseMirror, so a markdown one's translations were captured nowhere and a
388
388
  // clone rendered it in the source language.
389
- function decodeLeaf(raw, field, dec, isBody) {
389
+ function decodeLeaf(raw, field, dec, isBody, key) {
390
390
  if (field?.type === 'entity_ref') return decodeReference(raw, field, dec)
391
391
  if (isProseMirrorField(field)) {
392
392
  const sourceDoc = field.localized
393
- ? unwrapLocalizedContent(raw, dec.sourceLocale, dec.collector, isBody ? dec.freeformRelPath : undefined, null, dec.context)
393
+ ? unwrapLocalizedContent(raw, dec.sourceLocale, dec.collector, isBody ? dec.freeformRelPath : undefined, null, dec.context, undefined, dec.context ? recordFieldPlace(dec.context, key) : null)
394
394
  : raw
395
395
  return sourceDoc ? proseMirrorToMarkdown(sourceDoc) : ''
396
396
  }
package/src/uwx/index.js CHANGED
@@ -41,8 +41,10 @@ export {
41
41
  foundationSchemaToEntity,
42
42
  emitFoundationSchemaPackage,
43
43
  } from './foundation-schema.js'
44
+ export { documentSourceLocale } from './source-locale.js'
44
45
  export {
45
46
  siteProjectToDocument,
47
+ documentRows,
46
48
  emitSiteSyncPackage,
47
49
  writeSiteEntityUuid,
48
50
  extensionDeclaration,
@@ -138,6 +140,7 @@ export {
138
140
  diffSiteUnits,
139
141
  describeSiteDiff,
140
142
  computeUnitHashes,
143
+ sectionsChangedSince,
141
144
  collectSiteUnits,
142
145
  walkSiteUnits,
143
146
  collectUnitUuids,