@nocode-factory/sanity-kit 2.65.0-rc.3 → 2.65.0-rc.5
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/CHANGELOG.md +25 -0
- package/bin/ncf-kit.mjs +38 -3
- package/bin/translation-audit.mjs +39 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +138 -34
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/PropagatePublish.tsx +25 -1
- package/src/form/TranslationTodoInput.tsx +5 -1
- package/src/lib/linkedTranslations.ts +51 -0
- package/src/lib/propagate.ts +29 -1
- package/src/lib/translateDocument.ts +51 -19
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useState} from 'react'
|
|
1
|
+
import {useEffect, useState} from 'react'
|
|
2
2
|
import {Box, Button, Card, Checkbox, Flex, Stack, Text} from '@sanity/ui'
|
|
3
3
|
import {useToast} from '@sanity/ui/toast'
|
|
4
4
|
import {useSchema, type DocumentActionComponent, type DocumentActionProps} from 'sanity'
|
|
@@ -11,6 +11,7 @@ import {translateValuesSettled} from '../lib/aiTranslate'
|
|
|
11
11
|
import {createTranslatedVersion, translationsOf} from '../lib/translateDocument'
|
|
12
12
|
import {isReplaceable, readTrace, recordTrace} from '../lib/translationTrace'
|
|
13
13
|
import {localizeReferences} from '../lib/localizeReferences'
|
|
14
|
+
import {linkedWithoutVersion} from '../lib/linkedTranslations'
|
|
14
15
|
|
|
15
16
|
type Doc = Record<string, unknown>
|
|
16
17
|
type Client = ReturnType<typeof useKitClient>
|
|
@@ -150,6 +151,18 @@ function PropagateDialog(props: {
|
|
|
150
151
|
const field = config.i18n?.field ?? 'language'
|
|
151
152
|
const source = String(after[field] ?? '')
|
|
152
153
|
const [targets, setTargets] = useState<Target[]>(props.targets)
|
|
154
|
+
// Pages liées sans version publiée dans une langue : la version de cette langue sera
|
|
155
|
+
// créée, mais ne pourra pas être publiée avant elles (dit AVANT de créer).
|
|
156
|
+
const [unlinked, setUnlinked] = useState<Map<string, string[]>>(new Map())
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
let alive = true
|
|
159
|
+
linkedWithoutVersion(client, after, props.targets.map((t) => t.language), field)
|
|
160
|
+
.then((m) => alive && setUnlinked(m))
|
|
161
|
+
.catch(() => {})
|
|
162
|
+
return () => {
|
|
163
|
+
alive = false
|
|
164
|
+
}
|
|
165
|
+
}, [client, after, props.targets, field])
|
|
153
166
|
const [busy, setBusy] = useState(false)
|
|
154
167
|
const toast = useToast()
|
|
155
168
|
const [error, setError] = useState<string | null>(null)
|
|
@@ -318,6 +331,17 @@ function PropagateDialog(props: {
|
|
|
318
331
|
{mode === 'create' ? `New version (${t.result.toTranslate.length} texts)` : summary(t.result)}
|
|
319
332
|
{t.edited.length ? ` · ${t.edited.length} edited by hand` : ''}
|
|
320
333
|
</Text>
|
|
334
|
+
{unlinked.get(t.language)?.length ? (
|
|
335
|
+
<Card tone="caution" padding={2} radius={2}>
|
|
336
|
+
<Text size={1}>
|
|
337
|
+
Links to {unlinked.get(t.language)!.length} page{unlinked.get(t.language)!.length > 1 ? 's' : ''} with no published {t.language.toUpperCase()} version (
|
|
338
|
+
{unlinked.get(t.language)!.slice(0, 4).map((title) => `« ${title} »`).join(', ')}
|
|
339
|
+
{unlinked.get(t.language)!.length > 4 ? ', …' : ''}): this version can be saved as a draft, but not published until
|
|
340
|
+
{unlinked.get(t.language)!.length > 1 ? ' those pages are' : ' that page is'} translated and published. Not meant to be translated?
|
|
341
|
+
In the {t.language.toUpperCase()} version, link to its address directly (link type URL) or remove the link.
|
|
342
|
+
</Text>
|
|
343
|
+
</Card>
|
|
344
|
+
) : null}
|
|
321
345
|
</Stack>
|
|
322
346
|
</Box>
|
|
323
347
|
</Flex>
|
|
@@ -35,7 +35,11 @@ export function withTranslationTodo(Inner: ComponentType<InputProps>, config: Re
|
|
|
35
35
|
.getDocument(sourceId)
|
|
36
36
|
.then((doc) => {
|
|
37
37
|
if (!alive) return
|
|
38
|
-
const
|
|
38
|
+
const value = valueAtKey(doc, key)
|
|
39
|
+
// Slug : sa valeur est dans `current` (ce n'est pas un texte). Avant, la source
|
|
40
|
+
// d'un slug s'affichait « vide » alors qu'il était rempli (vu sur JAF).
|
|
41
|
+
const slug = value && typeof value === 'object' && typeof (value as {current?: unknown}).current === 'string' ? (value as {current: string}).current : ''
|
|
42
|
+
const text = slug || (typeof value === 'string' ? value : collectTexts(value).join('').trim())
|
|
39
43
|
setMaster(text || '(empty: follows the component default)')
|
|
40
44
|
})
|
|
41
45
|
.catch(() => alive && setMaster(null))
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {SanityClient} from 'sanity'
|
|
2
|
+
|
|
3
|
+
type Doc = Record<string, unknown>
|
|
4
|
+
|
|
5
|
+
/** Identifiants des documents référencés par `doc` (liens, pages liées...), hors images et fichiers. */
|
|
6
|
+
export function referencedIds(doc: unknown): string[] {
|
|
7
|
+
const out = new Set<string>()
|
|
8
|
+
const walk = (node: unknown) => {
|
|
9
|
+
if (Array.isArray(node)) return node.forEach(walk)
|
|
10
|
+
if (!node || typeof node !== 'object') return
|
|
11
|
+
const ref = (node as {_ref?: unknown})._ref
|
|
12
|
+
if (typeof ref === 'string' && !/^(image|file)-/.test(ref)) out.add(ref.replace(/^drafts\./, ''))
|
|
13
|
+
for (const [k, v] of Object.entries(node)) if (!k.startsWith('_')) walk(v)
|
|
14
|
+
}
|
|
15
|
+
walk(doc)
|
|
16
|
+
return [...out]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Pages liées depuis `doc` qui n'ont pas de version PUBLIÉE dans chacune des `languages`
|
|
21
|
+
* (kit >= 2.65.0). La version traduite d'un document qui les vise ne peut pas être
|
|
22
|
+
* publiée (« Cette page est en FR et n'a pas de version EN », vu sur JAF) : on le dit
|
|
23
|
+
* AVANT de la créer, avec les pages à traduire d'abord.
|
|
24
|
+
*/
|
|
25
|
+
export async function linkedWithoutVersion(
|
|
26
|
+
client: SanityClient,
|
|
27
|
+
doc: Doc,
|
|
28
|
+
languages: string[],
|
|
29
|
+
field = 'language',
|
|
30
|
+
): Promise<Map<string, string[]>> {
|
|
31
|
+
const result = new Map<string, string[]>()
|
|
32
|
+
const ids = referencedIds(doc)
|
|
33
|
+
if (!ids.length || !languages.length) return result
|
|
34
|
+
const linked = await client.fetch<{_id: string; title: string; language?: string; versions: {language?: string; id?: string}[] | null}[]>(
|
|
35
|
+
`*[_id in $ids && defined(${field})]{
|
|
36
|
+
_id, "title": coalesce(title, name, slug.current, _id), "language": ${field},
|
|
37
|
+
"versions": *[_type == "translation.metadata" && references(^._id)][0].translations[]{"language": coalesce(language, _key), "id": value._ref}
|
|
38
|
+
}`,
|
|
39
|
+
{ids},
|
|
40
|
+
)
|
|
41
|
+
const versionIds = linked.flatMap((d) => (d.versions ?? []).map((v) => v.id ?? '')).filter(Boolean)
|
|
42
|
+
const published = new Set(versionIds.length ? await client.fetch<string[]>(`*[_id in $ids]._id`, {ids: versionIds}) : [])
|
|
43
|
+
for (const language of languages)
|
|
44
|
+
for (const d of linked) {
|
|
45
|
+
if (d.language === language) continue
|
|
46
|
+
const version = (d.versions ?? []).find((v) => v.language === language)
|
|
47
|
+
if (version?.id && published.has(version.id)) continue
|
|
48
|
+
result.set(language, [...(result.get(language) ?? []), d.title])
|
|
49
|
+
}
|
|
50
|
+
return result
|
|
51
|
+
}
|
package/src/lib/propagate.ts
CHANGED
|
@@ -44,6 +44,23 @@ export interface PropagationResult {
|
|
|
44
44
|
const VALUE_TYPE_NAMES = new Set(['image', 'file', 'reference', 'crossDatasetReference', 'slug', 'geopoint', 'url', 'email', 'date', 'datetime', 'number', 'boolean'])
|
|
45
45
|
const SKIP_KEYS = new Set(['language', 'slug'])
|
|
46
46
|
|
|
47
|
+
// Champs texte qui ne se TRADUISENT pas (kit >= 2.65.0, vu sur JAF : ancres traduites en
|
|
48
|
+
// « start of school year », couleur « sun », téléphone et email envoyés à l'IA) :
|
|
49
|
+
// - par leur nom : ancres (d'une section ou de la page visée par un lien), adresses,
|
|
50
|
+
// jetons de couleur, codes, icônes ;
|
|
51
|
+
// - par leur valeur : email, téléphone, adresse web ou chemin, ancre, couleur hexadécimale.
|
|
52
|
+
const NON_TEXT_NAMES = /^(anchor|pageAnchor|.*Anchor|href|url|link|email|mail|phone|tel|telephone|token|customHex|hex|color|colour|icon|code|key|id|slug|path|target|rel)$/i
|
|
53
|
+
const NON_TEXT_VALUE = /^(?:[\w.+-]+@[\w-]+\.[\w.-]+|\+?[\d\s().-]{6,}|(?:https?:|mailto:|tel:)\S*|[/#]\S*|#[0-9a-f]{3,8})$/i
|
|
54
|
+
|
|
55
|
+
/** Un texte libre qui n'est pas à traduire (nom du champ, ou valeur technique). */
|
|
56
|
+
export function isNonTextValue(name: string | undefined, value: unknown): boolean {
|
|
57
|
+
if (name && NON_TEXT_NAMES.test(name)) return true
|
|
58
|
+
return typeof value === 'string' && NON_TEXT_VALUE.test(value.trim())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Dernier nom de champ d'un chemin lisible (« sections[k].cta.anchor » → « anchor »). */
|
|
62
|
+
const lastName = (path: string) => path.split('.').pop()?.replace(/\[.*$/, '')
|
|
63
|
+
|
|
47
64
|
const isObj = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v)
|
|
48
65
|
const same = (a: unknown, b: unknown) => JSON.stringify(a ?? null) === JSON.stringify(b ?? null)
|
|
49
66
|
const keyed = (v: unknown): v is Record<string, unknown>[] => Array.isArray(v) && v.length > 0 && v.every((i) => isObj(i) && typeof i._key === 'string')
|
|
@@ -61,6 +78,9 @@ export function kindOf(t: SchemaTypeLike | undefined, value: unknown): FieldKind
|
|
|
61
78
|
// Champ propre à l'instance (options.instanceOnly, ex. ancre d'une section) :
|
|
62
79
|
// une valeur recopiée telle quelle, jamais traduite.
|
|
63
80
|
if (t.options?.instanceOnly) return 'value'
|
|
81
|
+
// Image avec ses propres champs (alt, légende) : ses textes se traduisent, l'image
|
|
82
|
+
// (asset, recadrage) est recopiée. Avant, l'image entière était recopiée, alt compris.
|
|
83
|
+
if (names.includes('image') && hasOwnFields(t)) return 'object'
|
|
64
84
|
if (names.some((n) => VALUE_TYPE_NAMES.has(n)) || t.to) return 'value'
|
|
65
85
|
if (t.jsonType === 'array') {
|
|
66
86
|
const of = t.of ?? []
|
|
@@ -78,6 +98,11 @@ export function kindOf(t: SchemaTypeLike | undefined, value: unknown): FieldKind
|
|
|
78
98
|
return 'value'
|
|
79
99
|
}
|
|
80
100
|
|
|
101
|
+
function hasOwnFields(t: SchemaTypeLike | undefined): boolean {
|
|
102
|
+
for (let c = t; c; c = c.type) if (c.fields?.some((f) => !['asset', 'hotspot', 'crop', 'media'].includes(f.name))) return true
|
|
103
|
+
return false
|
|
104
|
+
}
|
|
105
|
+
|
|
81
106
|
function fieldType(t: SchemaTypeLike | undefined, name: string): SchemaTypeLike | undefined {
|
|
82
107
|
for (let c = t; c; c = c.type) {
|
|
83
108
|
const f = c.fields?.find((x) => x.name === name)
|
|
@@ -104,6 +129,7 @@ interface Acc {
|
|
|
104
129
|
/** Tous les textes d'un élément ajouté : à traduire. */
|
|
105
130
|
function collectTexts(value: unknown, t: SchemaTypeLike | undefined, path: string, acc: Acc) {
|
|
106
131
|
const kind = kindOf(t, value)
|
|
132
|
+
if (kind === 'text' && typeof value === 'string' && isNonTextValue(lastName(path), value)) return
|
|
107
133
|
if (kind === 'text') {
|
|
108
134
|
if (value !== undefined && value !== null && value !== '') acc.toTranslate.push(path)
|
|
109
135
|
} else if (kind === 'object' && isObj(value)) {
|
|
@@ -117,7 +143,9 @@ const ABSENT = Symbol('absent')
|
|
|
117
143
|
|
|
118
144
|
function merge(target: unknown, before: unknown, after: unknown, t: SchemaTypeLike | undefined, path: string, acc: Acc): unknown {
|
|
119
145
|
if (same(before, after)) return target === undefined ? ABSENT : target
|
|
120
|
-
|
|
146
|
+
let kind = kindOf(t, after ?? before)
|
|
147
|
+
// Texte libre technique (ancre, email, téléphone, couleur...) : recopié, jamais traduit.
|
|
148
|
+
if (kind === 'text' && typeof (after ?? before) === 'string' && isNonTextValue(lastName(path), after ?? before)) kind = 'value'
|
|
121
149
|
|
|
122
150
|
if (kind === 'text') {
|
|
123
151
|
if (after === undefined || after === null || after === '') {
|
|
@@ -5,7 +5,7 @@ import type {ResolvedKitConfig} from '../config/types'
|
|
|
5
5
|
import {TRANSLATION_TODO_TYPE, translationTodoId} from '../schema/translationTodo'
|
|
6
6
|
import {collectTexts, setAtKey, valueAtKey} from './aiTexts'
|
|
7
7
|
import {translateValuesSettled} from './aiTranslate'
|
|
8
|
-
import {propagate, type SchemaTypeLike} from './propagate'
|
|
8
|
+
import {isNonTextValue, propagate, type SchemaTypeLike} from './propagate'
|
|
9
9
|
import {slugify} from './slugify'
|
|
10
10
|
import {hashValue, recordTrace} from './translationTrace'
|
|
11
11
|
|
|
@@ -84,6 +84,39 @@ export async function linkTranslations(
|
|
|
84
84
|
* traduits par l'IA si `ai` (service du kit), slug traduit et unique, publiée si
|
|
85
85
|
* tout est traduit (sinon brouillon et textes « To translate »). Relie la version.
|
|
86
86
|
*/
|
|
87
|
+
/** Champ slug d'un type (registre des pages du kit, sinon « slug » s'il est rempli). */
|
|
88
|
+
function slugFieldOf(config: ResolvedKitConfig, type: string, source: Doc): string | null {
|
|
89
|
+
const pageType = config.pageTypes.find((t) => t.type === type)
|
|
90
|
+
const field = pageType?.slugField ?? ((source.slug as {current?: string} | undefined)?.current ? 'slug' : null)
|
|
91
|
+
return field && (source[field] as {current?: string} | undefined)?.current ? field : null
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Slug d'une version traduite : tiré de son titre (déjà traduit), unique dans la langue
|
|
96
|
+
* (-2, -3...) ; à défaut de titre, le slug de la version source. JAMAIS « traduit » comme
|
|
97
|
+
* un texte (vu sur JAF : une reprise avait vidé le slug d'une page).
|
|
98
|
+
*/
|
|
99
|
+
async function translatedSlug(client: SanityClient, config: ResolvedKitConfig, type: string, doc: Doc, source: Doc, language: string): Promise<{_type: 'slug'; current: string}> {
|
|
100
|
+
const field = config.i18n?.field ?? 'language'
|
|
101
|
+
const pageType = config.pageTypes.find((t) => t.type === type)
|
|
102
|
+
const slugField = slugFieldOf(config, type, source) ?? 'slug'
|
|
103
|
+
const titleField = pageType?.titleField ?? 'title'
|
|
104
|
+
const title = typeof doc[titleField] === 'string' ? (doc[titleField] as string) : ''
|
|
105
|
+
const base = slugify(title) || String((source[slugField] as {current: string}).current)
|
|
106
|
+
const id = typeof doc._id === 'string' ? doc._id.replace(/^drafts\./, '') : ''
|
|
107
|
+
let slug = base
|
|
108
|
+
for (
|
|
109
|
+
let n = 2;
|
|
110
|
+
await client.fetch<boolean>(
|
|
111
|
+
`count(*[_type == $type && ${slugField}.current == $slug && ${field} == $language && !(_id in path("drafts.**")) && _id != $id]) > 0`,
|
|
112
|
+
{type, slug, language, id},
|
|
113
|
+
);
|
|
114
|
+
n++
|
|
115
|
+
)
|
|
116
|
+
slug = `${base}-${n}`
|
|
117
|
+
return {_type: 'slug', current: slug}
|
|
118
|
+
}
|
|
119
|
+
|
|
87
120
|
export async function createTranslatedVersion(opts: {
|
|
88
121
|
client: SanityClient
|
|
89
122
|
config: ResolvedKitConfig
|
|
@@ -133,23 +166,8 @@ export async function createTranslatedVersion(opts: {
|
|
|
133
166
|
Object.assign(doc, await localizeReferences(client, doc, language))
|
|
134
167
|
// Slug : traduit depuis le titre, unique dans la langue (-2, -3...).
|
|
135
168
|
const pageType = config.pageTypes.find((t) => t.type === type)
|
|
136
|
-
const slugField =
|
|
137
|
-
if (slugField
|
|
138
|
-
const titleField = pageType?.titleField ?? 'title'
|
|
139
|
-
const title = typeof doc[titleField] === 'string' ? (doc[titleField] as string) : ''
|
|
140
|
-
const base = slugify(title) || String((source[slugField] as {current: string}).current)
|
|
141
|
-
let slug = base
|
|
142
|
-
for (
|
|
143
|
-
let n = 2;
|
|
144
|
-
await client.fetch<boolean>(
|
|
145
|
-
`count(*[_type == $type && ${slugField}.current == $slug && ${field} == $language && !(_id in path("drafts.**"))]) > 0`,
|
|
146
|
-
{type, slug, language},
|
|
147
|
-
);
|
|
148
|
-
n++
|
|
149
|
-
)
|
|
150
|
-
slug = `${base}-${n}`
|
|
151
|
-
doc[slugField] = {_type: 'slug', current: slug}
|
|
152
|
-
}
|
|
169
|
+
const slugField = slugFieldOf(config, type, source)
|
|
170
|
+
if (slugField) doc[slugField] = await translatedSlug(client, config, type, doc, source, language)
|
|
153
171
|
// Slug non traduit (titre resté dans la langue source) : marqué « To translate »
|
|
154
172
|
// avec le titre, la règle d'or veut un slug dans la langue de la version.
|
|
155
173
|
if (slugField && remaining.includes(pageType?.titleField ?? 'title') && !remaining.includes(slugField)) remaining = [...remaining, slugField]
|
|
@@ -255,7 +273,15 @@ export async function retryTranslatedVersion(opts: {
|
|
|
255
273
|
if (!source || !current) return {published: false, remaining: todo.paths, aiError: !source ? 'source version not found (unpublished or deleted)' : 'translated version not found'}
|
|
256
274
|
if (!config.history) return {published: false, remaining: todo.paths, aiError: 'AI translation is not configured (kit project key missing)'}
|
|
257
275
|
const {_rev, _createdAt, _updatedAt, ...doc} = structuredClone(current) as Doc
|
|
258
|
-
|
|
276
|
+
const type = String(current._type)
|
|
277
|
+
// Slug : jamais traduit comme un texte, recalculé depuis le titre traduit (plus bas).
|
|
278
|
+
// Valeurs techniques (ancre, email, téléphone, couleur) marquées par une version
|
|
279
|
+
// antérieure du kit : retirées de la liste, gardées telles quelles.
|
|
280
|
+
const slugField = slugFieldOf(config, type, source)
|
|
281
|
+
const nameOf = (path: string) => path.split('.').pop()?.replace(/\[.*$/, '')
|
|
282
|
+
const technical = (path: string) => isNonTextValue(nameOf(path), valueAtKey(source, path))
|
|
283
|
+
const slugPending = Boolean(slugField && todo.paths.includes(slugField))
|
|
284
|
+
let remaining = todo.paths.filter((p) => p !== slugField && !technical(p))
|
|
259
285
|
let aiError: string | null = null
|
|
260
286
|
const written: {path: string; value: unknown}[] = []
|
|
261
287
|
try {
|
|
@@ -274,6 +300,12 @@ export async function retryTranslatedVersion(opts: {
|
|
|
274
300
|
} catch (cause) {
|
|
275
301
|
aiError = cause instanceof Error ? cause.message : String(cause)
|
|
276
302
|
}
|
|
303
|
+
if (slugPending && slugField) {
|
|
304
|
+
const titleField = config.pageTypes.find((t) => t.type === type)?.titleField ?? 'title'
|
|
305
|
+
// Titre traduit : slug recalculé depuis lui ; sinon le slug reste « To translate ».
|
|
306
|
+
if (remaining.includes(titleField)) remaining = [...remaining, slugField]
|
|
307
|
+
else doc[slugField] = await translatedSlug(client, config, type, doc, source, language)
|
|
308
|
+
}
|
|
277
309
|
const published = publish && !remaining.length && !aiError
|
|
278
310
|
const tx = client.transaction()
|
|
279
311
|
if (published) {
|
package/src/version.ts
CHANGED